Reputation: 1257
i'm working with socket.io (mrniko/netty-socket.io server on java side and socket.io.js on client side) the problem is when i send json object from client to server , after recieving and showing the data , it gives error "Error during data processing" while its entertaining string data fine (i.e. sending & receiving ). Any idea if i'm doing something wrong ?? here is the code (server side)
@OnMessage
public void onMessageRecieved(SocketIOClient client, String data, AckRequest ackRequest){
System.out.println("client is "+client+" data is "+data);
}
here is client side code where i'm sending the data
var socket = io.connect('http://www.example.com:9090',
{
'reconnection delay' : 2000,
'force new connection' : true
});
var data1 = {
user : document.getElementById('t1').value,
pass : document.getElementById('p1').value
};
socket.send(JSON.stringify(data));
i have also tried
socket.json.send(JSON.stringify(data));
its sending and also displaying on server side but when i pass it in any other function for further operations , it gives error "Error during data processing". for parsing i'm using
JsonReader reader = new JsonReader(new StringReader(data));
JsonObject json = new JsonParser().parse(reader).getAsJsonObject();
System.out.println(json.get("value1").toString());
please tell me if i'm going wrong ?
Upvotes: 1
Views: 967
Reputation: 10793
Have you checked an https://github.com/mrniko/netty-socketio-demo project? It uses json message in "chat example".
As for your case. On client try this:
var socket = io.connect('http://www.example.com:9090',
{
'reconnection delay' : 2000,
'force new connection' : true
});
var data1 = {
user : document.getElementById('t1').value,
pass : document.getElementById('p1').value
};
socket.send(data); // you don't need to use JSON.stringify
On the server side: // LoginObj should have a "user" and "pass" fields
server.addJsonObjectListener(LoginObj.class, new DataListener<LoginObj>() {
@Override
public void onData(SocketIOClient client, LoginObj data, AckRequest ackRequest) {
data.getUser();
data.getPass();
}
});
Upvotes: 2