Websocket4Net发送多条消息

时间:2015-08-25 16:30:55

标签: c# websocket websocket4net

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

我有事件websocket_opened,如果我没有发送任何消息,那么当这个事件被打开时,我有一个例外

  

System.Exception:必须在websocket之后通过websocket发送数据   开

发送此消息后,我无法发送任何其他消息。我执行send命令,但我没有例外,它没有工作,如果我检查状态websocket是打开的

如果我关闭已打开事件的Socket并在closed_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();
}

这是正常的吗?你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

第一次使用WebSocket4Net时,我遇到了类似的问题。 这个库的一个好处是repository on github有很多examples of use (tests)。这些测试对我理解它应该如何工作非常有帮助。

<强>更新 我在下面看到弗雷德的评论,我想我真的需要添加一个例子。 所以就是这样,我使用了this 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();
    }
  }
}

答案 1 :(得分:-1)

我认为这将解决您的问题

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
}