var ws = require("nodejs-websocket");
var os = require('os');

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function buildMessage(type, object) {
    var message = {};
    message.type = type;
    message.data = object;
    return JSON.stringify(message)
}

function safeAdd(array, eles) {
    if (typeof eles === "number") {
        if (array.indexOf(eles) < 0) {
            array.push(eles);
        }
    } else {
        eles.forEach(function (ele) {
            if (array.indexOf(ele) < 0) {
                array.push(ele);
            }
        });
    }
}

var server = ws.createServer(function (conn) {
    var code = getRandomInt(100000, 999999);
    conn.sendText(buildMessage("new", code));
    conn.code = code;
    conn.nodes = [];
    conn.master = null;
    conn.on("text", function (str) {
        console.log(str);
        var message = JSON.parse(str);
        if ("conn" === message.type) {
            for (var index in server.connections) {
                var connection = server.connections[index];
                if (message.data == connection.code) {
                    conn.master = connection;
                    safeAdd(connection.nodes, conn.code);
                    conn.sendText(buildMessage("status", {"action": "conn", "code": 200}));
                    connection.sendText(buildMessage("push", ""));
                    break;
                }
            }

        } else if ("pull" === message.type || "play" === message.type) {
            var nodes = [];
            if (conn.master != null) {
                safeAdd(nodes, conn.master.code);
                safeAdd(nodes, conn.master.nodes);
                nodes.splice(nodes.indexOf(conn.code), 1);
            } else {
                safeAdd(nodes, conn.nodes);
            }
            console.log("conn code: " + conn.code + ", nodes: " + nodes);
            server.connections.forEach(function (connection) {
                for (var index in nodes) {
                    if (nodes[index] == connection.code) {
                        connection.sendText(str)
                    }
                }
            })
        }
    });
    conn.on("close", function (code, reason) {
        console.log("Connection closed")
    });
    conn.on("error", function (error) {
        console.log("Connection has error, it will be closed. The reason maybe browser has refresh, details:");
        console.error(error);
    })
}).listen(8001);

var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
        var address = interfaces[k][k2];
        if (address.family === 'IPv4' && !address.internal) {
            addresses.push(address.address);
        }
    }
}

console.log("Server running 8001");
console.log("Air play path: ws://" + addresses[0] + ":8001");
@@ -859,34 +859,8 @@
 ) %7B%0A
-        console.log(str);%0A
     
@@ -925,32 +925,65 @@
 message.type) %7B%0A
+            var success = false;%0A
             for 
@@ -1233,32 +1233,150 @@
 conn.code);%0A    
+                success = true;%0A                    break;%0A                %7D%0A            %7D%0A            if (success) %7B%0A
                 
@@ -1455,36 +1455,32 @@
                 
-    
 connection.sendT
@@ -1526,40 +1526,104 @@
     
-        break;%0A                %7D
+%7D else %7B%0A                conn.sendText(buildMessage(%22status%22, %7B%22action%22: %22conn%22, %22code%22: 404%7D));
 %0A   
@@ -1633,17 +1633,16 @@
       %7D%0A
-%0A
         

Refactor join group