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");
});

There are comments on Stack Overflow:

Keltar:

Nowhere in documentation it says SDL_net supports websocket, why do you presume it does? Hostname can’t contain protocol specifier. SDL_net don’t do any protocol handling and gives you raw TCP or UDP. You can connect to websocket by implementing websocket handshake and message wrapping yourself. If your websocket is under TLS - that would be much more problematic and you’ll need something like openssl in the middle. Any reason to use websocket specifically? You can use raw TCP in nodejs.

My answer:

I think I should reformulate my question. I just want to connect. It doesn’t matter what it will be: WebSockets or TCP. I will try to use TCP in Node.js. I’ll come back to my question later when I successfully connect to a free hosting server like glitch.com or render.com Thank you so much for letting me know it’s possible to use TCP in Node.js

Emscripten (which creates JavaScript or WebAssembly code for running in a browser, so is WebSockets only) provides a degree of support using the -s USE_SDL_NET=2 switch.

1 Like

Keltar’s answer:

With emscripten it makes a lot of sense as javascript in web browser can only do websocket and no raw TCP stream. So if SDL_net is properly ported to emscripten I suppose it’ll use websocket there.

My answer:

But I already have a web client in WebGL and JS. My goal is to create an SDL2/GLES2 client for desktop and mobile devices. I’m trying to use the SocketIO C++ client: github.com/socketio/socket.io-client-cpp. I have successfully built this library using MinGW 64-bit.

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 answer is that SDL_net doesn’t support WebSockets for desktop clients. Use socket.io-client-cpp or websocketpp instead.