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

separate servers for sub-protocols (i.e. Sec-WebSocket-Protocol) #40

Open
igg opened this issue Apr 28, 2012 · 1 comment
Open

separate servers for sub-protocols (i.e. Sec-WebSocket-Protocol) #40

igg opened this issue Apr 28, 2012 · 1 comment

Comments

@igg
Copy link

igg commented Apr 28, 2012

The API allows implementing multiple sub-protocols in the socket call back using a switch on socket.ws.protocol to give the different protocols different socket event call-backs. It seems it would be nicer to make the server API look more like the browser's WS API, where the protocol is specified in the connection parameters, and you get different connection objects for different sub-protocols on the same server/port.
So:

var ws = require('websocket.io')
  , one_server = ws.listen(3000, 'one_protocol')
  , another_server = ws.listen(3000, 'another_protocol')

one_server.on('connection', function (socket) {
    socket.on('message', function () { });
    socket.on('close', function () { });
});

another_server.on('connection', function (socket) {
    socket.on('message', function () { });
    socket.on('close', function () { });
});

Instead of:

var ws = require('websocket.io')
  , server = ws.listen(3000)

server.on('connection', function (socket) {
  switch (socket.ws.protocol) {
  case ('one_protocol'):
    socket.on('message', function () { });
    socket.on('close', function () { });
    break;
  case ('another_protocol'):
    socket.on('message', function () { });
    socket.on('close', function () { });
    break;
});

Or maybe there is a better way of doing this that I've missed?
Its a fairly minor and probably mostly aesthetic issue. Still a "sanctioned" way of supporting multiple protocols on the same port should really be included in the examples, since its not obvious from the API. There are few examples of using multiple WS sub-protocols on-line at the moment other than the nice ones in the libwebsockets C library, which does a good job of highlighting their utility.

@download13
Copy link

Is there a consistent way to do this in the current API? I would think that this example would only work with hybi since draft doesn't use ws, does it?

Is there any way to select the subprotocol at all when using draft?
I'm working on an implementation of a WebSocket subprotocol right now and need a way to see the offered subprotocols of a connection and select the subprotocol choice that is sent back to the client.

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

No branches or pull requests

2 participants