C # (SuperWebSocket) communicates with websocket,
Client code
Click here to view some introduction to websocket
1 <! DOCTYPE html> 2 Server code
SuperWebSocket inherits the Socket framework SuperSocket, which is the network implementation of a WebSocket server.
1 class Program 2 {3 static void Main (string [] args) 4 {5 WebSocketServer server = new WebSocketServer (); 6 server. newSessionConnected + = server_NewSessionConnected; 7 8 server. newMessageReceived + = server_NewMessageReceived; 9 server. sessionClosed + = server_SessionClosed; 10 try11 {12 server. setup ("127.0.0.1", 40001); // set Port 13 server. start (); // enable listening 14} 15 catch (Exception ex) 16 {17 Console. writeLine (ex. message); 18} 19 Console. readKey (); 20} 21 22 static void server_SessionClosed (WebSocketSession session, SuperSocket. socketBase. closeReason value) 23 {24 Console. writeLine (session. origin); 25} 26 27 static void server_NewMessageReceived (WebSocketSession session, string value) 28 {29 Console. writeLine (value); 30 session. send (value); 31} 32 33 static void server_NewSessionConnected (WebSocketSession session) 34 {35 Console. writeLine (session. origin); 36} 37}
At first, I tried to write a websocket server, but I was troubled. Later I used SuperWebSocket directly because it was really simple and practical. You can directly install it in the NuGet package,
By default, log4net and njson are installed in one step.