1

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!

Dylan Dodds
  • 76
  • 1
  • 9
  • 1
    `I only see a way to send strings` then post your data as json or xml. – L.B Dec 09 '13 at 22:41
  • Well, the lists aren't be stored in files, when a server connects to the master server, it temporarily adds its current existance to a list, then sends it to the clients. As soon as that server disconnects from the master server, the server removes it from the list. It's just simple data being sent back and fourth, using json and xml files to store the data would make the master server MUCH too big – Dylan Dodds Dec 09 '13 at 22:53
  • 1
    json or xml doesn't have to be stored as files. There are simply texts with some standard rules. What you see here is a legal json for ex, just copy& paste and send to the server `{"cmd":"DoSomething","parameters":["p1","p2"]}` – L.B Dec 09 '13 at 23:02
  • I get it, but I tried putting that in my C# code to send to the client: session.Send({"str":"some data"}); But I get syntax errors – Dylan Dodds Dec 09 '13 at 23:08
  • OK, C# syntax requires to escape `"` character in strings. but I quit, Forget my comments. – L.B Dec 09 '13 at 23:08
  • I was asking you how I would go about sending json commands from the server to the client... – Dylan Dodds Dec 09 '13 at 23:11
  • Further to LB's excellent suggestion, did you try `session.Send("{\"str\":\"some data\"}");` - it is the command he suggested, with 'escaped double quotes' (meaning you write `\"` to make sure the `"` in the middle of the string is not interpreted as the end of the string). See [this earlier question](http://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server) and associated answers. I think this is essentially a duplicate of that question (JSON = how you turn "any data" into a string so you can do what you ask). – Floris Dec 10 '13 at 00:03
  • yeah I tried that after his comment, thanks for the reply. The only problem is when I receive it on the server and write the string to the console I get a response such as "[object Object]" – Dylan Dodds Dec 10 '13 at 19:06

1 Answers1

2

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.

Jamie Gould
  • 264
  • 3
  • 6