WebSocketListener
WebSocketListener copied to clipboard
Run WebSocketListener as a window service
The examples use Console application with the following code to start the server: server.Start(); Console.ReadKey(true);
But once I wrap it in a Windows service Console.ReadKey is not available. Any advice how to modify it?
Thanks
Hi,
I added a new sample about using WSL as Windows Service. Let me know if you have any question.
Cheers.
Hi,
My base is the WebSocketEventListener sample. I prefer to use the event approach.
The following method starts the server and exits immediately.
`
protected override void OnStart(string[] args)
{
var endpoint = new IPEndPoint(IPAddress.Any, 4002);
using (var server = new WebSocketEventListener(endpoint, new WebSocketListenerOptions()
{
SubProtocols = new String[] { "test" },
PingTimeout = Timeout.InfiniteTimeSpan,
NegotiationTimeout = TimeSpan.FromSeconds(20),
ParallelNegotiations = 16,
NegotiationQueueCapacity = 256,
//TcpBacklog = 1000,
SendBufferSize = 8192 * 2,
BufferManager = BufferManager.CreateBufferManager((8192 * 2 + 1024) * 1000, 8192 * 2 + 1024)
}))
{
server.OnConnect += (ws) => clientConnect(ws);
server.OnDisconnect += (ws) => OnDisconnect(ws);
//server.OnError += (ws, ex) => Console.WriteLine("Error: " + ex.Message);
server.OnMessage += (ws, msg) => onMessage(ws, msg);
server.Start();
//Console.ReadKey(true);
}
}
`
Thanks
Same thing. On the start method, create a WebSocketListener and start it, and in the stop method, dispose it.