Azure / azure-relay-node

☁️Node.js library for Azure Relay Hybrid Connections
https://docs.microsoft.com/en-us/azure/service-bus-relay/relay-what-is-it
MIT License
12 stars 15 forks source link

Missing error object if send operation fails #12

Closed ddobric closed 7 years ago

ddobric commented 7 years ago

If following code fails, error is missing in 'err' object.

ws.send("HELLO", function(err){
        console.log(err);
     });

'err' is undefined.

dlstucki commented 7 years ago

Will you please provide more details?

jtaubensee commented 7 years ago

Also, is this a continuation of #11?

ddobric commented 7 years ago

It is the same code provided in #11

jtaubensee commented 7 years ago

@ddobric - Can you provide more specific reproduction steps? i.e. what did you do to get the error callback to fire?

dlstucki commented 7 years ago

How do you know your send is failing? 'err' is undefined when the send succeeds. This isn't even code written by the relay team or Microsoft, it's the websocket.js class. If you debug into the listener (launching with "node debug listener.js ..."), stop at the call to 'ws.send', then step-into the send call it takes you to a file like this:

C:\GitHub\azure-relay-node\examples\hyco-ws-simple\node_modules\ws\lib\WebSocket.js:212

"ws" library is the existing websocket library, not written by Microsoft. By WebSocket.js line 212 I see the following comment which states "Optional callback which is executed after the send completes":

/**
 * Sends a piece of data
 *
 * @param {Object} data to be sent to the server
 * @param {Object} Members - mask: boolean, binary: boolean, compress: boolean
 * @param {function} Optional callback which is executed after the send completes
 * @api public
 */

WebSocket.prototype.send = function send(data, options, cb) {

So, in all likelihood your send is completing without error. Here's a working request/reply sample I just worked up:

Listener.js

    var WebSocket = require('hyco-ws')

    var wss = WebSocket.createRelayedServer(
        {
            server : WebSocket.createRelayListenUri(ns, path),
            token: WebSocket.createRelayToken('http://' + ns, keyrule, key)
        },
        function (ws) {
            console.log('connection accepted');

            ws.onmessage = function (event) {                
                console.log("LISTENER RCV " + event.data);

                ws.send("Response from Listener", function(err){
                    if (typeof err === 'undefined') {
                        console.log(" send succeeded");
                    } else {
                        console.log(" send failed " + err);
                    }                    
                });
            };

            ws.on('close', function () {
                console.log('connection closed');
            });      

            ws.on('open', function () {
                console.log('connection open');
            });    
        }
    );

    console.log('listening');

    wss.on('error', function(err) {
        console.log('error' + err);
    });

Sender.js

    var WebSocket = require('hyco-ws')

    var uri = WebSocket.createRelaySendUri(ns, path);
    WebSocket.relayedConnect(
        uri,
        WebSocket.createRelayToken(uri, keyrule, key),
        function(wss) {
            var id = setInterval(function() {
                wss.send('message from Sender', function() { /* ignore errors */ });
            }, 1000);

            wss.onmessage = function(event) {
                console.log('Sender received: ' + event.data);
            };

            console.log('Started client interval. Press any key to stop.');
            wss.on('close', function() {
                console.log('stopping client interval');
                clearInterval(id);
                process.exit();
            });

            process.stdin.setRawMode(true);
            process.stdin.resume();
            process.stdin.on('data', function() {
                wss.close();
            });
        }
    );