我成功地从nodejs服务器向Android客户端发送了ack,但我没有成功做反向。我有这个错误:在Socket.emit
广播时不支持回调Serveur nodejs:
socket.broadcast.to(socketid).emit('message', data, callThis);
//this function is executed when client calls it
function callThis (dataFromClient){
console.log("Call back fired: " + dataFromClient);
}
客户端android:
socket.on("message", new Emitter.Listener() {
@Override
public void call(Object... args) {
Ack ack = (Ack) args[args.length - 1];
ack.call();
JSONObject data = (JSONObject) args[0];
.....
}
}
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
基本上支持@Xeoncross的答案。建立连接后,只需将套接字保存到地图中即可,如下所示:
this.connections = new Map<string, SocketIO.Socket>()
this.server.on("connection", (socket: SocketIO.Socket) => {
this.connections.set(socket.id, socket)
})
然后使用循环单独发送所有用户
public broadcast(msg: string) {
for(const socket of this.connections.values()) {
socket.emit("block", msg, (confirm: string) => {
console.log("confirmation msg: ", confirm)
})
}
}