node.js webserver和C++游戏服务器

Riv*_*Tam 4 c c++ sockets websocket node.js

这个问题可能过于宽泛,但我认为这是一个不错的问题,我不知道如何处理它.

我目前正在example.com上托管一个网站.我现在正在使用100%node.js这样做.我还主持一个使用的网络HTML5游戏(在game.example.com上)socket.io,这太棒了,但我已经决定我宁愿使用C++(或者,可能是Java)来处理游戏服务器,并且我正计划进行翻译来自JavaScript的服务器逻辑.

我目前最大的问题是我根本不知道如何连接WebSocket.我仍然计划使用node.js提供完整的客户端(HTML和JavaScript),但我希望客户端连接到C++服务器而不是node.js服务器.

我当前连接到服务器的方式只是使用从socket.io获得的套接字io.connect();.我认为这可以保留,我只需要将服务器端的socket从node.js传递给我的C++程序,我完全不知道如何做到这一点.

谁能帮我?

use*_*688 5

假设我理解正确,您希望Node处理常规HTTP请求,但您想将Websocket请求传递给C++服务器吗?尝试在Node中使用代理来获取升级请求:

var http = require('http'),
    httpProxy = require('http-proxy');

//have your c++ server for websockets operating on port 1333
var proxy = new httpProxy.HttpProxy({
  target: {
    host: 'localhost',
    port: 1333
  }
});

var server = http.createServer(function (req, res) {
    //handle normal requests in here
});

server.on('upgrade', function (req, socket, head) {
  // Proxy websocket requests...
  proxy.proxyWebSocketRequest(req, socket, head);
});

server.listen(80);
Run Code Online (Sandbox Code Playgroud)