Skip to main content

Gizzly Websocket how to use google json sending and receiving both from client and server

2 replies [Last post]
charliesimms
Offline
Joined: 2013-01-11
Points: 0

I am trying to send send data using json from client to server and vice versa.

In my socket when I receive the message I do not receive the json data.

Client:
var member ={
"firstName":"Ray",
"lastName":"Villalobos",
"joined":2012
};

ws = new WebSocket("ws://localhost:8080/echo");
ws.onopen = function(evt) { log("socket opened"); ws.send ('Data', member);};

Server:
@Override
public void onMessage(String text)
{
System.out.println("onMessage " + text);

Data data = new Gson().fromJson(text, Data.class);
System.out.println("onMessage " + data);
}

Does any body have any examples of how I can use json and websockets

Cheers
Charlie

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
oleksiys
Offline
Joined: 2006-01-25
Points: 0

Is onMessage(String test) method getting called at all?

WBR,
Alexey.

charliesimms
Offline
Joined: 2013-01-11
Points: 0

I found a solution. I dont know if it is the best solution but from the client I used the following:
var member ={
"firstName":"Ray",
"lastName":"Villalobos",
"joined":2012
};

var dataMember = JSON.stringify(member);

ws.onopen = function(evt) { log("socket opened"); ws.send (dataMember);};

I then just use google json to parse the string:
Data data = new Gson().fromJson(text, Data.class);

If there is a better way please let me know?

Thanks
Charlie