WebSocketListener icon indicating copy to clipboard operation
WebSocketListener copied to clipboard

Run WebSocketListener as a window service

Open buhoy opened this issue 9 years ago • 3 comments

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

buhoy avatar Jul 07 '16 14:07 buhoy

Hi,

I added a new sample about using WSL as Windows Service. Let me know if you have any question.

Cheers.

vtortola avatar Jul 07 '16 18:07 vtortola

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

buhoy avatar Jul 12 '16 12:07 buhoy

Same thing. On the start method, create a WebSocketListener and start it, and in the stop method, dispose it.

vtortola avatar Jul 18 '16 23:07 vtortola