美文网首页
node ws muiti 多个 path

node ws muiti 多个 path

作者: 此昵称已被狗抢占 | 来源:发表于2018-04-23 00:51 被阅读0次
    const WebSocket = require('ws');
        let wss = new WebSocket.Server({ port: port });
    
        wss.broadcast = function broadcast(data, path) {
            wss.clients.forEach(function each(client) {
    
                if (client.readyState === WebSocket.OPEN && client.path === path) {
                    //console.log(data);
                    client.send(data);
                }
    
            });
        };
    
    
        wss.addListener('connection', function connection(ws, req) {
            // const ip = req.connection.remoteAddress;;
            const location = url.parse(req.url, true);
            ws.path = location.path;
            //console.log('new connection from ' + location.path);
            // ws.on('message', function incoming(message) {
            //     console.log('received: %s', message);
            // });
    
        });
    
    // 按path发
     wss.broadcast(msg, path);
    

    相关文章

      网友评论

          本文标题:node ws muiti 多个 path

          本文链接:https://www.haomeiwen.com/subject/krzhlftx.html