0

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?

8Observer8
  • 868
  • 10
  • 17
  • 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. – keltar Dec 25 '22 at 06:42
  • 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 https://glitch.com/ or https://render.com/ Thank you so much for letting me know it's possible to use TCP in Node.js – 8Observer8 Dec 25 '22 at 10:55
  • rtrussell said here https://discourse.libsdl.org/t/is-it-possible-to-connect-sdl2-net-client-to-node-js-server-using-websockets/41320/4 that "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." – 8Observer8 Dec 25 '22 at 13:04
  • 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. – keltar Dec 25 '22 at 14:10
  • 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: https://github.com/socketio/socket.io-client-cpp. I have successfully built this library using MinGW 64-bit. – 8Observer8 Dec 25 '22 at 15:19

1 Answers1

0

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

8Observer8
  • 868
  • 10
  • 17