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

Commit

Permalink
Use Java-Websockets instead of Weberknecht, use SSLContext instead of…
Browse files Browse the repository at this point in the history
… SSLFactory to make it work with Java-Websockets
  • Loading branch information
Gottox committed Sep 11, 2012
1 parent 91ccf13 commit 946358c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 146 deletions.
Binary file added libs/WebSocket.jar
Binary file not shown.
Binary file removed libs/weberknecht-0.1.1.jar
Binary file not shown.
21 changes: 15 additions & 6 deletions src/io/socket/IOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -66,8 +67,7 @@ class IOConnection implements IOCallback {
public static final String SOCKET_IO_1 = "/socket.io/1/";

/** The SSL socket factory for HTTPS connections */
private static SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
private static SSLContext sslContext = null;

/** All available connections. */
private static HashMap<String, List<IOConnection>> connections = new HashMap<String, List<IOConnection>>();
Expand Down Expand Up @@ -205,10 +205,19 @@ public void run() {
/**
* Set the socket factory used for SSL connections.
*
* @param socketFactory
* @param sslContext
*/
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
sslSocketFactory = socketFactory;
public static void setSslContext(SSLContext sslContext) {
IOConnection.sslContext = sslContext;
}

/**
* Get the socket factory used for SSL connections.
*
* @return socketFactory
*/
public static SSLContext getSslContext() {
return sslContext;
}

/**
Expand Down Expand Up @@ -290,7 +299,7 @@ private void handshake() {
connection = url.openConnection();
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection)
.setSSLSocketFactory(sslSocketFactory);
.setSSLSocketFactory(sslContext.getSocketFactory());
}
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(connectTimeout);
Expand Down
5 changes: 3 additions & 2 deletions src/io/socket/SocketIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.URL;
import java.util.Properties;

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

import org.json.JSONObject;
Expand Down Expand Up @@ -127,8 +128,8 @@ public SocketIO(final URL url) {
* Set the socket factory used for SSL connections.
* @param socketFactory
*/
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
IOConnection.setDefaultSSLSocketFactory(socketFactory);
public static void setDefaultSSLSocketFactory(SSLContext sslContext) {
IOConnection.setSslContext(sslContext);
}

/**
Expand Down
231 changes: 94 additions & 137 deletions src/io/socket/WebsocketTransport.java
Original file line number Diff line number Diff line change
@@ -1,144 +1,101 @@
/*
* socket.io-java-client WebsocketTransport.java
*
* Copyright (c) 2012, Enno Boland
* socket.io-java-client is a implementation of the socket.io protocol in Java.
*
* See LICENSE file for more information
*/
package io.socket;


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

import de.roderick.weberknecht.WebSocketConnection;
import de.roderick.weberknecht.WebSocketEventHandler;
import de.roderick.weberknecht.WebSocketException;
import de.roderick.weberknecht.WebSocketMessage;

/**
* The Class WebsocketTransport.
*/
class WebsocketTransport implements IOTransport, WebSocketEventHandler {

WebSocketConnection websocket;

/** Pattern used to replace http:// by ws:// respectively https:// by wss:// */
private final static Pattern PATTERN_HTTP = Pattern.compile("^http");

/** The String to identify this Transport */
public static final String TRANSPORT_NAME = "websocket";

/** The IOConnection of this transport. */
private IOConnection connection;

/**
* Creates a new Transport for the given url an {@link IOConnection}.
*
* @param url the url
* @param connection the connection
* @return the iO transport
*/
public static IOTransport create(URL url, IOConnection connection) {
URI uri = URI.create(
PATTERN_HTTP.matcher(url.toString()).replaceFirst("ws")
+ IOConnection.SOCKET_IO_1 + TRANSPORT_NAME
+ "/" + connection.getSessionId());

return new WebsocketTransport(uri, connection);
}

/**
* Instantiates a new websocket transport.
*
* @param uri the uri
* @param connection the connection
* @throws WebSocketException
*/
public WebsocketTransport(URI uri, IOConnection connection) {
try {
websocket = new WebSocketConnection(uri);
} catch (WebSocketException e) {
connection.transportError(e);
return;
}
this.connection = connection;
websocket.setEventHandler(this);
}

/* (non-Javadoc)
* @see io.socket.IOTransport#disconnect()
*/
@Override
public void disconnect() {
try {
websocket.close();
} catch (Exception e) {
connection.transportError(e);
}
}

/* (non-Javadoc)
* @see io.socket.IOTransport#canSendBulk()
*/
@Override
public boolean canSendBulk() {
return false;
}

/* (non-Javadoc)
* @see io.socket.IOTransport#sendBulk(java.lang.String[])
*/
@Override
public void sendBulk(String[] texts) throws IOException {
throw new RuntimeException("Cannot send Bulk!");
}

/* (non-Javadoc)
* @see io.socket.IOTransport#invalidate()
*/
@Override
public void invalidate() {
connection = null;
}

@Override
public void onClose() {
if(connection != null)
connection.transportDisconnected();
}

@Override
public void onMessage(WebSocketMessage arg0) {
if(connection != null)
connection.transportMessage(arg0.getText());
}

@Override
public void onOpen() {
if(connection != null)
connection.transportConnected();
}

@Override
public void connect() {
try {
websocket.connect();
} catch (Exception e) {
connection.transportError(e);
}
}

@Override
public void send(String text) throws Exception {
websocket.send(text);
}

@Override
public String getName() {
return TRANSPORT_NAME;
}
}
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;

class WebsocketTransport extends WebSocketClient implements IOTransport {
private final static Pattern PATTERN_HTTP = Pattern.compile("^http");
public static final String TRANSPORT_NAME = "websocket";
private IOConnection connection;
public static IOTransport create(URL url, IOConnection connection) {
URI uri = URI.create(
PATTERN_HTTP.matcher(url.toString()).replaceFirst("ws")
+ IOConnection.SOCKET_IO_1 + TRANSPORT_NAME
+ "/" + connection.getSessionId());

return new WebsocketTransport(uri, connection);
}

public WebsocketTransport(URI uri, IOConnection connection) {
super(uri);
this.connection = connection;
SSLContext context = IOConnection.getSslContext();
if("wss".equals(uri.getScheme()) && context != null) {
this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
}
}

/* (non-Javadoc)
* @see io.socket.IOTransport#disconnect()
*/
@Override
public void disconnect() {
try {
this.close();
} catch (Exception e) {
connection.transportError(e);
}
}

/* (non-Javadoc)
* @see io.socket.IOTransport#canSendBulk()
*/
@Override
public boolean canSendBulk() {
return false;
}

/* (non-Javadoc)
* @see io.socket.IOTransport#sendBulk(java.lang.String[])
*/
@Override
public void sendBulk(String[] texts) throws IOException {
throw new RuntimeException("Cannot send Bulk!");
}

/* (non-Javadoc)
* @see io.socket.IOTransport#invalidate()
*/
@Override
public void invalidate() {
connection = null;
}

@Override
public void onClose(int code, String reason, boolean remote) {
if(connection != null)
connection.transportDisconnected();
}

@Override
public void onMessage(String text) {
if(connection != null)
connection.transportMessage(text);
}

@Override
public void onOpen(ServerHandshake handshakedata) {
if(connection != null)
connection.transportConnected();
}

@Override
public String getName() {
return TRANSPORT_NAME;
}

@Override
public void onError(Exception ex) {
// TODO Auto-generated method stub

}
}
7 changes: 7 additions & 0 deletions src/io/socket/XhrTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

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

/**
* The Class XhrTransport.
*/
Expand Down Expand Up @@ -76,6 +79,10 @@ public void run() {
URL url = new URL(XhrTransport.this.url.toString() + "?t="
+ System.currentTimeMillis());
urlConnection = (HttpURLConnection) url.openConnection();
SSLContext context = IOConnection.getSslContext();
if(urlConnection instanceof HttpsURLConnection && context != null) {
((HttpsURLConnection)urlConnection).setSSLSocketFactory(context.getSocketFactory());
}
if (!queue.isEmpty()) {
urlConnection.setDoOutput(true);
OutputStream output = urlConnection.getOutputStream();
Expand Down
2 changes: 1 addition & 1 deletion tests/io/socket/AbstractTestSocketIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class AbstractTestSocketIO implements IOCallback {
private static final String REQUEST_ACKNOWLEDGE = "requestAcknowledge";

/** The Constant to the node executable */
private final static String NODE = "node";
private final static String NODE = "/usr/local/bin/node";

/** The port of this test, randomly choosed */
private int port = -1;
Expand Down

0 comments on commit 946358c

Please sign in to comment.