为什么我无法连接到我的节点服务器?

时间:2017-01-29 13:04:56

标签: javascript node.js sockets io server

我一直在为朋友开发一个网络应用程序。在家里开发/测试时,一切都很好。但是,当我尝试运行它时,在我朋友家里安装了所有模块和所有模块,它不起作用!

除了运行服务器的计算机之外,没有任何设备可以连接和查看网页。我注意到的唯一事情是his IP is 192.168.1.xmine is 192.168.0.x.这会影响服务器的运行方式吗?这是代码:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var fs = require("fs");

function onwin(winner){
    fs.readFile('results.json', 'utf8', (err, data) => {
        if (err) throw err;
        var results = JSON.parse(data);
        console.log(typeof data);
        console.log(typeof results);
        var found = false;
        for (var i in results){
            console.log("I: "+i);
            console.log("Winner: "+winner);
            if (i==winner){
                results[winner] += 1;
                found = true;
            };
        };
        if (found==false){
            results[winner] = 1;
        };
        fs.writeFile('results.json', JSON.stringify(results), 'utf8', (err) => {
            if (err) throw err;
            console.log('It\'s saved!');
            console.log(results);
        });
    });
};

app.get('/', function(req, res){
  res.sendFile(path.join(__dirname, 'index.html'));
});
var players;
players = {p1: "", p2: "", p1score: 0, p2score: 0};
var draw = false;
var clients = 0;
io.on('connection', function(client){
      console.log("Client Connected");
      clients+=1
    client.on('displayname', function(data){
        if (players.p1 == ""){
            players.p1 = data;
            console.log(players.p1 + " is ready as player 1");
        }
        else if (players.p2 == ""){
            players.p2 = data;
            console.log(players.p2 + " is ready as player 2");
            io.emit("ready");
        };
    });
    client.on('mypoint', function(data){
        console.log(data + " scored a point");
        if (data==players.p1){players.p1score+=1;}
        else if (data==players.p2){players.p2score+=1;};
        if (draw==false){
            if ((players.p1score+players.p2score)%2==0){
                io.emit("changeserver");
            };
        };
        if (players.p1score==10){
            if (players.p2score==10){draw = true; io.emit("changeserverdraw");}
        };
        if (draw==false){
            if (players.p1score==11){io.emit("winner", players.p1); console.log(players.p1+ " wins"); onwin(players.p1); players.p1score = 0; players.p2score = 0;}
            else if (players.p2score==11){io.emit("winner", players.p2); console.log(players.p2+ " wins"); onwin(players.p2); players.p1score = 0; players.p2score = 0;};
        }
        else{
            if (players.p1score-players.p2score==2){io.emit("winner", players.p1); console.log(players.p1+ " wins"); onwin(players.p1); players.p1score = 0; players.p2score = 0; draw = false;}
            else if (players.p2score-players.p1score==2){io.emit("winner", players.p2); console.log(players.p2+ " wins"); onwin(players.p2); players.p1score = 0; players.p2score = 0; draw = false;};
        };
        io.emit('pointreturn', players.p1score, players.p2score)
    });
  client.on('disconnect', function(){
      console.log("Client Disconnected");
      clients = clients - 1;
      if (clients<2){players.p1 = ""; players.p2 = ""; players.p1score = 0; players.p2score = 0; io.emit("reset");};
    });
});
http.listen(3000, '0.0.0.0', function() {
    console.log('Listening to port:  ' + 3000);
});

任何帮助表示感谢。

感谢。

1 个答案:

答案 0 :(得分:0)

如果运行该应用程序的计算机可以从中提取页面或以其他方式调用函数,并且网络上的其他计算机不能,it is most likely due to a firewall config on the box or on the network, or else an antivirus app on the box。检查并查看这些设置是什么,并确保运行应用程序的端口未被阻止。