isapir / lucee-websocket

Enables server WebSockets for Lucee via JSR-356 compliant servlet containers (e.g. Tomcat 8, Jetty 9.1, etc.)
GNU Lesser General Public License v2.1
17 stars 6 forks source link

How to get all currently connected users #7

Closed Yamaha32088 closed 6 years ago

Yamaha32088 commented 6 years ago

This is more of a question than an issue. What I am looking to do is get the names of all currently connected clients. I have the code below:

var chanId  = arguments.websocket.getPathParameters().channel;
var connMgr = arguments.websocket.getConnectionManager();

var currentSubscribers = ArrayMap(connMgr.getChannel(chanId).getSubscribers().toArray(), function(subscriber) {
    return arguments.subscriber.getSessionScope().user.label;
});

This seems to do what I want because inside the sessionScope is a user struct that contains the name of the user. Is there a better way to accomplish the same thing?

Another thing I was wondering about is that if I put the above code in the (onClose, onUnsubscribe) functions and I have two users connected to a channel and I navigate one user away it will still show the user that navigated away in the array returned. Maybe I am doing something wrong. Thanks in advance for any help and creating such a great extension!

isapir commented 6 years ago

Can you please post your question at https://dev.lucee.org/ so that it may benefit others with similar questions in the future? I will look at it as soon as I can. Thanks!