Search code examples
node.jswebsocketosc

How to reconnect websocket connection for OSC messages server in Node.js


I have this server that receives osc messages and sends messages to other clients:

const osc = require('osc');
const WebSocket = require('ws');

const ws = new WebSocket('ws:192.xxx.x.xxx:8000');

var udp = new osc.UDPPort({
    localAddress: "0.0.0.0",
    localPort: 8000, 
});

udp.on('ready', function () {
    console.log('OSC Server is listening');
    console.log(JSON.stringify(udp));
});

// Receive osc message
udp.on('message', function (msg, timeTage, info) {
    console.log('An OSC message received: ', msg);

    // Send exact message to client a
    if(info.address !== '10.4.0.95') {
        udp.send(msg, '10.4.0.95', 9004);
    }

    // send message to other client
    if (msg.address.startsWith('/mov/moviePlayhead') && info.address !== '10.4.0.94') {
    udp.send(msg, '10.4.0.94', 9003)
    }

    else if (msg.address === '/bank/1/trig/3') {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send(JSON.stringify({event:'kaleidoscope', action: 'play', type: '3'}));
        }
    }
    else if (msg.address === '/bank/1/trig/2') {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send(JSON.stringify({event:'kaleidoscope', action: 'play', type: '5'}));
        }
    }
    else if (msg.address === '/bank/1/trig/1') {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send(JSON.stringify({event:'kaleidoscope', action: 'play', type: '7'}));
        }
    }
/*
    else {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send(JSON.stringify({event:'kaleidoscope', action: 'stop'}));
        }
    }
*/

});

udp.open();

On occasion, I get an error message 'Error: connect ECONNREFUSED 192.xxx.x.xxx:8000' that indicates that the connection was not made.

What can I add to the script so that it keeps reconnecting if the connection was dropped and I don't need to keep restarting the server when it does.

Any feedback on the script to make it better is also appreciated. Thank you!


Solution

  • I solved it by doing this:

    const osc = require('osc');
    const WebSocket = require('ws');
    
    let ws = undefined;
    
    function webSocket() {
        ws = new WebSocket('ws:192.xxx.x.xxx:8000');
    
        ws.on('open', function open() {
            console.log('WebSocket connected');
        });
    
        ws.on('close', function close() {
            console.log('WebSocket disconnected, attempting to reconnect...');
            setTimeout(webSocket, 1000);
        });
    
        ws.on('error', function error(err) {
            console.error('WebSocket encountered an error: ', err.message, 'Closing socket');
            ws.close();
        });
    }
    
    webSocket();
    
    var udp = new osc.UDPPort({
        localAddress: "0.0.0.0",
        localPort: 8000, 
    });
    
    udp.on('ready', function () {
        console.log('OSC Server is listening');
        console.log(JSON.stringify(udp));
    });
    
    // Receive osc message
    udp.on('message', function (msg, timeTage, info) {
        console.log('An OSC message received: ', msg);
    
        // Send exact message to client a
        if(info.address !== '10.4.0.95') {
            udp.send(msg, '10.4.0.95', 9004);
        }
    
        // Send message to tablet
        if (msg.address.startsWith('/mov/moviePlayhead') && info.address !== '10.4.0.94') {
        udp.send(msg, '10.4.0.94', 9003)
        }
    
        // Send message to other client
        if (msg.address === '/bank/1/trig/3') {
            if (ws.readyState === WebSocket.OPEN) {
                ws.send(JSON.stringify({event:'kaleidoscope', action: 'play', type: '3'}));
            }
        }
        else if (msg.address === '/bank/1/trig/2') {
            if (ws.readyState === WebSocket.OPEN) {
                ws.send(JSON.stringify({event:'kaleidoscope', action: 'play', type: '5'}));
            }
        }
        else if (msg.address === '/bank/1/trig/1') {
            if (ws.readyState === WebSocket.OPEN) {
                ws.send(JSON.stringify({event:'kaleidoscope', action: 'play', type: '7'}));
            }
        }
    
    });
    
    udp.open();