发送多个消息



我在使用WebSocket4Net库时遇到了问题。

我有事件websocket_opened,当这个事件在打开时引发,如果我没有发送任何消息,我有一个异常

系统。例外:在websocket失效后,必须通过websocket发送数据打开

发送此消息后,我不能发送任何其他消息。我执行发送命令,但我没有异常,它不工作,如果我检查状态,websocket是打开的

如果我在打开的事件上关闭套接字并在close_event中再次打开它,我可以发送另一条消息而不会出现问题。

所以,如果我想发送多条消息,我必须在发送消息后断开连接并重新连接以发送下一条消息。

private static void websocket_Opened(object sender, EventArgs e)
{
    websocket.Send(msg);
    websocket.Close();
}
private static void websocket_Closed(object sender, EventArgs e)
{
    websocket.Open();
}

这正常吗?你能帮我一下吗?

我第一次使用WebSocket4Net时也有类似的问题。这个库的一个好处是,它在github上有一个存储库,其中有许多使用示例(测试)。这些测试对我理解它应该如何工作很有帮助。

更新:我看到了弗雷德下面的评论,我想我真的需要添加一个例子。这里,我使用了这个api:

using SuperSocket.ClientEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebSocket4Net;
namespace TestProj
{
  public class WebSocketTest
  {
    static string webSocketUri = "wss://ws.binaryws.com/websockets/v3?app_id=1089";
    static void Main(string[] args)
    {
        var socketManager = new WebSocketManager(webSocketUri);
        socketManager.Send("{"time": 1}");
        socketManager.Send("{"ping": 1}");
        socketManager.Close();
        Console.WriteLine("Console can be closed...");
        Console.ReadLine();
    }
}
public class WebSocketManager
{
    private AutoResetEvent messageReceiveEvent = new AutoResetEvent(false);
    private string lastMessageReceived;
    private WebSocket webSocket;
    public WebSocketManager(string webSocketUri)
    {
        Console.WriteLine("Initializing websocket. Uri: " + webSocketUri);
        webSocket = new WebSocket(webSocketUri);
        webSocket.Opened += new EventHandler(websocket_Opened);
        webSocket.Closed += new EventHandler(websocket_Closed);
        webSocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
        webSocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
        webSocket.Open();
        while (webSocket.State == WebSocketState.Connecting) { };   // by default webSocket4Net has AutoSendPing=true, 
                                                                    // so we need to wait until connection established
        if (webSocket.State != WebSocketState.Open)
        {
            throw new Exception("Connection is not opened.");
        }
    }
    public string Send(string data)
    {
        Console.WriteLine("Client wants to send data:");
        Console.WriteLine(data);
        webSocket.Send(data);
        if (!messageReceiveEvent.WaitOne(5000))                         // waiting for the response with 5 secs timeout
            Console.WriteLine("Cannot receive the response. Timeout.");
        return lastMessageReceived;
    }
    public void Close()
    {
        Console.WriteLine("Closing websocket...");
        webSocket.Close();
    }
    private void websocket_Opened(object sender, EventArgs e)
    {
        Console.WriteLine("Websocket is opened.");
    }
    private void websocket_Error(object sender, ErrorEventArgs e)
    {
        Console.WriteLine(e.Exception.Message);
    }
    private void websocket_Closed(object sender, EventArgs e)
    {
        Console.WriteLine("Websocket is closed.");
    }
    private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        Console.WriteLine("Message received: " + e.Message);
        lastMessageReceived = e.Message;
        messageReceiveEvent.Set();
    }
  }
}
我想这会解决你的问题。
websocket.Open();
private static void websocket_Opened(object sender, EventArgs e)
{
    websocket.Send(msg); 
    websocket.Close(); 
}
private static void websocket_Closed(object sender, EventArgs e)
{
    //DO SOMETHINGS
}

相关内容

  • 没有找到相关文章

最新更新