C#(SuperWebSocket)与websocket通信

C#(SuperWebSocket)与websocket通信

客户端代码

点击可以查看一些关于websocket的介绍




    
    







服务器端代码

  SuperWebSocket  继承了Socket框架SuperSocket,是一个WebSocket服务器的网络实现。


class Program
    {
        static void Main(string[] args)
        {
            WebSocketServer server = new WebSocketServer();
            server.NewSessionConnected += server_NewSessionConnected;

            server.NewMessageReceived += server_NewMessageReceived;
            server.SessionClosed += server_SessionClosed;
            try
            {
                server.Setup("127.0.0.1", 40001);//设置端口
                server.Start();//开启监听
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }

        static void server_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
        {
            Console.WriteLine(session.Origin);
        }

        static void server_NewMessageReceived(WebSocketSession session, string value)
        {
            Console.WriteLine(value);
            session.Send(value);
        }

        static void server_NewSessionConnected(WebSocketSession session)
        {
            Console.WriteLine(session.Origin);
        }
    }




你可能感兴趣的:(其他)