Search code examples
c++node.jswebsocketsdl-2

Is it possible to connect SDL2_net client to Node.js server using WebSockets?


When I start the client:

    const char *host = "ws://localhost";
    IPaddress ip;
    if(SDLNet_ResolveHost(&ip, host, 3000) == -1)
    {
        std::cout << "ER: SDLNet_ResolveHost: " <<
            SDLNet_GetError() << std::endl;
    }

I see this messsage:

ER: SDLNet_ResolveHost:

Is it possible to connect SDL2_net client to Node.js server using WebSockets?

Server:

app.js

const express = require("express");
const http = require("http");
const ws = require("ws");
const path = require("path");

const app = express();
const httpServer = http.createServer(app);

const wss = new ws.Server(
{
    server: httpServer
});

const port = process.env.PORT || 3000;
httpServer.listen(port, () => console.log("Listening at port: " + port));

wss.on("connection", socket =>
{
    console.log("client was connected");
});

Added 7/1/2023

I tried to use socket.io-client-cpp with SDL2 but I am stuck on this error: [error] consume error: websocketpp.processor:9 (Invalid use of reserved bits) It was Dec 26, 2022. I'll leave this information here so that I can continue to look for a solution to the problem of connecting a desktop application in SDL2 to a Node.js server with WebSockets in the future.

The same topic on SDL2 forum: Is it possible to connect SDL2_net client to Node.js server using WebSockets?


Solution

  • The answer is that SDL_net doesn't support WebSockets for desktop clients. Use socket.io-client-cpp or websocketpp instead.