Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js webserver and C++ game server

This question may be too broad, but I think it's a decent question to ask, and I'm not sure how to handle it.

I'm currently hosting a website at example.com. I'm doing this using 100% node.js, at the moment. I'm also hosting a networked HTML5 game (at game.example.com) that uses socket.io, which is fantastic, but I have decided that I would rather handle the game server using C++ (or, potentially, Java) and am planning on translating the server logic from JavaScript.

My biggest problem at the moment is that I simply don't know how I would connect the WebSocket. I still plan on serving the full client (HTML and JavaScript) using node.js, but I would like the client to connect to the C++ server rather than the node.js server.

The way that I'm currently connecting to the server is simply using a socket gained from socket.io's io.connect();. I think this can remain, I just need to pass the socket on the server-side from node.js to my C++ program, and I have absolutely no idea how to do this.

Can anyone help me?

like image 825
River Tam Avatar asked Feb 15 '23 12:02

River Tam


1 Answers

Assuming I understand you correctly, you want Node to handle regular HTTP requests, but you want to pass Websocket requests to your C++ server? Try using a proxy in Node for the upgrade requests:

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);
like image 100
user2695688 Avatar answered Feb 24 '23 11:02

user2695688