我想与仅两个人进行私人对话。
在Socket.IO中仅对其使用rooms功能是否可以? 还是有更好的方法在socket.io中进行2人对话?
我正在使用NodeJS作为后端
那是我的客户:
var socket = io.connect('http://ip:port');
socket.emit('subscribe', conversation_id);
socket.emit('send message', {
room: conversation_id,
message: "Some message"
});
socket.on('conversation private post', function(data) {
//display data.message
});
这是我的服务器:
socket.on('subscribe', function(room) {
console.log('joining room', room);
socket.join(room);
});
socket.on('send message', function(data) {
console.log('sending room post', data.room);
socket.broadcast.to(data.room).emit('conversation private post', {
message: data.message
});
});