2

Trying to implement a promises wrapper on top of WebSocket api. And testing on chrome.

function WSConnection() {
    'use strict';
    this.socket = {};
}

WSConnection.prototype.connect = function (url) {
    'use strict';

    return new Promise(function (resolve, reject) {
        var this.socket = new WebSocket(url);

        this.socket.onopen = function () {
            socket.send('hello from the client');
            resolve();
        };

        this.socket.onmessage = function (message) {
            console.log("Received on websocket: " + message);
        };

        socket.onerror = function (error) {
            console.log('WebSocket error: ' + error);
            reject(error);
        };

        socket.onclose = function (event) {
            console.log("Websocket socket closed: " + JSON.stringify(event));
        };
    });
};


WSConnection.prototype.disconnect = function () {
    'use strict';
    console.log("Disconnect request from local app layer");
    this.socket.close();
};

However when I run this, I get exception at

        var this.socket = new WebSocket(url);

With the error

"Pause on promise rejection. TyperError: Cannot set property 'socket' of undefined"

. And on the console I see following error -

"Uncaught (in promise) TypeError: Cannot set property 'socket' of undefined".

What is wrong here? Does "this.socket" referring to 'socket' variable in promises object? If so, how can I access the 'socket' variable in the WSConnection object. Is there a better way to promisify websocket apis on the browser?

sthustfo
  • 1,209
  • 4
  • 19
  • 35
  • Well `this` is undefined in the promise resolver callback. – Bergi Dec 04 '15 at 14:27
  • I got useful info from the [duplicate link](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) provided above. Not sure how to accept it. – sthustfo Dec 07 '15 at 09:56
  • You don't need to accept anything when your question is closed. You should just upvote the helpful answers there. – Bergi Dec 07 '15 at 15:10
  • You may also be interested in [websocket-as-promised](https://github.com/vitalets/websocket-as-promised) - a ready-to-use Promise-based API for Websockets. – vitalets Dec 21 '17 at 09:59

1 Answers1

1

The line with error is in incorrect syntax

this.socket = new WebSocket(url);

It should be this.socket = new WebSocket(url); I suppose

Also in new Promise callback your this will undefined, because it is anonymus callback.

You can bind this to callback or ES2015 arrow functions (more details here)

Example with arrow functions:

function WSConnection() {
    'use strict';
    this.socket = {};
}

WSConnection.prototype.connect = function (url) {
    'use strict';

    return new Promise((resolve, reject) => {
        this.socket = new WebSocket(url);

        this.socket.onopen = function () {
            socket.send('hello from the client');
            resolve();
        };

        this.socket.onmessage = function (message) {
            console.log("Received on websocket: " + message);
        };

        socket.onerror = function (error) {
            console.log('WebSocket error: ' + error);
            reject(error);
        };

        socket.onclose = function (event) {
            console.log("Websocket socket closed: " + JSON.stringify(event));
        };
    });
};


WSConnection.prototype.disconnect = function () {
    'use strict';
    console.log("Disconnect request from local app layer");
    this.socket.close();
};
Community
  • 1
  • 1
Andrey
  • 4,020
  • 21
  • 35