-
Notifications
You must be signed in to change notification settings - Fork 85
WebSocket Event Listener
vtortola edited this page Jun 17, 2014
·
2 revisions
WebSocketListener can be used in many ways. If you are more familiar with event driven approaches, you can create a very simple WebSocketEventListener
class that allows you to subscribe to events, rather than doing asynchronous awaits. For example, a echo service would look like this:
using (var server = new WebSocketEventListener(new IPEndPoint(IPAddress.Any, 8009)))
{
server.OnConnect += (ws)=> Console.WriteLine("Connection from " + ws.RemoteEndpoint);
server.OnDisconnect += (ws) => Console.WriteLine("Disconnection from " + ws.RemoteEndpoint);
server.OnError += (ws, ex) => Console.WriteLine("Error: " + ex.Message);
server.OnMessage += (ws, msg) =>
{
Console.WriteLine("Message from [" + ws.RemoteEndpoint + "]: " + msg);
ws.WriteStringAsync(msg, CancellationToken.None).Wait();
};
server.Start();
...
}
The WebSocketEventListener
class can be found here.
You can find a working project here.