zoukankan      html  css  js  c++  java
  • Socket.io发送消息含义

    仅作收藏:转自博客园 若相忆;

    
    // send to current request socket client
    socket.emit('message', "this is a test");
    
    // sending to all clients, include sender
    io.sockets.emit('message', "this is a test");
    
    // sending to all clients except sender
    socket.broadcast.emit('message', "this is a test");
    
    // sending to all clients in 'game' room(channel) except sender
    socket.broadcast.to('game').emit('message', 'nice game');
    
    // sending to all clients in 'game' room(channel), include sender
    io.sockets.in('game').emit('message', 'cool game');
    
    // sending to individual socketid
    io.sockets.socket(socketid).emit('message', 'for your eyes only');
    
    // 进入一个房间
    socket.join('room');
    // 离开一个房间
    socket.leave('room');
    
    

    订阅发布模式

    
    //前端触发订阅/退订事件
    socket.emit('subscribe',{"room" : "room_name"};
    socket.emit('unsubscribe',{"room" : "room_name"};
    
    //后台处理订阅/退订事件
    socket.on('subscribe', function(data) { 
        socket.join(data.room);
    })
    socket.on('unsubscribe', function(data) { 
        socket.leave(data.room);
    })
    

    浏览器的全局属性 window.WebSocket

  • 相关阅读:
    5.2-5.3
    5.1封装
    阅读《构建之法》 5-7章
    做汉堡
    阅读《构建之法》1-5章
    结对 四则运算
    回答
    读后感
    提问*2
    提问1
  • 原文地址:https://www.cnblogs.com/zhaowinter/p/5332717.html
Copyright © 2011-2022 走看看