From 5067894babe5b5b579365770e1ebcb947a1bb4d3 Mon Sep 17 00:00:00 2001 From: Ryan Dietrich Date: Wed, 10 Jun 2015 17:59:20 -0500 Subject: [PATCH] Added a simple method to get all the rooms for a single namespace. Added the ability to use the "io" cookie for the sessionId if it is present. --- .../socketio/handler/AuthorizeHandler.java | 21 ++++++++++++++++--- .../socketio/namespace/Namespace.java | 4 ++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java b/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java index 38b04916..9809ab37 100644 --- a/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java +++ b/src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java @@ -98,7 +98,7 @@ public void run() { }, configuration.getFirstDataTimeout(), TimeUnit.MILLISECONDS); super.channelActive(ctx); } - + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { SchedulerKey key = new SchedulerKey(Type.PING_TIMEOUT, ctx.channel()); @@ -159,8 +159,7 @@ private boolean authorize(ChannelHandlerContext ctx, Channel channel, String ori return false; } - // TODO try to get sessionId from cookie - UUID sessionId = UUID.randomUUID(); + UUID sessionId = this.generateOrGetSessionIdFromRequest(headers); List transportValue = params.get("transport"); if (transportValue == null) { @@ -193,6 +192,22 @@ private boolean authorize(ChannelHandlerContext ctx, Channel channel, String ori return true; } + /** + This method will either generate a new random sessionId or will retrieve the value stored + in the "io" cookie. Failures to parse will cause a logging warning to be generated and a + random uuid to be generated instead (same as not passing a cookie in the first place). + */ + private UUID generateOrGetSessionIdFromRequest(Map> headers) { + if ( headers.containsKey("io") && headers.get("io").size() == 1 ) { + try { + return UUID.fromString(headers.get("io").get(0)); + } catch ( IllegalArgumentException iaex ) { + log.warn("Malformed UUID received for session! io=" + headers.get("io")); + } + } + return UUID.randomUUID(); + } + public void connect(UUID sessionId) { SchedulerKey key = new SchedulerKey(Type.PING_TIMEOUT, sessionId); disconnectScheduler.cancel(key); diff --git a/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java b/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java index 95912b4a..f1a958bc 100644 --- a/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java +++ b/src/main/java/com/corundumstudio/socketio/namespace/Namespace.java @@ -310,6 +310,10 @@ public Set getRooms(SocketIOClient client) { return Collections.unmodifiableSet(res); } + public Set getRooms() { + return roomClients.keySet(); + } + public Iterable getRoomClients(String room) { Set sessionIds = roomClients.get(room);