node ws muiti 多个 path

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)