|
15 | 15 | import java.net.URLConnection; |
16 | 16 | import java.util.Arrays; |
17 | 17 | import java.util.HashMap; |
| 18 | +import java.util.LinkedList; |
18 | 19 | import java.util.List; |
19 | 20 | import java.util.Scanner; |
20 | 21 | import java.util.Timer; |
@@ -55,7 +56,7 @@ class IOConnection { |
55 | 56 | public static final String SOCKET_IO_1 = "/socket.io/1/"; |
56 | 57 |
|
57 | 58 | /** All available connections. */ |
58 | | - private static HashMap<String, IOConnection> connections = new HashMap<String, IOConnection>(); |
| 59 | + private static HashMap<String, List<IOConnection>> connections = new HashMap<String, List<IOConnection>>(); |
59 | 60 |
|
60 | 61 | /** The url for this connection. */ |
61 | 62 | private URL url; |
@@ -234,13 +235,20 @@ private void handshake() { |
234 | 235 | * @return a IOConnection object |
235 | 236 | */ |
236 | 237 | static public IOConnection register(String origin, SocketIO socket) { |
237 | | - IOConnection connection = connections.get(origin); |
238 | | - if (connection == null) { |
239 | | - connection = new IOConnection(origin, socket); |
240 | | - connections.put(origin, connection); |
241 | | - } else { |
242 | | - connection.register(socket); |
| 238 | + List<IOConnection> list = connections.get(origin); |
| 239 | + if(list == null) { |
| 240 | + list = new LinkedList<IOConnection>(); |
| 241 | + connections.put(origin, list); |
243 | 242 | } |
| 243 | + else { |
| 244 | + for(IOConnection connection : list) { |
| 245 | + if(connection.register(socket)) |
| 246 | + return connection; |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + IOConnection connection = new IOConnection(origin, socket); |
| 251 | + list.add(connection); |
244 | 252 | return connection; |
245 | 253 | } |
246 | 254 |
|
@@ -351,20 +359,16 @@ protected void error(SocketIOException e) { |
351 | 359 | * @param socket |
352 | 360 | * the socket to be connected |
353 | 361 | */ |
354 | | - public void register(SocketIO socket) { |
| 362 | + public boolean register(SocketIO socket) { |
355 | 363 | String namespace = socket.getNamespace(); |
356 | 364 | if (sockets.containsKey(namespace)) |
357 | | - socket.getCallback() |
358 | | - .onError( |
359 | | - new SocketIOException( |
360 | | - "Namespace '" |
361 | | - + namespace |
362 | | - + "' is already registered. Do not try to connect twice to the same url.")); |
| 365 | + return false; |
363 | 366 | else { |
364 | 367 | sockets.put(namespace, socket); |
365 | 368 | IOMessage connect = new IOMessage(IOMessage.TYPE_CONNECT, |
366 | 369 | socket.getNamespace(), ""); |
367 | 370 | sendPlain(connect.toString()); |
| 371 | + return true; |
368 | 372 | } |
369 | 373 | } |
370 | 374 |
|
|
0 commit comments