Skip to content

Commit

Permalink
SocketIO now supports multiple connections to the same namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
tox committed Feb 20, 2012
1 parent d54131f commit 01cea3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
32 changes: 18 additions & 14 deletions src/io/socket/IOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Timer;
Expand Down Expand Up @@ -55,7 +56,7 @@ class IOConnection {
public static final String SOCKET_IO_1 = "/socket.io/1/";

/** All available connections. */
private static HashMap<String, IOConnection> connections = new HashMap<String, IOConnection>();
private static HashMap<String, List<IOConnection>> connections = new HashMap<String, List<IOConnection>>();

/** The url for this connection. */
private URL url;
Expand Down Expand Up @@ -234,13 +235,20 @@ private void handshake() {
* @return a IOConnection object
*/
static public IOConnection register(String origin, SocketIO socket) {
IOConnection connection = connections.get(origin);
if (connection == null) {
connection = new IOConnection(origin, socket);
connections.put(origin, connection);
} else {
connection.register(socket);
List<IOConnection> list = connections.get(origin);
if(list == null) {
list = new LinkedList<IOConnection>();
connections.put(origin, list);
}
else {
for(IOConnection connection : list) {
if(connection.register(socket))
return connection;
}
}

IOConnection connection = new IOConnection(origin, socket);
list.add(connection);
return connection;
}

Expand Down Expand Up @@ -351,20 +359,16 @@ protected void error(SocketIOException e) {
* @param socket
* the socket to be connected
*/
public void register(SocketIO socket) {
public boolean register(SocketIO socket) {
String namespace = socket.getNamespace();
if (sockets.containsKey(namespace))
socket.getCallback()
.onError(
new SocketIOException(
"Namespace '"
+ namespace
+ "' is already registered. Do not try to connect twice to the same url."));
return false;
else {
sockets.put(namespace, socket);
IOMessage connect = new IOMessage(IOMessage.TYPE_CONNECT,
socket.getNamespace(), "");
sendPlain(connect.toString());
return true;
}
}

Expand Down
11 changes: 10 additions & 1 deletion tests/io/socket/TestSocketIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@RunWith(io.socket.RandomBlockJUnit4ClassRunner.class)
public class TestSocketIO implements IOCallback {
private final static String NODE = "/opt/local/bin/node";
private final static String NODE = "C:\\Program Files (x86)\\nodejs\\node.exe";
private static final int PORT = 10214;
private static final int TIMEOUT = 2;
LinkedBlockingQueue<String> events;
Expand Down Expand Up @@ -199,9 +199,18 @@ public void namespaces() throws Exception {

assertEquals("onMessage_string", takeEvent());
assertEquals("ns2", takeArg());

SocketIO ns2_2 = new SocketIO("http://127.0.0.1:" + PORT + "/ns2", this);
assertEquals("onConnect", takeEvent());

assertEquals("onMessage_string", takeEvent());
assertEquals("ns2", takeArg());

ns2_2.disconnect();

ns2.disconnect();
assertEquals("onDisconnect", takeEvent());
assertEquals("onDisconnect", takeEvent());
doClose();
}

Expand Down

0 comments on commit 01cea3b

Please sign in to comment.