【发布时间】:2014-09-05 00:35:01
【问题描述】:
我如何向动态房间发送消息,以及当服务器收到该消息时,将该消息发送到同一个房间以供其他成员使用?
table_id是房间,会动态设置..
客户:
var table_id = 1; // example, python will give this value
var socket = io('http://localhost:3000');
socket.on('connect', function() {
console.log('Connected');
socket.emit('join', "table_"+table_id);
});
socket.on("table_"+table_id, function(data) {
console.log('New data:', data);
});
$('button').on('click', function(){
// send message to that room
});
服务器:
io.on('connection', function(socket){
socket.on('join', function(table) {
console.log('joined to table '+table);
socket.join(table);
});
// when receive message from particular room, send it back to others in same room
});
【问题讨论】:
标签: sockets websocket socket.io