Search code examples
c#jsonwebsocketdatamodelwebsocket-sharp

WebSocketsSharp custom events deserialize


I have to build a functionality where I can send custom event with websockets-sharp.

I made a function MakePacket that creates me a string like this ["draw:drawer:accept","{\"imei\":\"123\"}"] that I can send.

    public string MakePacket(string eventName, string data)
    {
        return JsonConvert.SerializeObject(new[] { eventName, data });
    }

So I want to do the same in the other direction. When there is an incomming event I want to convert this back to eventName and PayLoad.

So I create a data model:

public class PacketModel
{
    public string EventName { get; set; }

    public string PayLoad { get; set; }
}

And I tried to convert this with this function:

    public PacketModel OpenPacket(string data)
    {
        PacketModel packet = JsonConvert.DeserializeObject<PacketModel>(data);

        return packet;
    }

But this isn't working...

Does someone has an idea how I can do this?

Thank you


Solution

  • I create an example, how to work with WebSocket-Sharp and custom events like socket.io.

    I post the example in my git repository:

    Websocket-Sharp Custom Events

    Thanks to @Evk for the help