Open jjakob opened 4 years ago
getChannels could be made to work in onClose, as the following works and gets me the channel id:
var channels = Variables.ws.connMgr.getChannels(); // connectionManager
var id = arguments.websocket.getId();
// keys are channel ids, value is the number of subscribers
for (chanId in channels) {
if (channels[chanId] > 0) {
var chan = Variables.ws.connMgr.getChannel(chanId);
if (isNull(chan)){
this.log("getchannel returned null!")
} else {
// we can't for loop over getsubscribers because it's a java.util.HashMap and lucee can't cast it to [collection], so we use java's iterator
var wssetiter = chan.getSubscribers().iterator();
while (wssetiter.hasNext()) {
if (wssetiter.next().getId() == id){
// found our channel id
var myId = chanId;
break;
}
}
// no channel id found (not subscribed to any channels)
var myId = null;
}
}
}
By the way, this is the exception that's thrown when trying to for loop over what getSubscribers returns:
lucee.runtime.exp.CasterException: Can't cast Object type [java.util.HashSet] to a value of type
[collection]. stack trace: lucee.runtime.exp.CasterException: Can't cast Object type [java.util.HashSet] to a value of type [collection]
at lucee.runtime.op.Caster.toCollection(Caster.java:3959)
at lucee.runtime.util.ForEachUtil.forEach(ForEachUtil.java:65)
This is probably a bug too, as getSubscribers is unusable without resorting to Java trickery as in the code above. Code that should work (place it in the above post's code), but throws the above exception:
var wsset = chan.getSubscribers()
for (ws in wsset) {
if (ws.getId() == id){
// found our channel id
var myId = chanId;
break;
}
}
This should be documented in the Websocket API docs. As mentioned in #11, before #11 was fixed, onClose would return an error:
This was due to onClose also calling unsubscribeAll (which is redundant?). It'd also be good to document all methods that can and can't be used from e.g. onClose and all other handlers.