Skip to content

Commit d24aaff

Browse files
committed
Bug: 459142 - WebSocket support for the Java client
With this change, you can now use the Java client to connect to MQTT brokers supporting WebSockets. To do this, use either ws or wss as the protocol type in the URI. For example: ws://localhost:1883 or wss://localhost:1883 Signed-off-by: James Sutton <james.sutton@uk.ibm.com> Change-Id: Iefa994ad5950ea2a66a4733512f62a2755fa0f82
1 parent fa18abe commit d24aaff

9 files changed

Lines changed: 956 additions & 0 deletions

File tree

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttAsyncClient.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* Ian Craggs - MQTT 3.1.1 support
1616
* Ian Craggs - per subscription message handlers (bug 466579)
1717
* Ian Craggs - ack control (bug 472172)
18+
* James Sutton - Bug 459142 - WebSocket support for the Java client.
1819
*/
20+
1921
package org.eclipse.paho.client.mqttv3;
2022

2123
import java.util.Hashtable;
@@ -32,6 +34,8 @@
3234
import org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule;
3335
import org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule;
3436
import org.eclipse.paho.client.mqttv3.internal.security.SSLSocketFactoryFactory;
37+
import org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule;
38+
import org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketNetworkModule;
3539
import org.eclipse.paho.client.mqttv3.internal.wire.MqttDisconnect;
3640
import org.eclipse.paho.client.mqttv3.internal.wire.MqttPublish;
3741
import org.eclipse.paho.client.mqttv3.internal.wire.MqttSubscribe;
@@ -404,6 +408,47 @@ else if ((factory instanceof SSLSocketFactory) == false) {
404408
}
405409
}
406410
break;
411+
case MqttConnectOptions.URI_TYPE_WS:
412+
shortAddress = address.substring(5);
413+
host = getHostName(shortAddress);
414+
port = getPort(shortAddress, 1883);
415+
if (factory == null) {
416+
factory = SocketFactory.getDefault();
417+
}
418+
else if (factory instanceof SSLSocketFactory) {
419+
throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
420+
}
421+
netModule = new WebSocketNetworkModule(factory, host, port, clientId);
422+
((WebSocketNetworkModule)netModule).setConnectTimeout(options.getConnectionTimeout());
423+
break;
424+
case MqttConnectOptions.URI_TYPE_WSS:
425+
shortAddress = address.substring(6);
426+
host = getHostName(shortAddress);
427+
port = getPort(shortAddress, 8883);
428+
SSLSocketFactoryFactory wSSFactoryFactory = null;
429+
if (factory == null) {
430+
wSSFactoryFactory = new SSLSocketFactoryFactory();
431+
Properties sslClientProps = options.getSSLProperties();
432+
if (null != sslClientProps)
433+
wSSFactoryFactory.initialize(sslClientProps, null);
434+
factory = wSSFactoryFactory.createSocketFactory(null);
435+
436+
}
437+
else if ((factory instanceof SSLSocketFactory) == false) {
438+
throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_SOCKET_FACTORY_MISMATCH);
439+
}
440+
441+
// Create the network module...
442+
netModule = new WebSocketSecureNetworkModule((SSLSocketFactory) factory, host, port, clientId);
443+
((WebSocketSecureNetworkModule)netModule).setSSLhandshakeTimeout(options.getConnectionTimeout());
444+
// Ciphers suites need to be set, if they are available
445+
if (wSSFactoryFactory != null) {
446+
String[] enabledCiphers = wSSFactoryFactory.getEnabledCipherSuites(null);
447+
if (enabledCiphers != null) {
448+
((SSLNetworkModule) netModule).setEnabledCiphers(enabledCiphers);
449+
}
450+
}
451+
break;
407452
case MqttConnectOptions.URI_TYPE_LOCAL :
408453
netModule = new LocalNetworkModule(address.substring(8));
409454
break;

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class MqttConnectOptions {
5555
protected static final int URI_TYPE_TCP = 0;
5656
protected static final int URI_TYPE_SSL = 1;
5757
protected static final int URI_TYPE_LOCAL = 2;
58+
protected static final int URI_TYPE_WS = 3;
59+
protected static final int URI_TYPE_WSS = 4;
5860

5961
private int keepAliveInterval = KEEP_ALIVE_INTERVAL_DEFAULT;
6062
private int maxInflight = MAX_INFLIGHT_DEFAULT;
@@ -494,6 +496,12 @@ else if (vURI.getScheme().equals("ssl")) {
494496
else if (vURI.getScheme().equals("local")) {
495497
return URI_TYPE_LOCAL;
496498
}
499+
else if (vURI.getScheme().equals("ws")){
500+
return URI_TYPE_WS;
501+
}
502+
else if (vURI.getScheme().equals("wss")) {
503+
return URI_TYPE_WSS;
504+
}
497505
else {
498506
throw new IllegalArgumentException(srvURI);
499507
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2014 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.php.
12+
*
13+
* Contributors:
14+
* James Sutton - Bug 459142 - WebSocket support for the Java client.
15+
*/
16+
package org.eclipse.paho.client.mqttv3.internal.websocket;
17+
18+
import java.util.prefs.AbstractPreferences;
19+
import java.util.prefs.BackingStoreException;
20+
21+
public class Base64 {
22+
23+
private static final Base64 instance = new Base64();
24+
private static final Base64Encoder encoder = instance.new Base64Encoder();
25+
26+
public static String encode (String s){
27+
encoder.putByteArray("akey", s.getBytes());
28+
String result = encoder.getBase64String();
29+
return result;
30+
}
31+
32+
public static String encodeBytes (byte[] b){
33+
encoder.putByteArray("aKey", b);
34+
String result = encoder.getBase64String();
35+
return result;
36+
37+
}
38+
39+
public class Base64Encoder extends AbstractPreferences {
40+
41+
private String base64String = null;
42+
43+
public Base64Encoder() {
44+
super(null, "");
45+
}
46+
47+
48+
protected void putSpi(String key, String value) {
49+
base64String = value;
50+
}
51+
52+
public String getBase64String() {
53+
return base64String;
54+
}
55+
56+
57+
protected String getSpi(String key) {
58+
return null;
59+
}
60+
61+
62+
protected void removeSpi(String key) {
63+
}
64+
65+
66+
protected void removeNodeSpi() throws BackingStoreException {
67+
68+
}
69+
70+
71+
protected String[] keysSpi() throws BackingStoreException {
72+
return null;
73+
}
74+
75+
76+
protected String[] childrenNamesSpi() throws BackingStoreException {
77+
return null;
78+
}
79+
80+
81+
protected AbstractPreferences childSpi(String name) {
82+
return null;
83+
}
84+
85+
86+
protected void syncSpi() throws BackingStoreException {
87+
88+
}
89+
90+
91+
protected void flushSpi() throws BackingStoreException {
92+
93+
}
94+
95+
}
96+
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2014 IBM Corp.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* and Eclipse Distribution License v1.0 which accompany this distribution.
7+
*
8+
* The Eclipse Public License is available at
9+
* http://www.eclipse.org/legal/epl-v10.html
10+
* and the Eclipse Distribution License is available at
11+
* http://www.eclipse.org/org/documents/edl-v10.php.
12+
*
13+
* Contributors:
14+
* James Sutton - Bug 459142 - WebSocket support for the Java client.
15+
*/
16+
package org.eclipse.paho.client.mqttv3.internal.websocket;
17+
18+
public class HandshakeFailedException extends Exception {
19+
20+
private static final long serialVersionUID = 1L;
21+
22+
}

0 commit comments

Comments
 (0)