2017-03-25 65 views
1

我创建了两个构成小型多人游戏的JavaScript文件(服务器文件和客户端文件)。当我使用node server.js在本地运行服务器文件时,它开始运行。然后,我可以用node client.js运行客户端文件来玩游戏,并且一切正常。但是,当我尝试在Linux实例(谷歌云上的计算引擎)上实况托管游戏时,我无法从客户端连接到它。我正在为nodeJS中的websockets使用ws库。无法连接到Google云计算引擎上的NodeJS WS服务器

下面是代码:

server.js

const WebSocket = require('ws'); 
const util = require('util'); 

const wss = new WebSocket.Server({ port: 8889 }); 

var magicNum = Math.ceil(Math.random() * 5); 
var champion = "James"; 
var connections = []; 

console.log('server listening...'); 

wss.on('connection', function connection(ws) { 

    connections.push({ 
     ws: ws, 
     name: "", 
     score: 0 
    }); 

    ws.on('message', function incoming(message, flags) { 

     var currentPlayer; 

     for (var i = 0; i < connections.length; i++) { 
      if (connections[i].ws === ws) { 
       currentPlayer = connections[i]; 
      } 
     } 

     var response; 
     var obj = JSON.parse(message); 

     if (parseInt(obj.guess) === magicNum) { 

      currentPlayer.score++; 
      response = "HOLY COW! YOU GUESSED THE MAGIC NUMBER!!!" + 
       "I now declare you, " + obj.name + ", the new champion! (You have correctly guessed (" 
       + currentPlayer.score + ") numbers)"; 
      champion = obj.name; 
      magicNum = Math.ceil(Math.random() * 5); 

      ws.send(JSON.stringify({ response: response, prompt: true })); 

      for (var i = 0; i < connections.length; i++) { 
       if (connections[i].ws !== ws) { 
        response = "Uh oh, too slow! " + obj.name + " correctly guessed the number and is the new champion" 
         + " with " + currentPlayer.score + " correct guesses!"; 
        connections[i].ws.send(JSON.stringify({ response: response, prompt: false })) 
       } 
      } 

     } else { 
      response = "Sorry, that's not the magic number. The champion is still " + champion + "."; 
      ws.send(JSON.stringify({ response: response, prompt: true })); 
     } 

    }); 

    ws.on('close', function (a, b) { 

     for (var i = 0; i < connections.length; i++) { 
      if (connections[i].ws === ws) { 
       connections.pop(i); 
       console.log('removing connection from array'); 
      } 
     } 
     console.log('current connections now: ' + connections.length); 
    }) 
}); 

client.js

const WebSocket = require('ws'); 
var prompt = require('prompt'); 

//const ws = new WebSocket('ws://localhost:8889'); 
const ws = new WebSocket('ws://35.185.74.130:8889'); 

var clientName = ''; 

function beginPromptForGuess() { 

    var numberSchema = { 
     properties: { 
      number: { 
       description: 'Please guess an integer between 1 and 20', 
       pattern: /^\d+$/, 
       message: 'please enter an integer.', 
       required: true 
      } 
     } 
    }; 

    console.log('prompt is: ' + prompt.stopped + " " + !prompt.started) 

    if (prompt.stopped || !prompt.started) { 
     console.log('starting prompt'); 

     prompt.get(numberSchema, function (err, result) { 
      if (result) { 
       console.log('got number'); 
       ws.send(JSON.stringify({ name: clientName, guess: result.number })); 

      } 
     }) 
    } 
} 

ws.on('open', function open() { 

    setTimeout(function() { 
     console.log('Hey, let\'s play a game!'); 

     var nameSchema = { 
      properties: { 
       name: { 
        description: 'First, what is your name?', 
        pattern: /^[a-zA-Z\s\-]+$/, 
        message: 'Name must be only letters, spaces, or dashes', 
        required: true 
       } 
      } 
     }; 

     prompt.get(nameSchema, function (err, result) { 
      if (result) { 
       clientName = result.name; 
       console.log('Hi there, ' + result.name + '!'); 
       console.log('Let\'s play!'); 
       beginPromptForGuess(); 
      } 
     }); 
    }, 500); 

}); 

ws.on('message', function incoming(data, flags) { 
    var obj = JSON.parse(data); 
    console.log('Server says: ' + obj.response); 

    if (obj.prompt) { 
     beginPromptForGuess(); 
    } 
}); 

我创建的Linux实例和所使用的SSH后 “在浏览器窗口中打开” 选项在home/mrjim目录中的实例中打开一个shell。该实例已有节点,因此我将服务器代码粘贴到文件中,运行npm init以创建package.json文件,安装了ws库。然后我跑node server.js,它似乎是工作:

enter image description here

然而,当我尝试从外壳挂了一段时间的客户端连接到它,并给这个输出:

events.js:160 
     throw er; // Unhandled 'error' event 
    ^

Error: connect ECONNREFUSED 35.185.74.130:80 
    at Object.exports._errnoException (util.js:1026:11) 
    at exports._exceptionWithHostPort (util.js:1049:20) 
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1085:14) 

我在服务器文件的位置或我如何写ip /端口有问题吗?谢谢。

而且,这里都为Linux实例我的谷歌云配置: enter image description here

我也曾在防火墙规则开放的端口,但我仍然无法从客户端连接:

enter image description here

+1

看起来像你的IP范围是你的内部IP。找到你的公共IP地址。 (你可以通过在chrome标签中输入“我的IP是什么”来检查这一点。)在你的IP范围内添加这个。否则,通过提供您的ip为0.0.0.0/0来允许所有请求。它为我工作。请检查。 – Balu

回答

1

在你的谷歌云设置的屏幕截图中,它看起来像你的外部IP设置为* Ephemeral *。要从外部客户端(如本地计算机的命令行界面)访问它,您应该将其更改为统计信息IP。此外,对于多人游戏的后端,我建议使用更安全的SSL端口443而不是8889.

祝你好运!

相关问题