colinbdclark / osc.js

An Open Sound Control (OSC) library for JavaScript that works in both the browser and Node.js
GNU General Public License v2.0
774 stars 117 forks source link

Error: send EHOSTUNREACH [ip]:[host] at doSend (node:dgram:705:16) #212

Closed jellohouse closed 6 months ago

jellohouse commented 6 months ago

Right now there is no way to properly handle send message errors.

I tried to send an OSC message to a server which i know is down/not working (192.168.2.39:7000) but i would like to still handle the error myself...

Currently my electron app opens a dialog/alert with this error when the message fails to send:

A JavaScript error occurred in the main process

Uncaught Exception:
Error: send EHOSTUNREACH
192.168.2.39:7000
at doSend (node:dgram:705:16) at defaultTriggerAsyncldScope
(node:internal/async_hooks:465:18)
at afterDns (node:dgram:651:5)
at process.processTicksAndRejections
(node:internal/process/ task_queues: 84:21)

This stops my whole app from working until i close the dialog/alert.

I tried doing something like:

try {
  oscOut.send(message);
} catch (e) {
  console.log("error", e)
}

But the error is never caught by this try/catch block...

colinbdclark commented 6 months ago

You can listen for error events, there are some examples in the documentation:

https://github.com/colinbdclark/osc.js?tab=readme-ov-file#handling-errors

jellohouse commented 6 months ago

I tried both adding a listener for error and a try/catch block... neither seems to actually catch this error.

To be clear i'm using this in an Electron app. And the dialog that opens looks like this:

Screenshot 2024-04-05 at 12 31 12 PM

This is a snippet of my code currently:

    var oscPort = new osc.WebSocketPort({
      url: 'ws://127.0.0.1:8081',
      metadata: true,
    });
    oscPort.open();
    oscPort.on('message', function (oscMsg) {
      console.log('An OSC message just arrived!', oscMsg);
    });
    oscPort.on('error', function (err) {
      console.log('ERROR OSC', err); // -- - - - -- - - The error never shows up here...
    });
    oscPort.on('ready', function () {
      console.log('Connected to OSC server. Ready.');
    });

....

function sendMessage(message) {
  try {
    oscPort.send(message);
  } catch (e) {
    console.log("error", e);  // -- - - - -- - - The error never shows up here either... 
  }
}
colinbdclark commented 6 months ago

It looks like you’re opening the port before registering the event; also, are you waiting for the ready event before trying to send a message?

jellohouse commented 6 months ago

Yes I am waiting for the port to be ready. I also just changed the order and open the port after registering the events and still doesn't work.

    var oscPort = new osc.WebSocketPort({
      url: 'ws://127.0.0.1:8081',
      metadata: true,
    });
    oscPort.on('message', function (oscMsg) {
      console.log('An OSC message just arrived!', oscMsg);
    });
    oscPort.on('error', function (err) {
      console.log('ERROR OSC', err); // -- - - - -- - - The error never shows up here...
    });
    oscPort.on('ready', function () {
      console.log('Connected to OSC server. Ready.');
    });
    oscPort.open(); // I open the port here now...

....

function sendMessage(message) {
  try {
    oscPort.send(message);
  } catch (e) {
    console.log("error", e);  // -- - - - -- - - The error never shows up here either... 
  }
}

// ... Assume the port is ready .... // 

// This works normally because there is a software on port 21600 listening to OSC
sendMessage({
      address: '/palette/*/*/deactivate',
      args: [
        {
          type: 's',
          value: '192.168.2.39',
        },
        {
          type: 's',
          value: '21600',
        },
        {
          type: 'f',
          value: 0,
        },
      ],
});

// This does NOT work (AND it should NOT) because there is no software listening on port 7000 currently
// Assume the software that usually listens on port 7000 is quit.
// Now this throws an error, but i'm unable to catch/handle the error by myself without this Electron error dialog opening...
sendMessage({
      address: '/clip/connect',
      args: [
        {
          type: 's',
          value: '192.168.2.39',
        },
        {
          type: 's',
          value: '7000',
        },
        {
          type: 'f',
          value: 0,
        },
      ],
});
colinbdclark commented 6 months ago

Ok, your example code has me very confused now. It looks like you’re sending a different set of IP addresses and port numbers as string arguments to a websocket port. How does this involve an error in osc.js? I must be missing some context here.

jellohouse commented 6 months ago

My bad, the issue was on my side.

I forgot i was sending OSC by WS from my Front End to a local node server which was then sending out the OSC by UDP.

I was using those 2 first args as parameters i was reading in my server side to forward the mssages.

So the error was happening on the server side...

Closing this issue now. Thanks!