How to create a WebSocket server using SuperWebSocket

后端 未结 2 639
一整个雨季
一整个雨季 2020-12-19 01:40

I am creating an application which needs WebSocket Communication. All I need is a simple WebSocketServer with threading possibilities. I found that SuperWebSocket can satisf

相关标签:
2条回答
  • 2020-12-19 02:17

    SuperWebSocket

    Tutorial for Echo example

    Alchemy

    If you are open to other C# WebSocket server you could use Alchemy. The server implementation is quite straight forward:

    static void Main(string[] args) {
      var aServer = new WSServer(8100, IPAddress.Any) {
          DefaultOnReceive = new OnEventDelegate(OnReceive),
          DefaultOnSend = new OnEventDelegate(OnSend),
          DefaultOnConnect = new OnEventDelegate(OnConnect),
          DefaultOnConnected = new OnEventDelegate(OnConnected),
          DefaultOnDisconnect = new OnEventDelegate(OnDisconnect),
          TimeOut = new TimeSpan(0, 5, 0)
      };
    
      aServer.Start();
    }
    
    static void OnConnected(UserContext aContext) {
      Console.WriteLine("Client Connection From : " + aContext.ClientAddress.ToString());
      // TODO: send data back
    }
    

    As mentioned on their website, they have a simple chat example.

    0 讨论(0)
  • 2020-12-19 02:24
    class Program
    {
        static void Main(string[] args)
        {
            var listener = new TcpListener(IPAddress.Loopback, 8181);
            listener.Start();
            using (var client = listener.AcceptTcpClient())
            using (var stream = client.GetStream())
            using (var reader = new StreamReader(stream))
            using (var writer = new StreamWriter(stream))
            {
                writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
                writer.WriteLine("Upgrade: WebSocket");
                writer.WriteLine("Connection: Upgrade");
                writer.WriteLine("WebSocket-Origin: http://localhost:8080");
                writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
                writer.WriteLine("");
            }
            listener.Stop();
        }
    }
    
    0 讨论(0)
提交回复
热议问题