1
如何將消息發送到動態會議室,以及當服務器收到該消息時,將該消息發送給其他會員的同一會議室?socket.io動態發送和回覆消息
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
});
謝謝s,這工作正常..但爲什麼我應該在這裏使用命名空間,動態的房間?你能給我舉例命名空間嗎? – 2014-09-05 13:17:37
如果作品,不是,你不能;這是另一種方法。 – ainu 2014-09-05 13:21:33
有一個樣本,不如房間好,但它是另一種情況:https://gist.github.com/ramainen/3c738641fc7fbe3ca6fc – ainu 2014-09-05 13:30:15