Skip to content

Commit 23779ae

Browse files
committed
fix conflicts
2 parents 00eaed8 + 9cd260c commit 23779ae

File tree

8 files changed

+129
-55
lines changed

8 files changed

+129
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ target/
66
bin/
77
.externalToolBuilders/
88
.settings/
9+
node_modules/

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: java
2+
jdk:
3+
- openjdk7
4+
- oraclejdk7

pom.xml

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88

99
<repositories>
10+
<repository>
11+
<id>sonatype-oss-public</id>
12+
<url>https://oss.sonatype.org/content/groups/public/</url>
13+
<releases>
14+
<enabled>true</enabled>
15+
</releases>
16+
<snapshots>
17+
<enabled>true</enabled>
18+
</snapshots>
19+
</repository>
1020
<repository>
1121
<releases>
1222
<enabled>true</enabled>
@@ -25,24 +35,22 @@
2535
<dependency>
2636
<groupId>com.google.code.gson</groupId>
2737
<artifactId>gson</artifactId>
28-
<version>2.1</version>
38+
<version>2.2.2</version>
2939
</dependency>
3040

3141
<dependency>
32-
<groupId>org.java_websocket</groupId>
33-
<artifactId>WebSocket</artifactId>
34-
<version>1.0.0-SNAPSHOT</version>
42+
<groupId>org.java-websocket</groupId>
43+
<artifactId>Java-WebSocket</artifactId>
44+
<version>1.3.0-SNAPSHOT</version>
3545
</dependency>
3646

3747
<dependency>
3848
<groupId>junit</groupId>
3949
<artifactId>junit</artifactId>
40-
<version>4.8.1</version>
41-
<type>jar</type>
50+
<version>4.11</version>
4251
<scope>test</scope>
4352
</dependency>
44-
45-
53+
4654
</dependencies>
4755

4856

@@ -88,8 +96,18 @@
8896
<target>1.6</target>
8997
</configuration>
9098
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-surefire-plugin</artifactId>
102+
<version>2.14</version>
103+
<configuration>
104+
<includes>
105+
<include>**/AllTests.java</include>
106+
</includes>
107+
</configuration>
108+
</plugin>
91109

92-
110+
93111
<!-- DEPLOY -->
94112
<plugin>
95113
<groupId>org.apache.maven.plugins</groupId>
@@ -102,4 +120,4 @@
102120
</plugins>
103121
</build>
104122

105-
</project>
123+
</project>

