osc.js icon indicating copy to clipboard operation
osc.js copied to clipboard

osc and socket.io

Open davigega opened this issue 3 years ago • 3 comments

Hello! I am trying to rewrite the "Bidirectionl UDP <-> Web Browser" example (https://github.com/colinbdclark/osc.js-examples/blob/master/udp-browser/index.js) using the "socket.io" library instead of the "ws" one. The differences are pretty small:

server side

const io = require('socket.io')(server) // being the server an https server

io.on('connection', (socket)=> {
  socket.send("hello");
  var socketPort = new osc.WebSocketPort({socket:socket});
  socketPort.send({address: "/id", args: [{type: "s", value: "something"}]})
  var relay = new osc.Relay(udpPort, socketPort, { })
})

the udpPort is declared exactly as in the example and it is listening: it fires on readyand it receives osc messages. The https communication works like charm using ws

client side

var port = io()
port.on("message", (oscMessage) => {
  console.log(oscMessage);
})

The clients prints the hello message but the socketPort sends an error:

'Can\'t send packets on a closed osc.Port object. Please open (or reopen) this Port by calling open().'

I tried adding socketPort.open() after its declaration, but socketPort.on("ready", ... never fires. I'm sure that this is not a problem of your library, but are you maybe willing to help me? By the way: Great job!

davigega avatar Aug 26 '20 20:08 davigega

Hi @davigega. I'm not a socket.io user, so I don't have a lot of background information to help with here. My best guess from reading your code and looking briefly at the documentation for socket.io is that you're trying to pass an instance of socket io's Socket class to osc.js's WebSocketPort. However, osc.js expects to receive a ws WebSocket object. Unless I'm misunderstanding something, you'll likely need to implement your own custom Port instance that uses socket.io to make this work.

colinbdclark avatar Sep 08 '20 22:09 colinbdclark

Is there any update on this @davigega ? I would also like to use socket.io for OSC communication instead of ws as I am already using it in my app

KonVas avatar Jan 08 '21 06:01 KonVas

@KonVas Unfortunately, this was out of my league. I didn't manage do implement a custom Port instance on my own.

davigega avatar Jan 19 '21 17:01 davigega