Sending Json with c# and WebSocket4Net

6.2k Views Asked by At

I am attempting to send the following JSON data to a server via websockets in C#:

    {          
       "method": "ms.remote.control",
        "params": {
            "Cmd": "Click",
            "DataOfCmd": key,
            "Option": "false",
            "TypeOfRemote": "SendRemoteKey"
        }
    }

I am using the following code to send the data to the server:

string json = @"{""method"":""ms.remote.control"",""params"":""{""Cmd"":""Click"",""DataOfCmd"":""KEY_MENU"",""Option"":""false"",""TypeOfRemote"":""SendRemoteKey""}""}";

string message = JsonConvert.SerializeObject(json);

websocketClient.Send(message);

This is the output of the json data after Serialization:

"{\"method\":\"ms.remote.control\",\"params\":\"{\"Cmd\":\"Click\",\"DataOfCmd\":\"KEY_MENU\",\"Option\":\"false\",\"TypeOfRemote\":\"SendRemoteKey\"}\"}"

I'm getting the following response from the server:

Message Received. Server answered:

{"event":"ms.error","data":{"message":"missing method field from message"}}

Am I formatting the json incorrectly? I know that the json data is correct as the message works fine from the python program that I am attempting to port this from.

1

There are 1 best solutions below

0
Bobby Mayfield On BEST ANSWER

I figured it out. The server required the json to be formatted exactly as above. I formatted the json as follows and the command was successful!

 string json = @"
{
            ""method"": ""ms.remote.control"",
            ""params"": {
                             ""Cmd"": ""Click"",
                ""DataOfCmd"": ""KEY_VOLUP"",
                ""Option"": ""false"",
                ""TypeOfRemote"": ""SendRemoteKey""
            }
                    }";