Dylan Dodds
Dylan Dodds

Reputation: 76

How do I send a packet of information to a WebSocket?

So I'm trying to send a packet of information from a server to a socket and vice versa. I only see a way to send strings. what I NEED to do is send a variable with a specific header that defines what type of data that variable is. The data could be anything between a chat_Message to a player_move event.

I would also like to send a list of information at once. This list would include 5 variables. 4 strings and an int. and I need to give that a header as well.

The server is in C#.net SuperWebSockets, and the client is in javascript on a web server.

Examples of code would also be nice, thanks in advnaced!

Upvotes: 1

Views: 1228

Answers (1)

Jamie Gould
Jamie Gould

Reputation: 414

Assuming you are going down the route of creating a JSON string to be sent and processed on a JavaScript server, guessing NodeJS? You should look at using a library like JSON.Net on the .Net side.

http://james.newtonking.com/json.

The above will provide examples of how you can convert a C# object or set of variables to a string which can be sent to a javascript server.

Once you are on the JavaScript side refer to the documentation for the server you are using to convert the string into a javascript object again.

Starter would be looking up the 'JSON.parse' function.

Json is not a file format per say, you can store files in Json but that is incidental. Json is a serialisation technique to convert an object and its associated objects and fields into a format appropriate for transmission or storage. This seems to be exactly what you are looking to do.

The other route is to go down is to use POST variables and/or URL encoding which is a more complicated approach to what you are trying to do.

Upvotes: 2

Related Questions