Create server follow another http or https server #15

Closed
opened 2015-04-16 10:30:11 +00:00 by lichenhao · 6 comments
lichenhao commented 2015-04-16 10:30:11 +00:00 (Migrated from github.com)

RT, like https://www.npmjs.com/package/ws support new WebSocketServer({server: httpServer});

RT, like https://www.npmjs.com/package/ws support new WebSocketServer({server: httpServer});
AMorgaut commented 2019-04-23 10:16:18 +00:00 (Migrated from github.com)

I'm working on a project that is using nodejs-websocket
I'm about to need to add a Web Socket API on the same server / port than an existing REST-like HTTP API

This ticket has been closed without comment
Does it means:

  • it will never be implemented (this way or another) ?
  • it is already implemented (didn't saw it in the doc) ?
  • it is on going through another ticket (which one) ?

Maybe there is another approach available, like:

  • 1st creating the WebSocket Server
  • then getting access to its inner http server and bind row HTTP request listeners to it.

Thanks,

Regards

I'm working on a project that is using nodejs-websocket I'm about to need to add a Web Socket API on the same server / port than an existing REST-like HTTP API This ticket has been closed without comment Does it means: - it will never be implemented (this way or another) ? - it is already implemented (didn't saw it in the doc) ? - it is on going through another ticket (which one) ? Maybe there is another approach available, like: - 1st creating the WebSocket Server - then getting access to its inner http server and bind row HTTP request listeners to it. Thanks, Regards
AMorgaut commented 2019-04-23 11:16:34 +00:00 (Migrated from github.com)

Looks like similar requirement was raised in #3

From the answer on that ticket, one of the limitation would be that this module is directly bound to the tls.Server or net.Server socket level without the http layer

So...
I looked a bit...

Which means that when the nodejs-websocket server is created via createServer() it could accept an already existing http or https server socket, instead of creating a new one, if one if provided as option. Calling instanceof tls.TLSSocket/net.Socket could even work for a more generic usage.

var http = require("http")
var ws = require("nodejs-websocket")

var wsServer;
var httpServer = http.createServer();

httpServer.on('upgrade', function (req, socket, head) {
    wsServer = ws.createServer({ socket: socket, upgradeHead: head }, function (conn) {
    	conn.on("text", function (str) {
	    	conn.sendText(str.toUpperCase()+"!!!")
    	})
	    conn.on("close", function (code, reason) {
		    console.log("Connection closed")
    	})
    });
});

httpServer.listen(8001);

It may worth a try

Updated to allow handling upgrade http handshake

Looks like similar requirement was raised in #3 From the answer on that ticket, one of the limitation would be that this module is directly bound to the [tls.Server](https://nodejs.org/api/tls.html#tls_class_tls_server) or [net.Server](https://nodejs.org/api/net.html#net_class_net_server) socket level without the http layer So... I looked a bit... - the node.js [http.Server](https://nodejs.org/api/http.html#http_class_http_server) inherits from [net.Server](https://nodejs.org/api/net.html#net_class_net_server) - the node.js [https.Server](https://nodejs.org/api/https.html#http_class_http_server) inherits from [tls.Server](https://nodejs.org/api/tls.html#tls_class_tls_server) Which means that when the `nodejs-websocket` server is created via createServer() it could accept an already existing http or https server socket, instead of creating a new one, if one if provided as option. Calling `instanceof` `tls.TLSSocket`/`net.Socket` could even work for a more generic usage. ```js var http = require("http") var ws = require("nodejs-websocket") var wsServer; var httpServer = http.createServer(); httpServer.on('upgrade', function (req, socket, head) { wsServer = ws.createServer({ socket: socket, upgradeHead: head }, function (conn) { conn.on("text", function (str) { conn.sendText(str.toUpperCase()+"!!!") }) conn.on("close", function (code, reason) { console.log("Connection closed") }) }); }); httpServer.listen(8001); ``` It may worth a try > Updated to allow handling upgrade http handshake
AMorgaut commented 2019-04-23 11:20:18 +00:00 (Migrated from github.com)

Didn't tested but the implementation could look like


	var socket = options.socket;
	if (socket && (socket instanceof tls.TLSSocket || socket instanceof net.Socket)) {
		this.socket = options.socket
	} else if (secure) {
		this.socket = tls.createServer(options, onConnection)
	} else {
		this.socket = net.createServer(options, onConnection)
	}

Plus some code to handle the upgrade handshake

Didn't tested but the implementation could look like ```js var socket = options.socket; if (socket && (socket instanceof tls.TLSSocket || socket instanceof net.Socket)) { this.socket = options.socket } else if (secure) { this.socket = tls.createServer(options, onConnection) } else { this.socket = net.createServer(options, onConnection) } ``` Plus some code to handle the upgrade handshake
AMorgaut commented 2019-04-24 06:26:33 +00:00 (Migrated from github.com)

I tried to implement it
I'm almost there
image

Still an initial message to catch

I tried to implement it I'm almost there ![image](https://user-images.githubusercontent.com/49318/56636935-98584900-666a-11e9-92c2-818153e3eb01.png) Still an initial message to catch
lichenhao commented 2019-05-17 08:10:12 +00:00 (Migrated from github.com)

looks good.

looks good.
shrpne commented 2023-08-30 12:05:44 +00:00 (Migrated from github.com)

I've implemented in #66, you can try to use it and check if everything works well

I've implemented in #66, you can try to use it and check if everything works well
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: sitegui/nodejs-websocket#15
No description provided.