Node.js web server and C ++ game server

This question may be too broad, but I think it is a worthy question, and I'm not sure how to deal with it.

I currently have a site on example.com. I am doing this using 100% node.js at the moment. I also host an HTML5 network game (at game.example.com) that uses socket.io , which is fantastic, but I decided it was better to handle the game server using C ++ (or maybe Java) and am planning on translating server logic from JavaScript.

My biggest problem at the moment is that I just don’t know how I will connect the WebSocket. I still plan to serve the full client (HTML and JavaScript) using node.js, but I would like the client to connect to the C ++ server, and not to the node.js server.

The way I'm connecting to the server now just uses the socket obtained from socket.io io.connect(); . I think this can stay, I just need to pass the server-side socket from node.js to my C ++ program, and I absolutely don't know how to do this.

Can anybody help me?

+4
source share
2 answers

Assuming I understand you correctly, you want Node to handle regular HTTP requests, but you want to transfer Websocket requests to your C ++ server? Try using a proxy server in Node for update 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); 
+5
source

Firstly, it is possible to connect your clients directly to your C ++ server. For example, if your socket.io socket is a web site, you can use http://libwebsockets.org (the C ++ web socket library for the server side).

Otherwise, you can let your clients connect socket.io to your node.js server and establish some simple connection between your C ++ server and node.js. Do not try to β€œcope with socket.io on the C ++ server”: just the C ++ server and node.js server exchange messages, back and forth, about player states: C ++ will process the logic and node.js will Actually send and receive. You can do this, for example, using a simple TCP socket.

+2
source

Source: https://habr.com/ru/post/1497720/


All Articles