private void AcceptTcpClientCallback(IAsyncResult ar) { TcpListener listenerInstance = (TcpListener)ar.AsyncState; TcpClient client = listenerInstance.EndAcceptTcpClient(ar); Console.WriteLine("WebSocketClient connected from " + client.Client.RemoteEndPoint); WebSocketTcpClient webSocketTcpClient = new WebSocketTcpClient(client, isSecureListener); webSocketTcpClient.OnDidClose += (endpoint, wsClient) => { Console.WriteLine("Removing WebSocketClient ... " + endpoint); if (connectedWebSockets.Contains(webSocketTcpClient)) { connectedWebSockets.Remove(webSocketTcpClient); } }; webSocketTcpClient.OnDidRead += (data) => { OnReceiveBytes?.Invoke(data); }; // TODO: Implement "CONNECTING" and "CONNECTED" states to limit // the number of "CONNECTING" requests from a specific client to "one" connectedWebSockets.Add(webSocketTcpClient); webSocketTcpClient.Start(); // Wait for more clients listenerInstance.BeginAcceptTcpClient(AcceptTcpClientCallback, listenerInstance); }
public void Write(byte[] data, bool asTextFrames = false) { for (int i = 0; i < connectedWebSockets.Count; i++) { WebSocketTcpClient client = connectedWebSockets[i]; if (client.IsReady) { Console.WriteLine("Writing " + data.Length + " bytes"); client.Write(data, asTextFrames); } } }