Node.js gerçekten ilgilendiğim bir şeydi ve bunu projemden birinde çok oyunculu bir oyun yapmak için kullandım.
io.sockets.in().emit()
ve socket.broadcast.to().emit()
Socket.io'nun Odalarında kullandığımız başlıca iki yayma yöntemidir ( https://github.com/LearnBoost/socket.io/wiki/Rooms ) Odalar, bağlı istemcilerin basit bir şekilde bölümlenmesine izin verir. Bu, olayların bağlı istemci listesinin alt kümelerine gönderilmesine izin verir ve bunları yönetmek için basit bir yöntem sağlar.
Bağlantılı istemci listesinin (oda dediğimiz) alt kümelerini yönetmemize ve ana socket.io işlevleri gibi benzer işlevlere sahip olmamıza io.sockets.emit()
ve socket.broadcast.emit()
.
Her neyse, açıklamak için yorumlarla örnek kodlar vermeye çalışacağım. Yardımcı olup olmadığına bakın;
Socket.io Odaları
i) io.sockets.in (). emit ();
/* Send message to the room1. It broadcasts the data to all
the socket clients which are connected to the room1 */
io.sockets.in('room1').emit('function', {foo:bar});
ii) socket.broadcast.to (). emit ();
io.sockets.on('connection', function (socket) {
socket.on('function', function(data){
/* Broadcast to room1 except the sender. In other word,
It broadcast all the socket clients which are connected
to the room1 except the sender */
socket.broadcast.to('room1').emit('function', {foo:bar});
}
}
Socket.io
i) io.sockets.emit ();
/* Send message to all. It broadcasts the data to all
the socket clients which are connected to the server; */
io.sockets.emit('function', {foo:bar});
ii) socket.broadcast.emit ();
io.sockets.on('connection', function (socket) {
socket.on('function', function(data){
// Broadcast to all the socket clients except the sender
socket.broadcast.emit('function', {foo:bar});
}
}
Şerefe