This repository has been archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Java-Websockets instead of Weberknecht, use SSLContext instead of…
… SSLFactory to make it work with Java-Websockets
- Loading branch information
Showing
7 changed files
with
120 additions
and
146 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters