websocket测试地址 http://www.websocket-test.com/
using SuperWebSocket; using System; using System.Collections.Generic; namespace WebsocketServer { // NuGet SuperWebSocketNETServer class WebSocketServerHelper { List<WebSocketSession> sessionList = new List<WebSocketSession>(); public void Start(int port) { WebSocketServer webSocketServer = new WebSocketServer(); webSocketServer.NewSessionConnected += WebSocketServer_NewSessionConnected; webSocketServer.SessionClosed += WebSocketServer_SessionClosed; webSocketServer.NewMessageReceived += WebSocketServer_NewMessageReceived; if (!webSocketServer.Setup(port)) { Console.WriteLine("设置服务监听失败!"); return; } if (!webSocketServer.Start()) { Console.WriteLine("启动服务监听失败!"); return; } else { Console.WriteLine("启动服务监听成功,port=" + port); } } void WebSocketServer_NewSessionConnected(WebSocketSession session) { sessionList.Add(session); Console.WriteLine("ip: " + session.RemoteEndPoint.ToString() + "已经连接"); } void WebSocketServer_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value) { sessionList.Remove(session); Console.WriteLine("ip: " + session.RemoteEndPoint.ToString() + "断开连接"); } void WebSocketServer_NewMessageReceived(WebSocketSession session, string value) { Console.WriteLine(value); session.Send(value); } } }