BlackBerry Dynamics WebSocket is Java library based on Java WebSocket and GDSocket. It provides secure WebSocket client support and conforms to the WebSocket (RFC 6455 and RFC 7692) Java client library for Android.
- BlackBerry Dynamics SDK for Android, check environment requirements here.
For details on BlackBerry Dynamics please see https://www.blackberry.com/dynamics
- Nonblocking.
- Simple delegate pattern design.
- TLS/WSS support.
Follow these steps to integrate the BlackBerry Dynamics SDK:
The org.java_websocket.client.WebSocketClient
abstract class can connect to valid WebSocket servers. The constructor expects a valid ws://
URI to connect to.
Important events onOpen
, onClose
, onMessage
and onError
get fired throughout the life of the WebSocketClient, and must be implemented in your subclass.
Once the library is imported, open a connection to your WebSocket server.
WebSocketClient client = new SampleWebSocketClient(new URI("ws://localhost:8082"));
client.connect();
Note that socket is probably best provided as a property, so your delegate can stick around. Additionally, you can specify protocols if needed. s
After establishing a connection, the following delegate methods are required to be implemented.
onOpen
- WebSocket is ready to read/write data.onMessage
- Receive data in the onMessage method. It will contain a response or notification from the server.onClose
- WebSocket has been closed. To send data to the socket it will be necessary to reconnect.onError
- Caught an exception during reading/writing to the socket.
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.i(TAG, "Socket Opened");
}
@Override
public void onMessage(final String message) {
Log.i(TAG, "Received data from the server");
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.d(this, "onClose invoked");
}
webSocketClient.send(text) // write text over the socket!
Returns if the socket is connected or not.
if (webSocketClient.isOpen) {
// Can write data to the socket.
}
Close the WebSocket connection.
webSocketClient.close();
This library supports WebSocket over SSL/TLS (WSS).
A valid TLS certificate is required.
If you are using a self-signed certificate, note that browsers will refuse the connection and not prompt the user to accept the certificate. The following article explains a technique which may be helpful to force the browser to accept a self signed certificate during testing. ( https://bugzilla.mozilla.org/show_bug.cgi?id=594502 ).
Additionally the Java environment option -Djavax.net.debug=all
can help to find problems with the certificate.
- Accepting WS and WSS connections at the same time via the same WebSocket server instance is not currently supported.
- Firefox does not allow multiple connections to the same WSS server if the server uses a different port than the default port (443). This is specifically relevant for Android.
This sample is released as Open Source and licensed under Apache 2.0 License.