Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Updating to latest Java-WebSocket and fixing SSL issues running on Android 4.4+ #114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<classpathentry kind="src" path="tests"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="libs/json-org.jar"/>
<classpathentry kind="lib" path="libs/WebSocket.jar"/>
<classpathentry exported="true" kind="lib" path="libs/json-org.jar"/>
<classpathentry exported="true" kind="lib" path="libs/Java-WebSocket-1.3.1-SNAPSHOT.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libs/Java-WebSocket"]
path = libs/Java-WebSocket
url = https://github.com/TooTallNate/Java-WebSocket
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<path id="socket.io-java-client.classpath">
<pathelement location="bin"/>
<pathelement location="libs/json-org.jar"/>
<pathelement location="libs/WebSocket.jar"/>
<pathelement location="libs/Java-WebSocket-1.3.1-SNAPSHOT.jar"/>
<path refid="JUnit 4.libraryclasspath"/>
</path>
<target name="init">
Expand Down
1 change: 1 addition & 0 deletions libs/Java-WebSocket
Submodule Java-WebSocket added at 05d2e2
Binary file added libs/Java-WebSocket-1.3.1-SNAPSHOT.jar
Binary file not shown.
Binary file removed libs/WebSocket.jar
Binary file not shown.
13 changes: 12 additions & 1 deletion src/io/socket/IOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -296,6 +296,9 @@ private void handshake() {
try {
setState(STATE_HANDSHAKE);
url = new URL(IOConnection.this.url.toString() + SOCKET_IO_1);

logger.info("Handshake > " + url);

connection = url.openConnection();
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection)
Expand All @@ -314,6 +317,8 @@ private void handshake() {
Scanner in = new Scanner(stream);
response = in.nextLine();
String[] data = response.split(":");
logger.info("Handshake < " + Arrays.asList(data));

sessionId = data[0];
heartbeatTimeout = Long.parseLong(data[1]) * 1000;
closingTimeout = Long.parseLong(data[2]) * 1000;
Expand All @@ -339,6 +344,9 @@ else if (protocols.contains(XhrTransport.TRANSPORT_NAME))
"Server supports no available transports. You should reconfigure the server to support a available transport"));
return;
}

logger.info("Connecting via " + transport);

transport.connect();
}

Expand Down Expand Up @@ -568,6 +576,7 @@ public void transportDisconnected() {
*/
public void transportError(Exception error) {
this.lastException = error;
logger.warning("Error: " + error);
setState(STATE_INTERRUPTED);
reconnect();
}
Expand Down Expand Up @@ -757,6 +766,8 @@ public void transportMessage(String text) {
*/
public synchronized void reconnect() {
if (getState() != STATE_INVALID) {
logger.info("Reconnecting...");

invalidateTransport();
setState(STATE_INTERRUPTED);
if (reconnectTask != null) {
Expand Down
27 changes: 22 additions & 5 deletions src/io/socket/WebsocketTransport.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.socket;

import java.io.IOException;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.util.regex.Pattern;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

Expand All @@ -30,7 +29,15 @@ public WebsocketTransport(URI uri, IOConnection connection) {
this.connection = connection;
SSLContext context = IOConnection.getSslContext();
if("wss".equals(uri.getScheme()) && context != null) {
this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
try {
int port = uri.getPort() < 0 ? 443 : uri.getPort();

Socket socket = context.getSocketFactory().createSocket(uri.getHost(), port);

this.setSocket(socket);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Expand All @@ -46,6 +53,13 @@ public void disconnect() {
}
}


@Override
public void connect() {
IOConnection.logger.info("Connecting to " + getURI());

super.connect();
}
/* (non-Javadoc)
* @see io.socket.IOTransport#canSendBulk()
*/
Expand All @@ -72,8 +86,11 @@ public void invalidate() {

@Override
public void onClose(int code, String reason, boolean remote) {
if(connection != null)
if(connection != null) {
IOConnection.logger.info("Closed [code=" + code + ", reason=" + reason + ", remote=" + remote + "]");

connection.transportDisconnected();
}
}

@Override
Expand All @@ -96,6 +113,6 @@ public String getName() {
@Override
public void onError(Exception ex) {
// TODO Auto-generated method stub

ex.printStackTrace();
}
}