Websocket client connection to a node js server

Hello guys !

I started play with the mongoose library to make a client connection in C to a node js server (use of socket.io to receive websocket connection).

For the client connection i use the websocket-client example on github just for test the connection (https://raw.githubusercontent.com/cesanta/mongoose/master/examples/websocket-client/main.c) between my program and my server.

But i didn’t receive any connection on my server, just a message from the client that says “All connections closed”, any help ?

Platform : Windows 10 (Visual Studio Community 2019)

Code Client :

> #define _WINSOCK_DEPRECATED_NO_WARNINGS
> #define WIN32_LEAN_AND_MEAN
> 
> 
> #include <windows.h>
> //#include <winsock2.h>
> //#include <ws2tcpip.h>
> #include <stdlib.h>
> #include <stdio.h>
> 
> #include <mongoose.h>
> 
> // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
> #pragma comment (lib, "Ws2_32.lib")
> //#pragma comment (lib, "Mswsock.lib")
> //#pragma comment (lib, "AdvApi32.lib")
> //#pragma comment (lib, "kernel32.lib")
> 
> 
> //static const char* s_url = "ws://192.168.1.51:8000/";
> 
> // Print websocket response and signal that we're done
> static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data) {
>     if (ev == MG_EV_ERROR) {
>         // On error, log error message
>         printf("Here");
>         LOG(LL_ERROR, ("%p %s", c->fd, (char*)ev_data));
>     }
>     else if (ev == MG_EV_WS_OPEN) {
>         // When websocket handshake is successful, send message
>         mg_ws_send(c, "hello", 5, WEBSOCKET_OP_TEXT);
>     }
>     else if (ev == MG_EV_WS_MSG) {
>         // When we get echo response, print it
>         struct mg_ws_message* wm = (struct mg_ws_message*)ev_data;
>         printf("GOT ECHO REPLY: [%.*s]\n", (int)wm->data.len, wm->data.ptr);
>     }
> 
>     if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE || ev == MG_EV_WS_MSG) {
>         *(bool*)fn_data = true;  // Signal that we're done
>     }
> }
> 
> int main(void) {
>     struct mg_mgr mgr;        // Event manager
>     bool done = false;        // Event handler flips it to true
>     struct mg_connection* c;  // Client connection
>     mg_mgr_init(&mgr);        // Initialise event manager
>     c = mg_ws_connect(&mgr, "ws://192.168.1.51:8000/", fn, &done, NULL);    // Create client
>     
>     printf("c == %p\n", c);
>    
>     while (c && done == false) {
>         mg_mgr_poll(&mgr, 9000); // Wait for echo 
>         printf("here\n");
>     }
>     if (done != false) {
>         printf("Fail\n");
>     }
>     mg_mgr_free(&mgr);                                   // Deallocate resources
>     return 0;
> }

Code Node Server:

const express = require(‘express’);

const app = express();

const http = require(‘http’);

const server = http.createServer(app);

const PORT = 8000;

const io = require(‘socket.io’)(server);

app.get(’/’, (req, res) => {

res.sendFile(__dirname + ‘/index.html’);

});

io.on(‘connection’, (socket) => {

console.log(‘a user connected’);

});

server.listen(PORT, () => {

console.log(listening on *: ${PORT});

});

Ok i found a solution to my problem i will explain for those who try the same thing

I don’t know why but when i use socket.io i can get access to the server, i try with the basic ws npm lib and it’s works with the ws-client example.

Code server:

    const express = require('express');

    const ws = require('ws');

    const app = express();

    // Set up a headless websocket server that prints any

    // events that come in.

    const wsServer = new ws.Server({ noServer: true });

    wsServer.on('connection', socket => {

      socket.on('message', message => console.log(message));

    });

    // `server` is a vanilla Node.js HTTP server, so use

    // the same ws upgrade process described here:

    // https://www.npmjs.com/package/ws#multiple-servers-sharing-a-single-https-server

    const server = app.listen(8000);

    server.on('upgrade', (request, socket, head) => {

      wsServer.handleUpgrade(request, socket, head, socket => {

        wsServer.emit('connection', socket, request);

      });

    });