Skip to content

Repository files navigation

JsonWebSocket.Net

Simple .NET Standard library for sending json and bson messages through WebSocket.

As it is built on the .NET Standard platform it can be used to easily build communication channels between Legacy .NET Framework applications and .NET Core plaform apps or Universal Windows Platform apps. You can also use it to build WebSocket server for web browser based or other clients.

NuGet Package

https://www.nuget.org/packages/Henko.JsonWebSocket/

WebSocket server example usage

You can build simple WebSocket servers with a few lines of code both on the .NET Framework platform and .NET Core platform using Asp.Net Core.

usingJsonWebSocket;publicstaticIWebHostBuildWebHost(string[]args)=>WebHost.CreateDefaultBuilder(args).UseWebSockets().Map("/ws", ws =>ws.UseJsonWebSockets(socket =>{Console.WriteLine("[{0}] Client connected.",socket.RemoteEndPoint);socket.OnReceivedBinaryBson+=async(data)=>{Console.WriteLine("[{0}] Received binary Bson: {1}",socket.RemoteEndPoint,data);awaitsocket.SendBinaryBson(data);};socket.OnDisconnected+=()=>{Console.WriteLine("[{0}] Client disconnected.",socket.RemoteEndPoint);};})).UseUrls("http://0.0.0.0:5000/").Build();

WebSocket client example usage

Connect to any WebSocket server from a .Net application easily.

usingJsonWebSocket;staticasyncTaskConnect(stringurl){using(varsocket=newJsonSocketClient()){socket.OnReceived+=(data)=>{Console.WriteLine("Received: {0} {1}",data.Type,data.Data);};awaitsocket.Connect(url);Console.WriteLine("Connected to {0}.",url);awaitTask.WhenAll(socket.StartReceiving(),StartSending(socket));Console.WriteLine("Disconnected.");}}staticasyncTaskStartSending(JsonSocketsocket){while(socket.Connected){dynamicdata=new{name="Sample",time=DateTime.Now};awaitsocket.SendBinaryBson(data);awaitTask.Delay(1000);}}

JsonSocket

This class represents the WebSocket connection in both directions. You can use this to send or receive text, json or binary bson data and to get information about the connection.

boolConnected{get;}HttpContextContext{get;}WebSocketSocket{get;}stringConnectionId{get;}EndPointRemoteEndPoint{get;}TaskStartReceiving(intbufferSize=102400);TaskSendTextJson(objectdata);TaskSendTextString(stringdata);TaskSendBinaryBson(objectdata);TaskSendBinaryData(byte[]data);TaskSendClose();eventAction<ReceivedData> OnReceived;event Action<object> OnReceivedTextJson;event Action<string> OnReceivedTextString;event Action<object> OnReceivedBinaryBson;eventAction<byte[]>OnReceivedBinaryData;eventAction OnReceivedClose;eventAction<Exception> OnReceiveError;event Action OnDisconnected;

About

Simple .NET Standard library for sending and receiving Json and Bson messages through WebSocket.

Resources

Stars

7 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages