Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create server follow another http or https server #15

Closed
lichenhao opened this issue Apr 16, 2015 · 6 comments · May be fixed by #66
Closed

Create server follow another http or https server #15

lichenhao opened this issue Apr 16, 2015 · 6 comments · May be fixed by #66

Comments

@lichenhao
Copy link

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

@sitegui sitegui closed this as completed Sep 8, 2015
@AMorgaut
Copy link

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
Copy link

AMorgaut commented Apr 23, 2019

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

@AMorgaut
Copy link

AMorgaut commented Apr 23, 2019

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

@AMorgaut
Copy link

I tried to implement it
I'm almost there
image

Still an initial message to catch

@lichenhao
Copy link
Author

looks good.

@shrpne
Copy link

shrpne commented Aug 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants