jchristn / WatsonWebsocket

A simple C# async websocket server and client for reliable transmission and receipt of data
MIT License
277 stars 53 forks source link
async message messaging nuget rpc watson web websocket

alt tag

Watson Websocket

NuGet Version NuGet

WatsonWebsocket is the EASIEST and FASTEST way to build client and server applications that rely on messaging using websockets. It's. Really. Easy.

Thanks and Appreciation

Many thanks and much appreciation to those that take the time to make this library better!

@BryanCrotaz @FodderMK @caozero @Danatobob @Data33 @AK5nowman @jjxtra @MartyIX @rajeshdua123 @tersers @MacKey-255 @KRookoo1 @joreg @ilsnk @xbarra @mawkish00 @jlopvet @marco-manfroni-perugiatiming @GiaNTizmO @exergist @ebarale99 @WarstekHUN @Rubidium37 @codengine

Test App

A test project for both client (TestClient) and server (TestServer) are included which will help you understand and exercise the class library.

A test project that spawns a server and client and exchanges messages can be found here: https://github.com/jchristn/watsonwebsockettest

Supported Operating Systems

WatsonWebsocket currently relies on websocket support being present in the underlying operating system. Windows 7 does not support websockets.

SSL

SSL is supported in WatsonWebsocket. The constructors for WatsonWsServer and WatsonWsClient accept a bool indicating whether or not SSL should be enabled. Since websockets, and as a byproduct WatsonWebsocket, use HTTPS, they rely on certificates within the certificate store of your operating system. A test certificate is provided in both the TestClient and TestServer projects which can be used for testing purposes. These should NOT be used in production.

For more information on using SSL certificates, please refer to the wiki.

New in v4.0.x

Server Example

using WatsonWebsocket;

WatsonWsServer server = new WatsonWsServer("[ip]", port, true|false);
server.ClientConnected += ClientConnected;
server.ClientDisconnected += ClientDisconnected;
server.MessageReceived += MessageReceived; 
server.Start();

static void ClientConnected(object sender, ConnectionEventArgs args) 
{
    Console.WriteLine("Client connected: " + args.Client.ToString());
}

static void ClientDisconnected(object sender, DisconnectionEventArgs args) 
{
    Console.WriteLine("Client disconnected: " + args.Client.ToString());
}

static void MessageReceived(object sender, MessageReceivedEventArgs args) 
{ 
    Console.WriteLine("Message received from " + args.Client.ToString() + ": " + Encoding.UTF8.GetString(args.Data));
}

Client Example

using WatsonWebsocket;

WatsonWsClient client = new WatsonWsClient("[server ip]", [server port], true|false);
client.ServerConnected += ServerConnected;
client.ServerDisconnected += ServerDisconnected;
client.MessageReceived += MessageReceived; 
client.Start(); 

static void MessageReceived(object sender, MessageReceivedEventArgs args) 
{
    Console.WriteLine("Message from server: " + Encoding.UTF8.GetString(args.Data));
}

static void ServerConnected(object sender, EventArgs args) 
{
    Console.WriteLine("Server connected");
}

static void ServerDisconnected(object sender, EventArgs args) 
{
    Console.WriteLine("Server disconnected");
}

Client Example using Browser

server = new WatsonWsServer("http://localhost:9000/");
server.Start();
let socket = new WebSocket("ws://localhost:9000/test/");
socket.onopen = function () { console.log("success"); };
socket.onmessage = function (msg) { console.log(msg.data); };
socket.onclose = function () { console.log("closed"); };
// wait a moment
socket.send("Hello, world!");

Accessing from Outside Localhost

When you configure WatsonWebsocket to listen on 127.0.0.1 or localhost, it will only respond to requests received from within the local machine.

To configure access from other nodes outside of localhost, use the following:

Version History

Please refer to CHANGELOG.md for details.