我很难让多人游戏成为多计算机游戏,而不仅仅是多浏览器选项卡。我浏览了 HTML5 中的实时多人游戏教程(构建新游戏,单击 here )。它监听端口 4004,但我无法通过访问 192.168.1.4:4004 从 LAN 的另一台计算机进行连接,即使我可以通过访问 192.168.1.4 与端口 80 上的 Apache Web 服务器连接(此后我在路由器上禁用了 Apache 并转发端口 80,而是转发端口 4004)。我曾经通过访问 my-domain.com 从外部世界连接到 Apache(其 DNS 由我的 ISP 托管)。现在,我无法通过访问 my-domain.com 或 my-domain.com:4004 从 LAN 外部连接到我的教程。本教程使用node.js、socket.io 和express。您可以通过在终端中输入 node app.js 来启动它。您可以通过访问 127.0.0.1:4004192.168.1.4:4004 添加新玩家。以下是 app.js 的内容:

var 
    gameport        = process.env.PORT || 4004, 
    express         = require('express'), 
    UUID            = require('node-uuid'), 
    verbose         = false, 
    app             = express() 
  , http = require('http') 
  , server = http.createServer(app) 
  , io = require('socket.io').listen(server); 
 
/* Express server set up. */ 
 
//The express server handles passing our content to the browser, 
//As well as routing users where they need to go. This example is bare bones 
//and will serve any file the user requests from the root of your web server (where you launch the script from) 
//so keep this in mind - this is not a production script but a development teaching tool. 
 
    //Tell the server to listen for incoming connections 
server.listen( gameport ); 
 
    //Log something so we know that it succeeded. 
console.log('\t :: Express :: Listening on port ' + gameport ); 
 
    //By default, we forward the / path to index.html automatically. 
app.get( '/', function( req, res ){ 
    res.sendfile( __dirname + '/index.html' ); 
}); 
    //This handler will listen for requests on /*, any file from the root of our server. 
    //See expressjs documentation for more info on routing. 
 
app.get( '/*' , function( req, res, next ) { 
 
        //This is the current file they have requested 
    var file = req.params[0]; 
 
        //For debugging, we can track what files are requested. 
    if(verbose) console.log('\t :: Express :: file requested : ' + file); 
 
        //Send the requesting client the file. 
    res.sendfile( __dirname + '/' + file ); 
 
}); 
/* Socket.IO server set up. */ 
 
//Express and socket.io can work together to serve the socket.io client files for you. 
//This way, when the client requests '/socket.io/' files, socket.io determines what the client needs. 
 
    //Create a socket.io instance using our express server 
var sio = io; 
 
    //Configure the socket.io connection settings. 
    //See http://socket.io/ 
sio.configure(function (){ 
 
    sio.set('log level', 0); 
 
    sio.set('authorization', function (handshakeData, callback) { 
      callback(null, true); // error first callback style 
    }); 
 
}); 
    //Enter the game server code. The game server handles 
    //client connections looking for a game, creating games, 
    //leaving games, joining games and ending games when they leave. 
game_server = require('./game.server.js'); 
 
    //Socket.io will call this function when a client connects, 
    //So we can send that client looking for a game to play, 
    //as well as give that client a unique ID to use so we can 
    //maintain the list if players. 
sio.sockets.on('connection', function (client) { 
 
        //Generate a new UUID, looks something like 
        //5b2ca132-64bd-4513-99da-90e838ca47d1 
        //and store this on their socket/connection 
    client.userid = UUID(); 
 
        //tell the player they connected, giving them their id 
    client.emit('onconnected', { id: client.userid } ); 
 
        //now we can find them a game to play with someone. 
        //if no game exists with someone waiting, they create one and wait. 
    game_server.findGame(client); 
 
        //Useful to know when someone connects 
    console.log('\t socket.io:: player ' + client.userid + ' connected'); 
 
        //Now we want to handle some of the messages that clients will send. 
        //They send messages here, and we send them to the game_server to handle. 
    client.on('message', function(m) { 
        game_server.onMessage(client, m); 
    }); //client.on message 
 
        //When this client disconnects, we want to tell the game server 
        //about that as well, so it can remove them from the game they are 
        //in, and make sure the other player knows that they left and so on. 
    client.on('disconnect', function () { 
 
            //Useful to know when soomeone disconnects 
        console.log('\t socket.io:: client disconnected ' + client.userid + ' ' + client.game_id); 
 
            //If the client was in a game, set by game_server.findGame, 
            //we can tell the game server to update that game state. 
        if(client.game && client.game.id) { 
 
            //player leaving a game should destroy that game 
            game_server.endGame(client.game.id, client.userid); 
 
        } //client.game_id 
 
    }); //client.on disconnect 
 
}); //sio.sockets.on connection 

我意识到端口转发和路由器问题可能超出了大多数多人游戏教程的范围。尽管如此,我还是希望有人能在这里帮助我。谢谢。

请您参考如下方法:

根据评论,该问题与防火墙有关。如今,每个操作系统默认都有一些限制性的防火墙设置,因此检查这些设置始终很重要,特别是如果受影响的服务可以从本地主机运行但无法从其他设备访问。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!