[英]Communicating using socketio in Android - socket.emit error
I'm trying to communicate to my nodejs application using android as client. 我正在尝试使用android作为客户端与我的nodejs应用程序进行通信。 Currently I'm using https://github.com/Gottox/socket.io-java-client repository as reference.
目前,我正在使用https://github.com/Gottox/socket.io-java-client存储库作为参考。 Tried the examples of repo but no help.
尝试回购示例,但无济于事。
NodeJS-SocketIO Code: NodeJS-SocketIO代码:
socket.on('new user',function(data, callback){
if (nicknames.indexOf(data)!=-1){
callback(false);
} else{
callback(true);
socket.nickname = data;
nicknames.push(socket.nickname);
updateNicknames();
}
});
Android-Java Code: Android-Java代码:
try {
JSONObject json = new JSONObject();
json.putOpt("data", "user1");
socket.emit("new user", json);
} catch (JSONException ex) {
ex.printStackTrace();
}
However, the above code results in TypeError: undefined. 但是,以上代码导致TypeError:undefined。 Please help.
请帮忙。 Thanks in advance!
提前致谢!
You can do away with the JSON object
. 您可以
JSON object
。 You could just do: 您可以这样做:
socket.emit("new user", "user1");
Not so sure about sending a JSON over, but if you wanted to, did you try: 不确定是否要发送JSON,但是如果您愿意,您是否尝试过:
JSONArray array = new JSONArray();
array.put("data", "user1");
socket.emit("new user", array);
Pretty sure JSONs need to have the enclosing brackets. 可以肯定的是,JSON需要带有括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.