statianzo / Fleck

C# Websocket Implementation
MIT License
2.25k stars 583 forks source link

How to get the websocket connecting a client in server.start() #328

Closed digiponta closed 1 year ago

digiponta commented 1 year ago

Hi,

I would like to know how to get the websocket connecting a client in server.start(). I know the blow variable, "socket" in the below is not WebSocket. The socket seams to be WebSocketConnection. I would like to inform the websocket used in OnMessage with C#. into the other resident thread for sending data. Especially, I would like to use WebSocket.SendAsync (instead of socket.Send) in the thread. For example, there are not the code, "0x0000" for the end of data by the bug of gzip/C#, so I need to append the data, "0x0000" to gzip-commpressed data by WebSocket.SendAsync in the thread.

Thank you.

   public WebSocketConnection mgsSocket = null;
   public string myMessage = null;

   xxx () {
        server.Start(socket =>
        {
            socket.OnOpen = () => Console.WriteLine("Open!");
            socket.OnClose = () => {
                mgsSocket = null;
                Console.WriteLine("Close!");
            }
            socket.OnMessage = message => {
                  msgSocket = socket;
                  myMessage  = message;
                  // socket.Send(message);
            }
        });
  }

  myThread() {
    while (true) {
       (Preparing gzip data)
       ...
       if ( mgsSocket != null ) {
            ( I would like to know how to get the WebSocket from the WebSocketConnection.socket. )
            WebSocket.SendAsync( gzipData, no_end)
            WebSocket.SendAsync( 0x0000, end of data)
       }
       ...
   }
  }
statianzo commented 1 year ago

Fleck does not use System.Net.WebSockets.WebSocket internally. It runs on top of Socket directly.

Fleck.WebSocketConnection.Send does support a byte array. You would need to send a single array with the extra 0x0000 in one Send call as there's no endOfMessage boolean that can be used.

digiponta commented 1 year ago

Hi,

Thank you for your reply. I will think the prosess for make the byte array appending "0x0000". I hope that Send has the parametar of endOfMessage boolean, in future.

Thank you.