Websocket4Net 发送多条消息
Posted
技术标签:
【中文标题】Websocket4Net 发送多条消息【英文标题】:Websocket4Net Send Multiple messages 【发布时间】:2015-08-25 16:30:54 【问题描述】:我在使用 WebSocket4Net 库时遇到问题。
我有事件 websocket_opened 并且当这个事件在打开时引发如果我不发送任何消息我有一个异常
System.Exception : 你必须在 websocket 之后通过 websocket 发送数据 打开
发送此消息后,我无法发送任何其他消息。我执行了发送命令,但我没有例外,它不工作,如果我检查 websocket 的打开状态
如果我在打开的事件上关闭套接字并在 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();
这正常吗?你能帮我解决这个问题吗?
【问题讨论】:
这听起来不对。你用它作为客户端还是服务器? 【参考方案1】:我第一次使用 WebSocket4Net 时也有类似的问题。 这个库的一个好处是它有一个repository on github 和许多examples of use (tests)。这些测试对我理解它应该如何工作很有帮助。
更新: 我在下面看到了 Fred 的评论,我想我真的需要添加一个示例。 就这样吧,我用过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();
【讨论】:
【参考方案2】:我认为这将解决您的问题
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
【讨论】:
以上是关于Websocket4Net 发送多条消息的主要内容,如果未能解决你的问题,请参考以下文章