src/main/java/io/socket/IOConnection.java

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public void run() {
213213
public static void setSslContext(SSLContext sslContext) {
214214
IOConnection.sslContext = sslContext;
215215
}
216-
216+
217217
/**
218218
* Get the socket factory used for SSL connections.
219219
*
@@ -488,7 +488,7 @@ private synchronized void resetTimeout() {
488488
if (heartbeatTimeoutTask != null) {
489489
heartbeatTimeoutTask.cancel();
490490
}
491-
if(getState() != STATE_INVALID) {
491+
if (getState() != STATE_INVALID) {
492492
heartbeatTimeoutTask = new HearbeatTimeoutTask();
493493
backgroundTimer.schedule(heartbeatTimeoutTask, closingTimeout
494494
+ heartbeatTimeout);
@@ -521,34 +521,11 @@ private IOCallback findCallback(IOMessage message) throws SocketIOException {
521521
* {@link IOTransport} calls this when a connection is established.
522522
*/
523523
public synchronized void transportConnected() {
524-
setState(STATE_READY);
525524
if (reconnectTask != null) {
526525
reconnectTask.cancel();
527526
reconnectTask = null;
528527
}
529528
resetTimeout();
530-
if (transport.canSendBulk()) {
531-
ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
532-
this.outputBuffer = new ConcurrentLinkedQueue<String>();
533-
try {
534-
// DEBUG
535-
String[] texts = outputBuffer.toArray(new String[outputBuffer
536-
.size()]);
537-
logger.info("Bulk start:");
538-
for (String text : texts) {
539-
logger.info("> " + text);
540-
}
541-
logger.info("Bulk end");
542-
// DEBUG END
543-
transport.sendBulk(texts);
544-
} catch (IOException e) {
545-
this.outputBuffer = outputBuffer;
546-
}
547-
} else {
548-
String text;
549-
while ((text = outputBuffer.poll()) != null)
550-
sendPlain(text);
551-
}
552529
this.keepAliveInQueue = false;
553530
}
554531

@@ -635,6 +612,7 @@ public void transportMessage(String text) {
635612
case IOMessage.TYPE_CONNECT:
636613
try {
637614
if (firstSocket != null && "".equals(message.getEndpoint())) {
615+
setState(STATE_READY);
638616
if (firstSocket.getNamespace().equals("")) {
639617
firstSocket.getCallback().onConnect();
640618
} else {
@@ -643,6 +621,8 @@ public void transportMessage(String text) {
643621
firstSocket.getNamespace(), "");
644622
sendPlain(connect.toString());
645623
}
624+
// should flush after connecting to namespace
625+
flushBuffer();
646626
} else {
647627
findCallback(message).onConnect();
648628
}
@@ -755,6 +735,34 @@ public void transportMessage(String text) {
755735
}
756736
}
757737

738+
/**
739+
* Flushes the buffer data.
740+
*/
741+
private synchronized void flushBuffer() {
742+
if (transport.canSendBulk()) {
743+
ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
744+
this.outputBuffer = new ConcurrentLinkedQueue<String>();
745+
try {
746+
// DEBUG
747+
String[] texts = outputBuffer.toArray(new String[outputBuffer
748+
.size()]);
749+
logger.info("Bulk start:");
750+
for (String text : texts) {
751+
logger.info("> " + text);
752+
}
753+
logger.info("Bulk end");
754+
// DEBUG END
755+
transport.sendBulk(texts);
756+
} catch (IOException e) {
757+
this.outputBuffer = outputBuffer;
758+
}
759+
} else {
760+
String text;
761+
while ((text = outputBuffer.poll()) != null)
762+
sendPlain(text);
763+
}
764+
}
765+
758766
/**
759767
* forces a reconnect. This had become useful on some android devices which
760768
* do not shut down TCP-connections when switching from HSDPA to Wifi
@@ -827,26 +835,26 @@ public void send(SocketIO socket, IOAcknowledge ack, JsonElement json) {
827835
*/
828836
public void emit(SocketIO socket, String event, IOAcknowledge ack, Object... args) {
829837
try {
830-
831838
JsonArray jarray = new JsonArray();
832-
833-
for ( Object arg : args ) {
834-
if ( arg instanceof String ) {
835-
jarray.add( new JsonParser().parse( (String) arg ).getAsJsonObject() );
839+
840+
for (Object arg : args) {
841+
if (arg instanceof JsonElement) {
842+
jarray.add((JsonElement)arg);
843+
} else if (arg instanceof String) {
844+
jarray.add(new JsonParser().parse((String)arg));
836845
} else {
837-
error( new SocketIOException("a non-string message received: " + arg.toString() ) );
846+
error(new SocketIOException("a non-json message received: " + arg.toString()));
838847
}
839848
}
840-
841-
849+
842850
JsonObject jobj = new JsonObject();
843851
jobj.add("name", new JsonPrimitive(event));
844852
jobj.add("args", jarray);
845-
853+
846854
IOMessage message = new IOMessage(IOMessage.TYPE_EVENT, socket.getNamespace(), jobj.toString());
847855
synthesizeAck(message, ack);
848856
sendPlain(message.toString());
849-
857+
850858
} catch (JsonParseException e) {
851859
error(new SocketIOException("Error while emitting an event. Make sure you only try to send arguments, which can be serialized into JSON."));
852860
}

src/main/java/io/socket/WebsocketTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ public void onError(Exception ex) {
9797
// TODO Auto-generated method stub
9898

9999
}
100-
}
100+
}

src/test/java/io/socket/AbstractTestSocketIO.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.socket.testutils.MutateProxy;
1515

1616
import java.io.BufferedReader;
17+
import java.io.File;
1718
import java.io.IOException;
1819
import java.io.InputStreamReader;
1920
import java.util.Arrays;
@@ -23,6 +24,7 @@
2324
import org.junit.After;
2425
import org.junit.AfterClass;
2526
import org.junit.Before;
27+
import org.junit.BeforeClass;
2628
import org.junit.Test;
2729
import org.junit.runner.RunWith;
2830

@@ -40,7 +42,10 @@ public abstract class AbstractTestSocketIO implements IOCallback {
4042
private static final String REQUEST_ACKNOWLEDGE = "requestAcknowledge";
4143

4244
/** The Constant to the node executable */
43-
private final static String NODE = "/usr/local/bin/node";
45+
private final static String NODE = "node";
46+
private final static String NPM = "npm";
47+
48+
private final static File RESOURCES_DIR = new File("./src/test/resources");
4449

4550
/** The port of this test, randomly choosed */
4651
private int port = -1;
@@ -74,6 +79,21 @@ public abstract class AbstractTestSocketIO implements IOCallback {
7479
/** The transport of this test */
7580
static protected String transport = null;
7681

82+
/**
83+
* Set up before class.
84+
*
85+
* @throws Exception
86+
* the exception
87+
*
88+
* TODO: Install dependencies before all the tests run
89+
*/
90+
@BeforeClass
91+
public static void npmInstall() throws Exception {
92+
System.out.println("Installing socket.io ...");
93+
Runtime.getRuntime().exec(new String[] { NPM, "install" },
94+
null, RESOURCES_DIR).waitFor();
95+
}
96+
7797
/**
7898
* Tear down after class.
7999
*
@@ -98,10 +118,11 @@ public void setUp() throws Exception {
98118
events = new LinkedBlockingQueue<String>();
99119
outputs = new LinkedBlockingQueue<String>();
100120
args = new LinkedBlockingQueue<Object>();
121+
101122
System.out.println("Connect with " + transport);
102123
node = Runtime.getRuntime().exec(
103-
new String[] { NODE, "./tests/io/socket/testutils/socketio.js",
104-
"" + getPort(), transport });
124+
new String[] { NODE, "socketio.js", "" + getPort(), transport },
125+
null, RESOURCES_DIR);
105126
proxy = new MutateProxy(getPort() + 1, getPort());
106127
proxy.start();
107128

@@ -254,7 +275,7 @@ public void emitAndOn() throws Exception {
254275
String str = "TESTSTRING";
255276
socket.emit("echo", str);
256277
assertEquals("Test String", "on", takeEvent());
257-
assertEquals(str, takeArg());
278+
assertEquals(str, ((JsonElement)takeArg()).getAsString());
258279

259280
JsonObject obj = new JsonParser().parse("{'foo':'bar'}").getAsJsonObject();
260281
socket.emit("echo", obj);
@@ -361,11 +382,11 @@ public void ack(JsonElement... args) {
361382
}
362383
}, "TESTSTRING");
363384
assertEquals("ack", takeEvent());
364-
assertEquals("TESTSTRING", takeArg());
385+
assertEquals("TESTSTRING", ((JsonElement)takeArg()).getAsString());
365386

366387
socket.emit(REQUEST_ACKNOWLEDGE, "TESTSTRING");
367388
assertEquals("on", takeEvent());
368-
assertEquals("TESTSTRING", takeArg());
389+
assertEquals("TESTSTRING", ((JsonElement)takeArg()).getAsString());
369390
assertEquals("ACKNOWLEDGE:TESTSTRING", takeLine());
370391
doClose();
371392
}
@@ -384,12 +405,27 @@ public void reconnectInvalidated() throws Exception {
384405
@Test(timeout = TIMEOUT)
385406
public void sendUtf8() throws Exception {
386407
doConnect();
387-
socket.emit("fooo", "\uD83C\uDF84");
388-
socket.emit("fooo", "🎄");
389-
assertEquals("on", takeEvent());
408+
String[] strings = new String[] {"\uD83C\uDF84", "🎄"};
409+
for (String str : strings) {
410+
socket.emit("echo", str);
411+
assertEquals("on", takeEvent());
412+
assertEquals(str, ((JsonElement)takeArg()).getAsString());
413+
}
390414
doClose();
391415
}
392416

417+
@Test(timeout = TIMEOUT)
418+
public void bufferMessages() throws Exception {
419+
socket = new SocketIO("http://127.0.0.1:" + getProxyPort() + "/main", this);
420+
String str = "TESTSTRING";
421+
// sends before the connection is established
422+
socket.emit("echo", str);
423+
assertEquals("onConnect", takeEvent());
424+
assertEquals("on", takeEvent());
425+
assertEquals(str, ((JsonElement)takeArg()).getAsString());
426+
doClose();
427+
}
428+
393429
// END TESTS
394430

395431
/**

src/test/resources/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "socket.io-java-client-test",
3+
"version": "0.0.0",
4+
"dependencies": {
5+
"socket.io": "0.9.14"
6+
}
7+
}

0 commit comments

Comments
 (0)