1

I am working on electron project which includes node addon module. In native module, I am using ixWebSocket library to communicate with third party api. After initializing websocket properties, I call start() function to run background thread. Code is running in intel Mac, M1 Mac but not working only on Windows.

In Windows, I can run code in REPL by typing one by one line in console. For ex in cmd:

node
>const addon = require("bindings")("addon")
>addon.startSocket()

Above works. But it didn't work when I call it in electron app main.js.

const { app, BrowserWindow } = require('electron')
// include the Node.js 'path' module at the top of your file
const path = require('path')
let addon = require("bindings")("addon");

const createWindow = () => {
    const win = new BrowserWindow({
        width: 400,
        height: 100,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js')
          }
    })
  try 
    {
        addon.startSocket();
    }
    catch(e){
        console.log(e);
    }
    win.loadFile('index.html')
}

app.whenReady().then(() => {
    createWindow()
    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
      })
})


app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})

This is my C++ code.

#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXUserAgent.h>
#include <iostream>

Napi::Value startSocket(const Napi::CallbackInfo &info)
{
    // Required on Windows
    ix::initNetSystem();

    // Our websocket object
    ix::WebSocket webSocket;

    // Connect to a server with encryption
    // See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configuration
    std::string url("wss://echo.websocket.org");
    webSocket.setUrl(url);

    std::cout << "Connecting to " << url << "..." << std::endl;

    // Setup a callback to be fired (in a background thread, watch out for race conditions !)
    // when a message or an event (open, close, error) is received
    webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
        {
            if (msg->type == ix::WebSocketMessageType::Message)
            {
                std::cout << "received message: " << msg->str << std::endl;
                std::cout << "> " << std::flush;
            }
            else if (msg->type == ix::WebSocketMessageType::Open)
            {
                std::cout << "Connection established" << std::endl;
                std::cout << "> " << std::flush;
            }
            else if (msg->type == ix::WebSocketMessageType::Error)
            {
                // Maybe SSL is not configured properly
                std::cout << "Connection error: " << msg->errorInfo.reason << std::endl;
                std::cout << "> " << std::flush;
            }
        }
    );

    // Now that our callback is setup, we can start our background thread and receive messages
    webSocket.start();

    // Send a message to the server (default to TEXT mode)
    webSocket.send("hello world");

    // Display a prompt
    std::cout << "> " << std::flush;

    std::string text;
    // Read text from the console and send messages in text mode.
    // Exit with Ctrl-D on Unix or Ctrl-Z on Windows.
    while (std::getline(std::cin, text))
    {
        webSocket.send(text);
        std::cout << "> " << std::flush;
    }

    return Napi::String::New(info.Env(), "Success");
}

Napi::Object Init(Napi::Env env, Napi::Object exports)
{
    exports.Set(
        Napi::String::New(env, "startSocket"),
        Napi::Function::New(env, startSocket));

    return exports;
}

NODE_API_MODULE(addon, Init)

The electron main app exited with this error

[2680:0406/164343.960:ERROR:crashpad_client_win.cc(844)] not connected
error Command failed with exit code 4294930435.

My environment is:

  • Node: 16.5.0(also tried in 18.15.0)
  • Electron: ^15.3.1(also tried in 24.0.0)
  • OS: Windows 10 Pro
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • You don't check the return value of send function. – kiner_shah Apr 06 '23 at 09:25
  • Maybe you can use getReadyState() function to first check status of connection before sending. https://machinezone.github.io/IXWebSocket/usage/ – kiner_shah Apr 06 '23 at 09:31
  • The thing is that the process exited before sending data. aka the process is killed after calling webSocket.start() function. I checked it by removing all statements after webSocket.start() function. – Super Coder Apr 06 '23 at 11:07
  • Does it throw Segmentation fault error? Or any abort error? Did you check RAM when you call start? Does the RAM gets full? – kiner_shah Apr 07 '23 at 05:51

0 Answers0