Skip to content

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

@igg

Description

@igg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions