简体   繁体   中英

Why can't I connect to my Node server?

I've been developing a web app for a friend. While developing/testing it at home, everything has been fine. But, as I try running it, at my friend's house with all the modules installed and everything, it doesn't work!

No devices apart from the computer the server is running from can connect and view the webpage. The only thing i have noticed is that his IP is 192.168.1.x and mine is 192.168.0.x. Would this affect how the server runs? Here is the code:

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);
});

Any help appreciated.

Thanks.

If the computer on which the app is running can pull pages from it or otherwise call a function as expected, and the other machines on the network cannot, it is most likely due to a firewall config on the box or on the network, or else an antivirus app on the box . Check and see what those settings are and make sure the port which the app is running on is not blocked by either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM