/// <summary> /// Open a web socket server and return the stream of /// its (eventual) connection. /// </summary> public WebSocketStream Start() { server = new TcpListener(endpoint); server.Start(); Console.WriteLine("Server started on " + endpoint.Address + ":" + endpoint.Port + ", awaiting connection..."); Console.WriteLine("You can either use a Grace WebSocket client, " + "or visit http://" + endpoint.Address + ":" + endpoint.Port + " in your web browser."); var client = server.AcceptTcpClient(); Console.WriteLine("Connection received from " + ((IPEndPoint)client.Client.RemoteEndPoint).Address + "."); var stream = client.GetStream(); if (Handshake(stream)) { var wss = new WebSocketStream(stream); return(wss); } return(Next()); }
/// <summary> /// After a disconnection, await a further connection and /// return the stream. /// </summary> public WebSocketStream Next() { Console.WriteLine("Connection closed. Server still open on " + endpoint.Address + ":" + endpoint.Port + ", awaiting connection..."); var client = server.AcceptTcpClient(); Console.WriteLine("Connection received from " + ((IPEndPoint)client.Client.RemoteEndPoint).Address + "."); var stream = client.GetStream(); if (Handshake(stream)) { var wss = new WebSocketStream(stream); return(wss); } return(Next()); }