C# WebSocket【使用SuperWebSocket实现】
一) SuperWebSocket实现服务端
1、创建窗口程序,WindowsFormsWebsocketServer
2、添加程序包
工具 -->Nuget包管理 -->管理解决方案的Nuget程序包 -->搜索 SuperWebSocket ,选择SuperWebSocketNETServer,点击右侧 安装,等待安装完成,安装完成以后,项目会多出很多引用库,如下
public partial class Form1 : Form { public Form1() { InitializeComponent(); websocketServer(); } private void websocketServer() { Log("我是服务端"); WebSocketServer webSocketServer = new WebSocketServer(); webSocketServer.NewSessionConnected += WebSocketServer_NewSessionConnected; webSocketServer.NewMessageReceived += WebSocketServer_NewMessageReceived; webSocketServer.SessionClosed += WebSocketServer_SessionClosed; if (!webSocketServer.Setup("127.0.0.1", 1234)) { Log("设置服务监听失败!"); } if (!webSocketServer.Start()) { Log("启动服务监听失败!"); } Log("启动服务监听成功!"); //webSocketServer.Dispose(); } private void WebSocketServer_NewSessionConnected(WebSocketSession session) { Log("欢迎客户端: 加入"); //SendToAll(session, msg); } private void WebSocketServer_NewMessageReceived(WebSocketSession session, string value) { Log("服务端收到客户端的数据 ==》"+value); //SendToAll(session, value); } private void WebSocketServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value) { Log("客户端:关闭,原因:"); //SendToAll(session, msg); } /// <summary> /// 广播,同步推送消息给所有的客户端 /// </summary> /// <param name="webSocketSession"></param> /// <param name="msg"></param> public static void SendToAll(WebSocketSession webSocketSession, string msg) { foreach (var item in webSocketSession.AppServer.GetAllSessions()) { item.Send(msg); } } private delegate void DoLog(string msg); public void Log(string msg) { if (this.logReveal.InvokeRequired) { DoLog doLog = new DoLog(Log); this.logReveal.Invoke(doLog, new object[] { msg }); } else { if (this.logReveal.Items.Count > 20) { this.logReveal.Items.RemoveAt(0); } msg = DateTime.Now.ToLocalTime().ToString() + " " + msg; this.logReveal.Items.Add(msg); } } }
(二)WebSocket4Net实现客户端
1、创建窗口程序,WindowsFormsWebsocketClient
2、添加程序包
工具 -->Nuget包管理 -->管理解决方案的Nuget程序包 -->搜索 WebSocket4Net ,选择WebSocket4Net,点击右侧 安装,等待安装完成,安装完成以后,项目会多出很多引用库,如下
public partial class Form1 : Form { public Form1() { InitializeComponent(); websocketServerTest(); } public static WebSocket webSocket4Net = null; public void websocketServerTest() { FileUtil.getInstance().Log("我是客户端"); webSocket4Net = new WebSocket("ws://127.0.0.1:1234"); webSocket4Net.Opened += WebSocket4Net_Opened; webSocket4Net.Error += websocket_Error; webSocket4Net.Closed += new EventHandler(websocket_Closed); webSocket4Net.MessageReceived += WebSocket4Net_MessageReceived; webSocket4Net.Open(); Thread thread = new Thread(ClientSendMsgToServer); thread.IsBackground = true; thread.Start(); //webSocket4Net.Dispose(); } private void saveBtn_Click(object sender, EventArgs e) { websocketServerTest(); } public void ClientSendMsgToServer() { int i = 1; while (true) { webSocket4Net.Send("love girl" + i++); Thread.Sleep(TimeSpan.FromSeconds(5)); } } private void WebSocket4Net_MessageReceived(object sender, MessageReceivedEventArgs e) { FileUtil.getInstance().Log("服务端回复的数据:" + e.Message); } private void WebSocket4Net_Opened(object sender, EventArgs e) { FileUtil.getInstance().Log("客户端连接成功!发送数据中..."); webSocket4Net.Send("来自客户端,准备发送数据!"); } private void websocket_Error(object sender, EventArgs e) { FileUtil.getInstance().Log("WebSocket错误"); Thread.Sleep(5000); if (webSocket4Net.State!= WebSocketState.Open&&webSocket4Net.State!=WebSocketState.Connecting) { websocketServerTest(); } } private void websocket_Closed(object sender, EventArgs e) { FileUtil.getInstance().Log("WebSocket已关闭"); Thread.Sleep(5000); if (webSocket4Net.State != WebSocketState.Open && webSocket4Net.State != WebSocketState.Connecting) { websocketServerTest(); } } }
来源:https://www.zhangshengrong.com/p/nDa9jWORNj/