node ws muiti 多个 path

2018-04-23  本文已影响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);
上一篇下一篇

猜你喜欢

热点阅读