socket.io簡單筆記1

socket.io簡單使用說明

連接

connection事件是連接事件
connection等價於connect
io等價於io.sockets

io.sockets.on('connection', function (so) {
    // 把io看作電話機房,那麼so可以看作是連到電話機房的電話線
});

socket方法

即connection之後的每個電話線

emit用以回答當前的socket,可以理解爲回覆給當前的電話線
send(data)等價於emit(‘message’,data);

socket.emit('message', "this is a test");

發送給其他所有的用戶(除去自己)

socket.broadcast.emit('message', "this is a test");

發送給”某個頻道”的其他所有的用戶(除去自己)
to來選擇頻道
to等價於in

socket.broadcast.to('game').emit('message', 'nice gameµ');

發送給所有的用戶

io.sockets.emit('message', "this is a test");

發送給”某個頻道”的其他所有的用戶

io.sockets.to('game').emit('message', 'cool game');

針對某人發送一個信息

io.sockets.socket(socketid).emit('message', 'for your eyes only');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章