/// <summary> /// WebSocket Server event handler, fires the event. /// </summary> /// <param name="arg">Arguments containing event arguments.</param> public void OnWebsocketEvent(object sender, WebSocketEventArg arg) { if (arg.isClosed) { WebSocketClientInfo key = null; bool found = false; foreach (var connection in _ListWithConnections) { if (connection.Key.clientId == arg.clientId) { key = connection.Key; found = true; break; } } if (found) { _ListWithConnections.Remove(key); } } WebsocketEvent?.Invoke(this, arg); }
/// <summary> /// Starts a connection given the settings set by caller. /// </summary> /// <param name="client">TcpClient socket handler.</param> /// <param name="stream">NetworkStream stream handler.</param> /// <param name="clientId">Id of client.</param> /// <param name="baseUrl">URL path used by client.</param> public async void StartConnection(TcpClient client, string clientId, string baseUrl) { WebSocketClientInfo info = new WebSocketClientInfo() { client = client, clientId = clientId, clientBaseUrl = baseUrl }; WebSocketServer newServer = new WebSocketServer(info, _BufferSize); newServer.WebSocketServerEvent += OnWebsocketEvent; _ListWithConnections.Add(info, newServer); try { await newServer.StartServerAsync(); } catch (Exception) { WebSocketEventArg args = new WebSocketEventArg() { clientId = clientId, clientBaseUrl = baseUrl, errorMessage = "Client disconnected forcefully", isClosed = true }; OnWebsocketEvent(this, args); } }
public override void SendMessage(NetworkConnection connection, string message) { if (m_Connections.ContainsKey(connection.Id)) { return; } WebSocketClientInfo clientInfo = m_Connections[connection.Id]; clientInfo.Socket.Send(message); }
/// <summary> /// Starts a connection given the settings set by caller. /// </summary> /// <param name="client">TcpClient socket handler.</param> /// <param name="stream">NetworkStream stream handler.</param> /// <param name="clientId">Id of client.</param> /// <param name="baseUrl">URL path used by client.</param> public async void StartConnection(TcpClient client, string clientId, string baseUrl) { WebSocketClientInfo info = new WebSocketClientInfo() { client = client, clientId = clientId, clientBaseUrl = baseUrl }; WebSocketServer newServer = new WebSocketServer(info, _BufferSize); newServer.WebSocketServerEvent += OnWebsocketEvent; _ListWithConnections.Add(info, newServer); await newServer.StartServerAsync(); }
private void OnConnectionOpened(IWebSocketConnection client) { //Lưu vào từ điển string connectionId = NetworkConnection.GenerateId(); WebSocketClientInfo clientInfo = new WebSocketClientInfo(connectionId, client); m_Connections.Add(connectionId, clientInfo); //Ném ra sự kiện RaiseConnecionOpenedEvent( new ConnectionOpenedEventArgs { Connection = new NetworkConnection { Id = connectionId, BelongListener = this } }); }
static void Main(string[] args) { Console.WriteLine("WebSocket Server is running, press a key to stop the server!"); SimpleWebSocketServer websocketServer = new SimpleWebSocketServer(new SimpleWebSocketServerSettings() { bufferSize = 65535, port = 1234 }); WebSocketClientInfo webSocketClientInfo = new WebSocketClientInfo() { clientId = "GENERATE CLIENT ID", clientBaseUrl = "URL CLIENT USED TO CONNECT", client = new System.Net.Sockets.TcpClient() // TCP CLIENT RETREIVED FROM HTTP SERVER }; websocketServer.WebsocketServerEvent += OnWebsocketEvent; websocketServer.StartServer(); _WebsocketServer = websocketServer; Console.ReadKey(); bool stopped = _WebsocketServer.StopAll("Server had enough, RIP server."); if (stopped) { Console.WriteLine("Succesfully closed client :D, press key to close."); Console.ReadKey(); } else { Console.WriteLine("Unsuccesfully closed client :("); Console.ReadKey(); } }
/// <summary> /// Stops a connection with a specific client. /// </summary> /// <param name="clientId">Id of client of which to stop the connection with.</param> /// <returns>True on success.</returns> public async Task <bool> StopClient(string clientId) { bool succes = false; WebSocketClientInfo key = null; foreach (var connection in _ListWithConnections) { if (connection.Key.clientId == clientId) { succes = await connection.Value.StopServerAsync(); key = connection.Key; break; } } if (succes) { _ListWithConnections.Remove(key); } return(succes); }
/// <summary> /// Start a websocket server. /// </summary> /// <param name="bufferSize">Sets the receive buffer size, default = 4096</param> /// <param name="clientInfo">Sets the client information.</param> /// <param name="handler">Sets the websocket handler.</param> public WebSocketServer(WebSocketClientInfo clientInfo, int bufferSize = 4096) { _BufferSize = bufferSize; _ClientInfo = clientInfo; }