如何解析NODEJS+WEBSOCKET中连接信息?


nodejs代码:


 nodejs


 var wsc = [];
var server = require("http").createServer();
var ws = require('ws');

server.listen(3000);
wss = new ws.Server({
    "server" : server
});
wss.on("connection", function(connection) {
    /* 如何能知道CONNECTION的客户端IP地址?URL等信息? */
    console.log("INFO:输入[" + connection + "]");
    /* 2015/6/3 */
    wsc.push(connection);
    if (wsc.length > 1) {
        data = JSON.stringify({
            "event" : "login"
        });
        connection.send(data);
    }
    connection.on("message", function(message) {
        for ( var int = 0; int < wsc.length; int++) {
            try {
                if (wsc[int] != connection) {
                    wsc[int].send(message);
                }
            } catch (e) {
            }
        }
    });
});

websocket node.js

ck2000 9 years, 7 months ago

Your Answer