pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.23k stars 199 forks source link

TypeError: Invalid WebSocket constructor. Set `options.constructor` #17

Closed LeetCodes closed 7 years ago

LeetCodes commented 7 years ago

code doesnt work at all, just get this error TypeError: Invalid WebSocket constructor. Set options.constructor

tried running all examples, also tried to test but got errors with it, any help is appreciated

LeetCodes commented 7 years ago
const WebSocket = require('reconnecting-websocket');
const ws = new WebSocket('ws://echo.websocket.org', null, {maxRetries: 3});

ws.addEventListener('open', () => {
    ws.send('hello!');
});
ws.onerror = (err) => {
        console.log(err);
};
pladaria commented 7 years ago

Where are you executing your code? Browser (using webpack/browserify) or node.js?

If you are using node.js, it fails because there's no global WebSocket in node.

Have a look at the README (there's a section named "using alternative constructor") or see how tests are implemented (using the module html5-websocket to wrap a ws)