Ws: Using WS in conjuction with HTTP(S) server using ECMA6 classes crashes the WS-service

Created on 23 Oct 2017  路  1Comment  路  Source: websockets/ws

Description

The Websocket server seems unable to properly digest a HTTP(S) server when using NodeJS class schematics. The same examples with functions in the public namespace work:

https://pastebin.com/3mzy85iv

But once in the new class syntax functionality breaks:
HTTP https://pastebin.com/raw/8AuyrETb
HTTPS https://pastebin.com/JQSVRBBp

Reproducible in:

version:
Node.js version(s): v6.11.4, likely more
OS version(s): Windows 10, Linux Mint 18.2

Expected result:

[#] Initializing server on port 8080...
[#] Webserver started!
[#] Websocket service started!

Actual result:

[#] Initializing services on port 8080...
/home/test/Documents/Projects/x/prototypes/node_modules/ws/lib/WebSocketServer.js:63
      throw new TypeError('missing or invalid options');
      ^

TypeError: missing or invalid options
    at WebSocketServer (/home/test/Documents/Projects/x/prototypes/node_modules/ws/lib/WebSocketServer.js:63:13)
    at Server.initWebsocketServer (/home/test/Documents/Projects/x/prototypes/services.js:21:26)
    at new Server (/home/test/Documents/Projects/x/prototypes/services.js:9:14)
    at Object.<anonymous> (/home/test/Documents/Projects/x/prototypes/services.js:25:16)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Most helpful comment

You should pass an options object to the WebSocketServer constructor:

const wss = new WebSocket.Server({ server: httpServer });

Your first example works because your HttpsServer instance has a server property but technically it is still not correct.

>All comments

You should pass an options object to the WebSocketServer constructor:

const wss = new WebSocket.Server({ server: httpServer });

Your first example works because your HttpsServer instance has a server property but technically it is still not correct.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nodesocket picture nodesocket  路  4Comments

nabeelio picture nabeelio  路  5Comments

NodePing picture NodePing  路  5Comments

cra0kalo picture cra0kalo  路  3Comments

dcflow picture dcflow  路  4Comments