Saturday 29 April 2017

Create and connect to different websocket paths

We are using the ws library from https://github.com/websockets/ws as our server. Currently we have the server such as this where everyone connects to port 192.68.1.1:8080 and everyone deals within this context:

var WebSocketServer = require('ws').Server;
wss = new WebSocketServer({port: 8080});

wss.on('connection', function (ws) {
    ws.on('message', function (message) {
        console.log('received: %s', message);
        ws.send('We got your message!', function ack(error) {
            // If error is not defined, the send has been completed, otherwise the error
            // object will indicate what failed.
            if(error){
                console.log('Some error occured ' + error);
            }
        });
    });
});

We would like to have different paths available so that users can connect to different paths. E.g

ws://192.68.1.1:8080/space1
ws://192.68.1.1:8080/space2
ws://192.68.1.1:8080/space3

We know socket.io has something similar to this in terms of namespaces. We are looking to implement a feature like this (but due to other modules and architecture of the project we have to use the ws library and can't use socket.io).

Our goal that we are trying to accomplish is to have certain users only be able to communicate with certain users like in a group chat scenario. We want the user to connect to a unique room and be able to chat with all the members there.

Is there a method using this library to accomplish something similar?



via user2924127

No comments:

Post a Comment