Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MMO WebSocket Server: Node.js or C++?

I have been thinking of making a real-time game with WebSockets for the web. I know how to use Node.js, and it is tempting to make it on there. But everywhere I look, C++ seems to be the popular server language because of its speed.

Should I give making it in Node.js a go, and worry about C++ later, or should I learn C++ now and make it in there from scratch?

like image 615
Luke Avatar asked Apr 25 '16 02:04

Luke


People also ask

Is node js good for WebSockets?

Node. js can maintain many hundreds of WebSockets connections simultaneously. WebSockets on the server can become complicated as the connection upgrade from HTTP to WebSockets requires handling. This is why developers commonly use a library to manage this for them.

Which language is best for WebSocket?

A WebSocket server can be written in any server-side programming language that is capable of Berkeley sockets, such as C(++), Python, PHP, or server-side JavaScript.

Is WebSocket heavy on server?

One HTTP request and response took a total of 282 bytes while the request and response websocket frames weighed in at a total of 54 bytes (31 bytes for the request message and 24 bytes for the response). This difference will be less significant for larger payloads however since the HTTP header size doesn't change.

Is WebSocket backend or frontend?

Using WebSockets to connect a backend with a frontend enables you to form long-lasting, full duplex connections for continuously streaming data. For the backend, the essential steps are: Import the socket.io library, create an node HttpServer instance, and use this instance to create a Socket.IO instance.


1 Answers

If you do decide to go the C++ route (and that does offer the best performance of any language), there's this great open source Websocket library that does all the heavy lifting for you. Its header-only and uses just boost. It comes with example code and documentation: http://vinniefalco.github.io/

Here's a complete program that sends a message to the echo server:

#include <beast/websocket.hpp>
#include <beast/buffers_debug.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <string>

int main()
{
    // Normal boost::asio setup
    std::string const host = "echo.websocket.org";
    boost::asio::io_service ios;
    boost::asio::ip::tcp::resolver r(ios);
    boost::asio::ip::tcp::socket sock(ios);
    boost::asio::connect(sock,
        r.resolve(boost::asio::ip::tcp::resolver::query{host, "80"}));

    using namespace beast::websocket;

    // WebSocket connect and send message using beast
    stream<boost::asio::ip::tcp::socket&> ws(sock);
    ws.handshake(host, "/");
    ws.write(boost::asio::buffer("Hello, world!"));

    // Receive WebSocket message, print and close using beast
    beast::streambuf sb;
    opcode op;
    ws.read(op, sb);
    ws.close(close_code::normal);
    std::cout <<
        beast::debug::buffers_to_string(sb.data()) << "\n";
}
like image 116
Vinnie Falco Avatar answered Sep 30 '22 04:09

Vinnie Falco