【发布时间】:2017-04-22 04:43:10
【问题描述】:
我已经使用 socket.io 将 Android 客户端连接到 node.js 服务器,并且我能够向服务器发送消息但在客户端上无法接收。 对于客户端,我会做类似
Log.i("MainActivity: ", "sending message");
final JSONObject sdpObj = new JSONObject();
try {
sdpObj.put("id","presenter");
sdpObj.put("sdpOffer",localSdpOffer.description);
} catch (JSONException e) {
e.printStackTrace();
}
LoginActivity.mSocket.emit("new message", sdpObj);
在服务器上我收到这样的对象:
io.sockets.on('connection',function(socket){
socket.on('new message',function(message){
// some logic
socket.emit('created',object);
然后在客户端:
LoginActivity.mSocket.on("created", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.i( TAG, "message back:received ");
User user = new User();
JSONObject obj = null;
try {
obj = new JSONObject((String) args[0]);
//Log.i(TAG,"SdpAnswer: "+args[0].sdpAnswer+"id "+obj.sdpAnswer);
Log.i(TAG, "Instance of"+args[0].toString());
}
});
}
但由于某种原因,它永远不会收到消息。 任何人都有一些想法为什么会这样?谢谢!
【问题讨论】:
标签: java android node.js sockets socket.io