├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
├── org.eclipse.jdt.core.prefs
└── org.eclipse.m2e.core.prefs
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
├── java
│ ├── io
│ │ └── inventit
│ │ │ └── dev
│ │ │ └── mqtt
│ │ │ └── paho
│ │ │ └── jetty8
│ │ │ ├── Jdk16MqttWebSocketAsyncClient.java
│ │ │ └── WebSocketNetworkModule.java
│ └── org
│ │ └── eclipse
│ │ └── jetty
│ │ └── websocket
│ │ └── api
│ │ └── WebSocketAdapter.java
└── libs
│ └── org
│ └── eclipse
│ └── paho
│ └── org.eclipse.paho.client.mqttv3
│ └── 1.0.0
│ └── org.eclipse.paho.client.mqttv3-1.0.0.pom
└── test
├── android
├── .classpath
├── .gitignore
├── .project
├── .settings
│ ├── org.eclipse.core.resources.prefs
│ ├── org.eclipse.jdt.core.prefs
│ └── org.eclipse.m2e.core.prefs
├── AndroidManifest.xml
├── pom.xml
├── proguard.cfg
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ └── icon.png
│ ├── drawable-ldpi
│ │ └── icon.png
│ ├── drawable-mdpi
│ │ └── icon.png
│ ├── drawable
│ │ └── icon.png
│ ├── layout
│ │ └── simple.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── main
│ ├── java
│ └── io
│ │ └── inventit
│ │ └── dev
│ │ └── android
│ │ └── testapp
│ │ └── mqtt
│ │ └── ws
│ │ └── MainApplication.java
│ ├── libs
│ └── org
│ │ └── eclipse
│ │ └── paho
│ │ └── org.eclipse.paho.client.mqttv3
│ │ └── 1.0.0
│ │ └── org.eclipse.paho.client.mqttv3-1.0.0.pom
│ └── resources
│ └── android-logger.properties
└── java
└── io
└── inventit
└── dev
└── mqtt
└── paho
└── jetty8
├── Jdk16MqttWebSocketAsyncClientInContainerTest.java
├── Jdk16MqttWebSocketAsyncClientTest.java
├── JettyStrErrLogUtils.java
└── WebSocketNetworkModuleInContainerTest.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | /pom.xml.versionsBackup
3 |
4 | *.class
5 |
6 | # Mobile Tools for Java (J2ME)
7 | .mtj.tmp/
8 |
9 | # Package Files #
10 | *.jar
11 | *.war
12 | *.ear
13 |
14 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
15 | hs_err_pid*
16 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | mqtt-websocket-jdk16-android
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/main/java=UTF-8
3 | encoding//src/main/resources=UTF-8
4 | encoding//src/test/java=UTF-8
5 | encoding//src/test/resources=UTF-8
6 | encoding/=UTF-8
7 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5 | org.eclipse.jdt.core.compiler.source=1.6
6 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Inventit Inc.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Announcement
2 |
3 | Eclipse Paho Android Java client has WebSocket support. You can use Paho Android Java client for using MQTT over WebSocket.
4 |
5 | Please refer to the project page at https://eclipse.org/paho/clients/android/
6 |
7 | The project is EOL. Thank you for all our project users and contributors.
8 |
9 | Daisuke Baba
10 |
11 | ---
12 |
13 | # MQTT over WebSocket library for JDK1.6/Android
14 |
15 | This library offers MQTT client functionality over WebSocket transport with [Paho](http://www.eclipse.org/paho/) library and [Jetty 8](http://www.eclipse.org/jetty/) library.
16 |
17 | # Supported MQTT Version
18 |
19 | 1. MQTT v3.1 (with Sub-Protocol: `mqttv3.1`)
20 | 1. MQTT v3.1.1 (with Sub-Protocol: `mqtt`) ... DEFAULT
21 |
22 | # Supported Paho MQTT library version and Jetty WebSocket Client version
23 |
24 | 1. [Paho org.eclipse.paho.mqtt.java 1.0.0](http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/tag/?id=v1.0.0)
25 | 1. [Jetty websocket-client 8.1.16.v20140903](http://download.eclipse.org/jetty/stable-8/apidocs/index.html?org/eclipse/jetty/websocket/package-summary.html)
26 |
27 | # Supported JDK/JRE Version
28 |
29 | JDK/JRE 1.6+ is supported.
30 |
31 | # Dependencies
32 |
33 | The following libraries are requried as well as `org.eclipse.paho.client.mqttv3-1.0.0.jar`.
34 |
35 | | GroupId | ArtifactId | Version |
36 | |-----------------|----------------|----------------|
37 | |org.eclipse.jetty|jetty-http |8.1.16.v20140903|
38 | |org.eclipse.jetty|jetty-io |8.1.16.v20140903|
39 | |org.eclipse.jetty|jetty-util |8.1.16.v20140903|
40 | |org.eclipse.jetty|jetty-websocket |8.1.16.v20140903|
41 |
42 | ## maven pom.xml settings
43 |
44 | Adds the following elements to your pom.xml if you're using maven.
45 |
46 | ```
47 |
48 | io.inventit.dev
49 | mqtt-websocket-jdk16-android
50 | 1.0.0
51 |
52 |
53 | io.inventit.dev
54 | mqtt-websocket-java
55 | 1.0.1
56 | jdk16
57 |
58 |
59 | org.eclipse.jetty
60 | jetty-websocket
61 | 8.1.16.v20140903
62 |
63 |
64 | org.eclipse.paho
65 | org.eclipse.paho.client.mqttv3
66 | 1.0.0
67 |
68 |
69 | com.noveogroup.android
70 | android-logger
71 | 1.3.1
72 |
73 | ```
74 |
75 | The `android-logger` is included because it includes SLF4J API, which Jetty uses as a default logging framework. You can use other SLF4J API compliant logger library as you like.
76 |
77 | # How to install
78 | Download the jar file from the [releases tab](https://github.com/inventit/mqtt-websocket-jdk16-android/releases/) and add it to your IDE or your favorite building tool.
79 |
80 | For maven users, the [releases page](https://github.com/inventit/mqtt-websocket-jdk16-android/releases/) instruction helps you to install the jar file to your local repo.
81 |
82 | # How to use
83 | You can use this library as the same manner as Paho's library but use `Jdk16MqttWebSocketAsyncClient` instead of Paho's classes such as `MqttClient` and `MqttAsyncClient`.
84 |
85 | The `Jdk16MqttWebSocketAsyncClient` supports the following URI schimes:
86 |
87 | 1. `ws://:` ... for a plain WebSocket
88 | 1. `wss://:` ... for a WebSocket with SSL/TLS
89 | 1. `tcp://:` ... for a plain TCP MQTT socket
90 | 1. `ssl://:` ... for a secure SSL/TLS MQTT socket
91 |
92 | Here is sample code to use `Jdk16MqttWebSocketAsyncClient`.
93 |
94 | // Plain MQTT
95 | // final String uriString = "tcp://your-mqtt-broker:1883";
96 |
97 | // MQTT over WebSocket
98 | final String uriString = "wss://your-ws-broker/mqtt";
99 |
100 | // Credentials
101 | final String clientId = "your-client-id";
102 | final String userName = "your-user-name";
103 | final String password = "your-password";
104 |
105 | final IMqttAsyncClient client = new Jdk16MqttWebSocketAsyncClient(
106 | uriString, clientId, new MemoryPersistence());
107 | final MqttConnectOptions options = new MqttConnectOptions();
108 | options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
109 | options.setCleanSession(true);
110 | options.setUserName(userName);
111 | options.setPassword(password.toCharArray());
112 | client.connect(options, null, new IMqttActionListener() {
113 |
114 | @Override
115 | public void onSuccess(IMqttToken asyncActionToken) {
116 | // on successfully connected
117 | }
118 |
119 | @Override
120 | public void onFailure(IMqttToken asyncActionToken,
121 | Throwable exception) {
122 | // on connection failure
123 | }
124 | );
125 | client.setCallback(new MqttCallback() {
126 |
127 | @Override
128 | public void messageArrived(String topic, MqttMessage message)
129 | throws Exception {
130 | }
131 |
132 | @Override
133 | public void deliveryComplete(IMqttDeliveryToken token) {
134 | }
135 |
136 | @Override
137 | public void connectionLost(Throwable cause) {
138 | }
139 | });
140 |
141 | # How to build
142 |
143 | ## Library jar
144 |
145 | Install maven then run the following command on the project root directory.
146 |
147 | Note that Paho Java library is included in this project as the binary isn't uploaded to any maven repository yet.
148 |
149 | $ mvn clean package
150 |
151 | Then you'll get `mqtt-websocket-jdk16-android-.jar` under the `target` directory.
152 |
153 | ## Android Test app
154 |
155 | With maven, run the following commands after installing the library jar file (mqtt-websocket-jdk16-android).
156 |
157 | $ cd src/test/android
158 | $ mvn clean package
159 | $ adb install -r target/mqtt-websocket-test.apk
160 |
161 | Or use IDE to build an APK and install it to your Android device.
162 |
163 | * Tap an applicaiton icon image to publish a message, see `MainApplication#publish()` for detail
164 | * The app automatically subscribes a topic `/io/inventit/dev/android/testapp/android`, see `MainApplication#startTesting()` for detail
165 |
166 | # Source Code License
167 |
168 | See [LICENSE](https://github.com/inventit/mqtt-websocket-jdk16-android/blob/master/LICENSE) file.
169 |
170 | # Change History
171 |
172 | [1.0.0 : December 7, 2014](https://github.com/inventit/mqtt-websocket-jdk16-android/releases/tag/1.0.0)
173 |
174 | * Initial
175 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | 4.0.0
6 |
7 | io.inventit.dev
8 | mqtt-websocket-jdk16-android
9 | 1.0.0
10 | MQTT over WebScoket for JDK 1.6/Android with Paho and Jetty 8
11 | https://github.com/inventit/mqtt-websocket-jdk16-android
12 |
13 |
14 | MIT
15 | https://github.com/inventit/mqtt-websocket-jdk16-android/blob/master/LICENSE
16 |
17 |
18 |
19 | https://github.com/inventit/mqtt-websocket-jdk16-android
20 | scm:git:git://github.com/inventit/mqtt-websocket-jdk16-android.git
21 | scm:git:git@github.com:inventit/mqtt-websocket-jdk16-android.git
22 |
23 |
24 | https://github.com/inventit/mqtt-websocket-jdk16-android/issues
25 | GitHub Issues
26 |
27 |
28 |
29 | false
30 | LocalRepository
31 | Local Repository
32 | file://${basedir}/target
33 | default
34 |
35 |
36 |
37 | UTF-8
38 |
39 |
40 |
41 |
42 | ProjectLocalLibrariesRepository
43 | Project Local Libraries Repository
44 | file://${basedir}/src/main/libs
45 | default
46 |
47 |
48 |
49 |
50 |
51 |
52 | maven-compiler-plugin
53 | 3.1
54 |
55 | UTF-8
56 | 1.6
57 | 1.6
58 |
59 |
60 |
61 | maven-surefire-plugin
62 | 2.17
63 |
64 | 0
65 | -Dfile.encoding=UTF-8 -Xmx512m -XX:MaxPermSize=256m
66 |
67 | **/*InContainerTest.java
68 |
69 |
70 |
71 |
72 | org.codehaus.mojo
73 | cobertura-maven-plugin
74 | 2.5.1
75 |
76 |
77 | false
78 |
79 |
80 |
81 |
82 | pre-site
83 |
84 | clean
85 | check
86 |
87 |
88 |
89 |
90 |
91 | maven-clean-plugin
92 |
93 |
94 |
95 | ${basedir}
96 |
97 | **/*.log*
98 |
99 |
100 | .hg/**/*
101 | .git/**/*
102 | **/src/test/resources/logs/*
103 |
104 | false
105 |
106 |
107 |
108 |
109 |
110 | maven-javadoc-plugin
111 | 2.9
112 |
113 |
114 |
115 | UTF-8
116 |
117 | http://java.sun.com/javase/6/docs/api/
118 | http://download.oracle.com/javaee/5/api/
119 |
120 |
121 |
122 |
123 | maven-jar-plugin
124 |
125 |
126 | false
127 |
128 | true
129 |
130 |
131 | InventIt Inc. dev.inventit.io
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 | io.inventit.dev
142 | mqtt-websocket-java
143 | 1.0.1
144 | jdk16
145 |
146 |
147 | org.eclipse.jetty
148 | jetty-websocket
149 | 8.1.16.v20140903
150 |
151 |
152 | org.eclipse.paho
153 | org.eclipse.paho.client.mqttv3
154 | 1.0.0
155 |
156 |
157 | ch.qos.logback
158 | logback-classic
159 | 1.1.2
160 | test
161 |
162 |
163 | junit
164 | junit
165 | 4.11
166 | test
167 |
168 |
169 | org.mockito
170 | mockito-all
171 | 1.9.5
172 | test
173 |
174 |
175 | org.easytesting
176 | fest-assert-core
177 | 2.0M10
178 | test
179 |
180 |
181 | org.jmock
182 | jmock
183 | 2.6.0
184 | test
185 |
186 |
187 |
188 |
--------------------------------------------------------------------------------
/src/main/java/io/inventit/dev/mqtt/paho/jetty8/Jdk16MqttWebSocketAsyncClient.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.mqtt.paho.jetty8;
5 |
6 | import io.inventit.dev.mqtt.paho.MqttWebSocketAsyncClient;
7 |
8 | import java.net.URI;
9 |
10 | import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
11 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
12 | import org.eclipse.paho.client.mqttv3.MqttException;
13 | import org.eclipse.paho.client.mqttv3.MqttPingSender;
14 | import org.eclipse.paho.client.mqttv3.internal.NetworkModule;
15 |
16 | public class Jdk16MqttWebSocketAsyncClient extends MqttWebSocketAsyncClient {
17 |
18 | public Jdk16MqttWebSocketAsyncClient(String serverURI, String clientId,
19 | MqttClientPersistence persistence, MqttPingSender pingSender,
20 | String loggerName) throws MqttException {
21 | super(serverURI, clientId, persistence, pingSender, loggerName);
22 | }
23 |
24 | public Jdk16MqttWebSocketAsyncClient(String serverURI, String clientId,
25 | MqttClientPersistence persistence, String loggerName)
26 | throws MqttException {
27 | super(serverURI, clientId, persistence, loggerName);
28 | }
29 |
30 | public Jdk16MqttWebSocketAsyncClient(String serverURI, String clientId,
31 | MqttClientPersistence persistence) throws MqttException {
32 | super(serverURI, clientId, persistence);
33 | }
34 |
35 | public Jdk16MqttWebSocketAsyncClient(String serverURI, String clientId,
36 | String loggerName) throws MqttException {
37 | super(serverURI, clientId, loggerName);
38 | }
39 |
40 | public Jdk16MqttWebSocketAsyncClient(String serverURI, String clientId)
41 | throws MqttException {
42 | super(serverURI, clientId);
43 | }
44 |
45 | /**
46 | * A factory method for instantiating a {@link NetworkModule} with websocket
47 | * support. Subclasses is able to extend this method in order to create an
48 | * arbitrary {@link NetworkModule} class instance.
49 | *
50 | * @param uri
51 | * @param subProtocol
52 | * Either `mqtt` for MQTT v3.1.1 or `mqttv3.1` for MQTT v3.1
53 | * @param options
54 | * @return
55 | */
56 | @Override
57 | protected NetworkModule newWebSocketNetworkModule(URI uri,
58 | String subProtocol, MqttConnectOptions options) {
59 | final WebSocketNetworkModule netModule = new WebSocketNetworkModule(
60 | uri, subProtocol, getClientId());
61 | netModule.setConnectTimeout(options.getConnectionTimeout());
62 | return netModule;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/io/inventit/dev/mqtt/paho/jetty8/WebSocketNetworkModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.mqtt.paho.jetty8;
5 |
6 | import java.io.ByteArrayOutputStream;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.OutputStream;
10 | import java.io.PipedInputStream;
11 | import java.io.PipedOutputStream;
12 | import java.net.ConnectException;
13 | import java.net.URI;
14 | import java.util.concurrent.TimeUnit;
15 |
16 | import org.eclipse.jetty.websocket.WebSocket;
17 | import org.eclipse.jetty.websocket.WebSocketClient;
18 | import org.eclipse.jetty.websocket.WebSocketClientFactory;
19 | import org.eclipse.paho.client.mqttv3.MqttException;
20 | import org.eclipse.paho.client.mqttv3.internal.NetworkModule;
21 | import org.eclipse.paho.client.mqttv3.logging.Logger;
22 | import org.eclipse.paho.client.mqttv3.logging.LoggerFactory;
23 |
24 | public class WebSocketNetworkModule implements NetworkModule,
25 | WebSocket.OnBinaryMessage {
26 |
27 | private static final String CLASS_NAME = WebSocketNetworkModule.class
28 | .getName();
29 | private static final Logger log = LoggerFactory.getLogger(
30 | LoggerFactory.MQTT_CLIENT_MSG_CAT, CLASS_NAME);
31 |
32 | /**
33 | * WebSocket URI
34 | */
35 | private final URI uri;
36 |
37 | /**
38 | * Sub-Protocol
39 | */
40 | private final String subProtocol;
41 |
42 | /**
43 | * A stream for outgoing data
44 | */
45 | private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream() {
46 | @Override
47 | public void flush() throws IOException {
48 | final byte[] data;
49 | synchronized (this) {
50 | data = toByteArray();
51 | reset();
52 | }
53 | getConnection().sendMessage(data, 0, data.length);
54 | }
55 | };
56 |
57 | /**
58 | * A pair of streams for incoming data
59 | */
60 | private final PipedOutputStream receiverStream = new PipedOutputStream();
61 | private final PipedInputStream inputStream;
62 |
63 | private WebSocketClientFactory factory;
64 | private WebSocketClient client;
65 | private int conTimeout;
66 | private Connection connection;
67 |
68 | /**
69 | * Constructs a new WebSocketNetworkModule using the specified URI.
70 | *
71 | * @param uri
72 | * @param subProtocol
73 | * @param resourceContext
74 | */
75 | public WebSocketNetworkModule(URI uri, String subProtocol,
76 | String resourceContext) {
77 | log.setResourceName(resourceContext);
78 | this.uri = uri;
79 | this.subProtocol = subProtocol;
80 | try {
81 | this.inputStream = new PipedInputStream(receiverStream);
82 | } catch (IOException unexpected) {
83 | throw new IllegalStateException(unexpected);
84 | }
85 | }
86 |
87 | protected Connection getConnection() {
88 | return connection;
89 | }
90 |
91 | /**
92 | * A factory method for {@link WebSocketClient} class
93 | *
94 | * @return
95 | */
96 | protected WebSocketClient createWebSocketClient() {
97 | factory = new WebSocketClientFactory();
98 | factory.getSslContextFactory().setTrustAll(false);
99 | try {
100 | factory.start();
101 | } catch (Exception e) {
102 | throw new IllegalStateException(e);
103 | }
104 | final WebSocketClient client = factory.newWebSocketClient();
105 | // you can manipulate the client by overriding this method.
106 | return client;
107 | }
108 |
109 | /**
110 | * Starts the module, by creating a TCP socket to the server.
111 | */
112 | @Override
113 | public void start() throws IOException, MqttException {
114 | final String methodName = "start";
115 | try {
116 | // @TRACE 252=connect to host {0} port {1} timeout {2}
117 | if (log.isLoggable(Logger.FINE)) {
118 | log.fine(
119 | CLASS_NAME,
120 | methodName,
121 | "252",
122 | new Object[] { uri.toString(),
123 | Integer.valueOf(uri.getPort()),
124 | Long.valueOf(conTimeout * 1000) });
125 | }
126 | client = createWebSocketClient();
127 | client.setProtocol(subProtocol);
128 | if (conTimeout > 0) {
129 | connection = client.open(uri, this, conTimeout,
130 | TimeUnit.SECONDS);
131 | } else {
132 | // wait until a connection is established
133 | connection = client.open(uri, this).get();
134 | }
135 |
136 | } catch (ConnectException ex) {
137 | // @TRACE 250=Failed to create TCP socket
138 | log.fine(CLASS_NAME, methodName, "250", null, ex);
139 | throw new MqttException(
140 | MqttException.REASON_CODE_SERVER_CONNECT_ERROR, ex);
141 |
142 | } catch (Exception ex) {
143 | // @TRACE 250=Failed to create TCP socket
144 | log.fine(CLASS_NAME, methodName, "250", null, ex);
145 | throw new MqttException(MqttException.REASON_CODE_UNEXPECTED_ERROR,
146 | ex);
147 | }
148 | }
149 |
150 | @Override
151 | public InputStream getInputStream() throws IOException {
152 | return inputStream;
153 | }
154 |
155 | @Override
156 | public OutputStream getOutputStream() throws IOException {
157 | return outputStream;
158 | }
159 |
160 | /**
161 | * Stops the module, by closing the web socket.
162 | */
163 | @Override
164 | public void stop() throws IOException {
165 | if (connection != null && connection.isOpen()) {
166 | connection.close();
167 | }
168 | if (factory != null && factory.isRunning()) {
169 | try {
170 | factory.stop();
171 | } catch (Exception e) {
172 | throw new IllegalStateException(e);
173 | }
174 | factory.destroy();
175 | }
176 | connection = null;
177 | client = null;
178 | factory = null;
179 | }
180 |
181 | /**
182 | * Set the maximum time in seconds to wait for a socket to be established
183 | *
184 | * @param timeout
185 | * in seconds
186 | */
187 | public void setConnectTimeout(int timeout) {
188 | this.conTimeout = timeout;
189 | }
190 |
191 | @Override
192 | public void onMessage(byte[] payload, int offset, int len) {
193 | try {
194 | this.receiverStream.write(payload, offset, len);
195 | this.receiverStream.flush();
196 | } catch (IOException e) {
197 | log.fine(CLASS_NAME, "onWebSocketError", "401", null, e);
198 | throw new IllegalStateException(e);
199 | }
200 | }
201 |
202 | @Override
203 | public void onOpen(Connection connection) {
204 | if (log.isLoggable(Logger.FINE)) {
205 | log.fine(CLASS_NAME, "onOpen", "116", new Object[] { uri.toString()
206 | + ", WebSocket CONNECTED." });
207 | }
208 | }
209 |
210 | @Override
211 | public void onClose(int closeCode, String message) {
212 | if (log.isLoggable(Logger.FINE)) {
213 | log.fine(CLASS_NAME, "onClose", "116",
214 | new Object[] { uri.toString() + ", WebSocket CLOSED." });
215 | }
216 | }
217 | }
218 |
--------------------------------------------------------------------------------
/src/main/java/org/eclipse/jetty/websocket/api/WebSocketAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package org.eclipse.jetty.websocket.api;
5 |
6 | /**
7 | * A dummy class for suppressing dalvik error
8 | */
9 | public class WebSocketAdapter {
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/libs/org/eclipse/paho/org.eclipse.paho.client.mqttv3/1.0.0/org.eclipse.paho.client.mqttv3-1.0.0.pom:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | org.eclipse.paho
4 | org.eclipse.paho.client.mqttv3
5 | 1.0.0
6 | http://www.eclipse.org/paho/
7 | MQTT Java Client
8 | http://www.eclipse.org/legal/epl-v10.html
9 |
--------------------------------------------------------------------------------
/src/test/android/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/test/android/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 | /target/
3 |
--------------------------------------------------------------------------------
/src/test/android/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | mqtt-websocket-test
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 | org.eclipse.m2e.core.maven2Builder
30 |
31 |
32 |
33 |
34 |
35 | com.android.ide.eclipse.adt.AndroidNature
36 | org.eclipse.jdt.core.javanature
37 | org.eclipse.m2e.core.maven2Nature
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/test/android/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/main/java=UTF-8
3 |
--------------------------------------------------------------------------------
/src/test/android/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5 | org.eclipse.jdt.core.compiler.source=1.6
6 |
--------------------------------------------------------------------------------
/src/test/android/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/src/test/android/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
9 |
10 |
16 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/src/test/android/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | io.inventit.dev.android
6 | mqtt-websocket-test
7 | 1.0.0
8 | mqtt-websocket-jdk16-android test Android app
9 |
10 |
11 | false
12 | SharedLibrariesRepository
13 | Shared Libraries Repository
14 | file://${basedir}/target
15 | default
16 |
17 |
18 |
19 |
20 | ProjectLocalLibrariesRepository
21 | Project Local Libraries Repository
22 | file://${basedir}/src/main/libs
23 | default
24 |
25 |
26 |
27 | 19
28 |
29 |
30 |
31 | io.inventit.dev
32 | mqtt-websocket-jdk16-android
33 | 1.0.0
34 |
35 |
36 | io.inventit.dev
37 | mqtt-websocket-java
38 | 1.0.1
39 | jdk16
40 |
41 |
42 | org.eclipse.jetty
43 | jetty-websocket
44 | 8.1.16.v20140903
45 |
46 |
47 | org.eclipse.paho
48 | org.eclipse.paho.client.mqttv3
49 | 1.0.0
50 |
51 |
52 |
53 | com.noveogroup.android
54 | android-logger
55 | 1.3.1
56 |
57 |
58 | com.google.android
59 | android
60 | 4.0.1.2
61 | provided
62 |
63 |
64 | commons-logging
65 | commons-logging
66 |
67 |
68 | httpclient
69 | org.apache.httpcomponents
70 |
71 |
72 | json
73 | org.json
74 |
75 |
76 |
77 |
78 | org.json
79 | json
80 | 20090211
81 | provided
82 |
83 |
84 |
85 | ${project.artifactId}
86 |
87 |
88 | maven-jar-plugin
89 |
90 |
91 | false
92 |
93 |
94 |
95 |
96 | maven-compiler-plugin
97 | 3.1
98 |
99 | UTF-8
100 | 1.6
101 | 1.6
102 |
103 |
104 |
105 |
107 | com.jayway.maven.plugins.android.generation2
108 | android-maven-plugin
109 |
110 | 3.9.0-rc.3
111 |
112 |
113 | ${env.ANDROID_HOME}
114 | ${android.api}
115 |
116 |
117 | true
118 |
119 |
120 | true
121 |
122 | true
123 |
124 |
125 | org.codehaus.mojo
126 | buildnumber-maven-plugin
127 | 1.0-beta-4
128 |
129 |
130 | none
131 |
132 |
133 |
134 |
135 | org.apache.maven.plugins
136 | maven-source-plugin
137 | 2.1.2
138 |
139 |
140 | compile
141 |
142 | jar
143 |
144 |
145 |
146 |
147 |
148 | org.apache.maven.plugins
149 | maven-javadoc-plugin
150 | 2.8.1
151 |
152 |
153 | package
154 |
155 | jar
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
165 |
166 | org.eclipse.m2e
167 | lifecycle-mapping
168 | 1.0.0
169 |
170 |
171 |
172 |
173 |
174 |
175 | com.jayway.maven.plugins.android.generation2
176 |
177 |
178 | android-maven-plugin
179 |
180 |
181 | [3.8.2,)
182 |
183 |
184 | consume-aar
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 | oss.sonatype.org-jayway-snapshots
202 | Jayway OpenSource SNAPSHOTs on Sonatype.org
203 | http://oss.sonatype.org/content/repositories/jayway-snapshots/
204 |
205 | true
206 |
207 |
208 |
209 |
210 | Inventit Inc.
211 | https://github.com/inventit
212 |
213 | https://github.com/inventit/mqtt-websocket-jdk16-android
214 |
215 | scm:git:git@github.com:inventit/mqtt-websocket-jdk16-android.git
216 | https://github.com/inventit/mqtt-websocket-jdk16-android
217 | scm:git:git@github.com:inventit/mqtt-websocket-jdk16-android.git
218 |
219 | apk
220 | mqtt-websocket-jdk16-android test Android app
221 |
222 | GitHub Issues
223 | https://github.com/inventit/mqtt-websocket-jdk16-android/issues
224 |
225 |
--------------------------------------------------------------------------------
/src/test/android/proguard.cfg:
--------------------------------------------------------------------------------
1 | -optimizationpasses 5
2 | -dontusemixedcaseclassnames
3 | -dontskipnonpubliclibraryclasses
4 | -dontpreverify
5 | -verbose
6 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
7 |
8 | -keep public class * extends android.app.Activity
9 | -keep public class * extends android.app.Application
10 | -keep public class * extends android.app.Service
11 | -keep public class * extends android.content.BroadcastReceiver
12 | -keep public class * extends android.content.ContentProvider
13 | -keep public class * extends android.app.backup.BackupAgentHelper
14 | -keep public class * extends android.preference.Preference
15 | -keep public class com.android.vending.licensing.ILicensingService
16 |
17 | -keepclasseswithmembernames class * {
18 | native ;
19 | }
20 |
21 | -keepclasseswithmembers class * {
22 | public (android.content.Context, android.util.AttributeSet);
23 | }
24 |
25 | -keepclasseswithmembers class * {
26 | public (android.content.Context, android.util.AttributeSet, int);
27 | }
28 |
29 | -keepclassmembers class * extends android.app.Activity {
30 | public void *(android.view.View);
31 | }
32 |
33 | -keepclassmembers enum * {
34 | public static **[] values();
35 | public static ** valueOf(java.lang.String);
36 | }
37 |
38 | -keep class * implements android.os.Parcelable {
39 | public static final android.os.Parcelable$Creator *;
40 | }
--------------------------------------------------------------------------------
/src/test/android/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system use,
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 |
10 | # Indicates whether an apk should be generated for each density.
11 | split.density=false
12 | java.encoding=UTF-8
13 | # Project target.
14 | target=android-16
15 | apk-configurations=
16 |
--------------------------------------------------------------------------------
/src/test/android/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/inventit/mqtt-websocket-jdk16-android/057d3b4d0b5653894ef3dcdae51af02369af19c9/src/test/android/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/src/test/android/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/inventit/mqtt-websocket-jdk16-android/057d3b4d0b5653894ef3dcdae51af02369af19c9/src/test/android/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/src/test/android/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/inventit/mqtt-websocket-jdk16-android/057d3b4d0b5653894ef3dcdae51af02369af19c9/src/test/android/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/src/test/android/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/inventit/mqtt-websocket-jdk16-android/057d3b4d0b5653894ef3dcdae51af02369af19c9/src/test/android/res/drawable/icon.png
--------------------------------------------------------------------------------
/src/test/android/res/layout/simple.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
21 |
22 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/test/android/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | mqtt-websocket-jdk16-android Test App
5 | mqtt-websocket-jdk16-android Test App
6 |
7 |
--------------------------------------------------------------------------------
/src/test/android/src/main/java/io/inventit/dev/android/testapp/mqtt/ws/MainApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.android.testapp.mqtt.ws;
5 |
6 | import io.inventit.dev.mqtt.paho.PahoConsoleLogger;
7 | import io.inventit.dev.mqtt.paho.jetty8.Jdk16MqttWebSocketAsyncClient;
8 |
9 | import org.eclipse.paho.client.mqttv3.IMqttActionListener;
10 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
11 | import org.eclipse.paho.client.mqttv3.IMqttToken;
12 | import org.eclipse.paho.client.mqttv3.MqttCallback;
13 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
14 | import org.eclipse.paho.client.mqttv3.MqttException;
15 | import org.eclipse.paho.client.mqttv3.MqttMessage;
16 | import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
17 | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
18 | import org.slf4j.Logger;
19 | import org.slf4j.LoggerFactory;
20 |
21 | import android.app.Activity;
22 | import android.os.Bundle;
23 | import android.view.View;
24 | import android.widget.ScrollView;
25 | import android.widget.TextView;
26 |
27 | public class MainApplication extends Activity implements MqttCallback {
28 |
29 | private static final Logger LOGGER = LoggerFactory
30 | .getLogger(MainApplication.class);
31 |
32 | /**
33 | * {@link ScrollView}
34 | */
35 | private ScrollView scrollView;
36 |
37 | /**
38 | * {@link TextView}
39 | */
40 | private TextView textView;
41 |
42 | private Jdk16MqttWebSocketAsyncClient client;
43 |
44 | @Override
45 | protected void onCreate(Bundle savedInstanceState) {
46 | LOGGER.info("Starting the activity...");
47 | super.onCreate(savedInstanceState);
48 | // Widgets
49 | setContentView(R.layout.simple);
50 | scrollView = (ScrollView) findViewById(R.id.scroll_view);
51 | textView = (TextView) findViewById(R.id.scroll_content);
52 |
53 | // MQTTT
54 | try {
55 | startTesting();
56 | } catch (MqttException e) {
57 | LOGGER.error("MQTT Error", e);
58 | throw new IllegalStateException(e);
59 | }
60 | }
61 |
62 | @Override
63 | protected void onDestroy() {
64 | super.onDestroy();
65 |
66 | if (client != null) {
67 | try {
68 | client.disconnect();
69 | } catch (MqttException e) {
70 | e.printStackTrace();
71 | }
72 | try {
73 | client.close();
74 | } catch (MqttException e) {
75 | e.printStackTrace();
76 | }
77 | client = null;
78 | }
79 | }
80 |
81 | protected void startTesting() throws MqttException {
82 | // MQTT over WebSocket (MQTT v3.1)
83 | LOGGER.info("Starting tests for MQTT over WebSocket (MQTT v3.1)...");
84 | PahoConsoleLogger.enableLog();
85 |
86 | // Hive MQ Public MQTT Server
87 | // (http://www.hivemq.com/showcase/public-mqtt-broker/) =>
88 | // broker.mqtt-dashboard.com:8000 (non-SSL)
89 | // Mosquitto (http://test.mosquitto.org/ws.html) =>
90 | // ws://test.mosquitto.org/mqtt (non-SSL)
91 | // Or whatever you want to test
92 |
93 | // final String uriString = "ws://broker.mqtt-dashboard.com:8000/mqtt";
94 | // final String uriString = "ws://test.mosquitto.org:8080/mqtt";
95 | final String uriString = "ws://localhosy/mqtt";
96 |
97 | // Credentials
98 | final String clientId = "IVI:" + System.currentTimeMillis();
99 |
100 | appendText("ClientID=>" + clientId + "\n");
101 |
102 | client = new Jdk16MqttWebSocketAsyncClient(uriString, clientId,
103 | new MemoryPersistence());
104 | final MqttConnectOptions options = new MqttConnectOptions();
105 | options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
106 | options.setCleanSession(true);
107 | client.connect(options, null, new IMqttActionListener() {
108 |
109 | @Override
110 | public void onSuccess(IMqttToken asyncActionToken) {
111 | final String topic = "/io/inventit/dev/android/testapp/android";
112 | try {
113 | client.subscribe(topic, 0);
114 | } catch (MqttException e) {
115 | onFailure(asyncActionToken, e);
116 | }
117 | LOGGER.info("Subscribing [{}]", topic);
118 | }
119 |
120 | @Override
121 | public void onFailure(IMqttToken asyncActionToken,
122 | Throwable exception) {
123 | connectionLost(exception);
124 | }
125 | });
126 | client.setCallback(this);
127 | LOGGER.info("Reday for tests...");
128 | }
129 |
130 | /**
131 | * When the image is tapped.
132 | *
133 | * @param view
134 | */
135 | public void publish(View view) {
136 | try {
137 | final String topic = "/io/inventit/dev/android/testapp/mqtt";
138 | final String message = "mqtt-websocket-test:"
139 | + System.currentTimeMillis();
140 | LOGGER.info("Publishing a message => {} to the topic[{}]", message);
141 | client.publish(topic, message.getBytes(), 0, false);
142 | appendText("Published:" + message + "\n");
143 | } catch (MqttPersistenceException e) {
144 | appendText("Failed to publish:" + e.getMessage() + "\n");
145 | e.printStackTrace();
146 | } catch (MqttException e) {
147 | appendText("Failed to publish:" + e.getMessage() + "\n");
148 | e.printStackTrace();
149 | }
150 | }
151 |
152 | @Override
153 | public void messageArrived(String topic, MqttMessage message)
154 | throws Exception {
155 | LOGGER.info("messageArrived => {}, {}", topic, message);
156 | appendText("messageArrived:" + topic + "/" + message + "\n");
157 | }
158 |
159 | @Override
160 | public void deliveryComplete(IMqttDeliveryToken token) {
161 | LOGGER.info("deliveryComplete => {}", token);
162 | appendText("deliveryComplete:" + token + "\n");
163 | }
164 |
165 | @Override
166 | public void connectionLost(Throwable cause) {
167 | LOGGER.info("connectionLost", cause);
168 | appendText("connectionLost:" + cause.getMessage() + "\n");
169 | cause.printStackTrace();
170 | }
171 |
172 | /**
173 | * Append a text to the textView widget
174 | *
175 | * @param line
176 | */
177 | protected void appendText(final String line) {
178 | runOnUiThread(new Runnable() {
179 | @Override
180 | public void run() {
181 | textView.append(line);
182 | scrollView.smoothScrollTo(0, textView.getBottom());
183 | }
184 | });
185 | }
186 |
187 | }
188 |
--------------------------------------------------------------------------------
/src/test/android/src/main/libs/org/eclipse/paho/org.eclipse.paho.client.mqttv3/1.0.0/org.eclipse.paho.client.mqttv3-1.0.0.pom:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | org.eclipse.paho
4 | org.eclipse.paho.client.mqttv3
5 | 1.0.0
6 | http://www.eclipse.org/paho/
7 | MQTT Java Client
8 | http://www.eclipse.org/legal/epl-v10.html
9 |
--------------------------------------------------------------------------------
/src/test/android/src/main/resources/android-logger.properties:
--------------------------------------------------------------------------------
1 | # Android Logger configuration
2 | # http://noveogroup.github.io/android-logger/
3 |
4 | # By default logger will print only ERROR (and higher) messages
5 | # with "MyApplication" tag
6 | root=ERROR:TEST_APP
7 |
8 | # DEBUG (and higher) messages from classes of com.example.database
9 | # will be logged with "MyApplication-Database" tag
10 | logger.io.inventit=VERBOSE:IVI
11 | logger.org.eclipse.jetty=VERBOSE:Jetty
12 |
--------------------------------------------------------------------------------
/src/test/java/io/inventit/dev/mqtt/paho/jetty8/Jdk16MqttWebSocketAsyncClientInContainerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.mqtt.paho.jetty8;
5 |
6 | import io.inventit.dev.mqtt.paho.PahoConsoleLogger;
7 |
8 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
9 | import org.eclipse.paho.client.mqttv3.IMqttToken;
10 | import org.eclipse.paho.client.mqttv3.MqttCallback;
11 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
12 | import org.eclipse.paho.client.mqttv3.MqttMessage;
13 | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
14 | import org.junit.Before;
15 | import org.junit.Test;
16 |
17 | /**
18 | * This test is skipped when building with maven.
19 | */
20 | public class Jdk16MqttWebSocketAsyncClientInContainerTest {
21 |
22 | @Before
23 | public void setUp() throws Exception {
24 | JettyStrErrLogUtils.enableLog();
25 | PahoConsoleLogger.enableLog();
26 | }
27 |
28 | @Test
29 | public void test() throws Exception {
30 | // Plain MQTT
31 | // final String uriString = "tcp://your-mqtt-broker:1883";
32 |
33 | // MQTT over WebSocket
34 | final String uriString = "wss://your-ws-broker/mqtt";
35 |
36 | // Credentials
37 | final String clientId = "123";
38 | final String userName = "";
39 | final String password = "";
40 |
41 | final Jdk16MqttWebSocketAsyncClient client = new Jdk16MqttWebSocketAsyncClient(
42 | uriString, clientId, new MemoryPersistence());
43 | final MqttConnectOptions options = new MqttConnectOptions();
44 | options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
45 | options.setCleanSession(true);
46 | options.setUserName(userName);
47 | options.setPassword(password.toCharArray());
48 | final IMqttToken token = client.connect(options);
49 | client.setCallback(new MqttCallback() {
50 |
51 | @Override
52 | public void messageArrived(String topic, MqttMessage message)
53 | throws Exception {
54 | System.out.println("messageArrived:" + topic + "/" + message);
55 | }
56 |
57 | @Override
58 | public void deliveryComplete(IMqttDeliveryToken token) {
59 | System.out.println("deliveryComplete:" + token);
60 | }
61 |
62 | @Override
63 | public void connectionLost(Throwable cause) {
64 | System.out.println("connectionLost");
65 | cause.printStackTrace();
66 | }
67 | });
68 |
69 | try {
70 | final long start = System.currentTimeMillis();
71 | while (client.isConnected() == false
72 | && System.currentTimeMillis() - start < 5000) {
73 | Thread.sleep(100);
74 | }
75 | System.out.println("OK!");
76 | } finally {
77 | System.out.println(token.isComplete());
78 | client.disconnect();
79 | client.close();
80 | }
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/src/test/java/io/inventit/dev/mqtt/paho/jetty8/Jdk16MqttWebSocketAsyncClientTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.mqtt.paho.jetty8;
5 |
6 | import static org.junit.Assert.*;
7 |
8 | import java.net.URI;
9 |
10 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
11 | import org.eclipse.paho.client.mqttv3.MqttException;
12 | import org.junit.Test;
13 |
14 | public class Jdk16MqttWebSocketAsyncClientTest {
15 |
16 | @Test
17 | public void test_newWebSocketNetworkModule() throws MqttException {
18 | final URI uri = URI.create("wss://your-ws-broker/mqtt");
19 | assertEquals(
20 | WebSocketNetworkModule.class,
21 | new Jdk16MqttWebSocketAsyncClient(uri.toString(), "clientId")
22 | .newWebSocketNetworkModule(uri, "mqtt",
23 | new MqttConnectOptions()).getClass());
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/io/inventit/dev/mqtt/paho/jetty8/JettyStrErrLogUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.mqtt.paho.jetty8;
5 |
6 | public class JettyStrErrLogUtils {
7 |
8 | private JettyStrErrLogUtils() {
9 | }
10 |
11 | public static void enableLog() {
12 | System.setProperty("org.eclipse.jetty.util.log.class",
13 | "org.eclipse.jetty.util.log.StrErrLog");
14 | System.setProperty("org.eclipse.jetty.LEVEL", "INFO");
15 | System.setProperty("org.eclipse.jetty.websocket.LEVEL", "DEBUG");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/test/java/io/inventit/dev/mqtt/paho/jetty8/WebSocketNetworkModuleInContainerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Inventit Inc.
3 | */
4 | package io.inventit.dev.mqtt.paho.jetty8;
5 |
6 | import java.net.URI;
7 |
8 | import org.junit.Test;
9 |
10 |
11 | public class WebSocketNetworkModuleInContainerTest {
12 |
13 | @Test
14 | public void test_connection() throws Exception {
15 | final String uriString = "ws://your.broker.com:80/mqtt";
16 | // final String uriString = "ws://test.mosquitto.org:8080/mqtt";
17 | final String clientId = "my-client-id";
18 |
19 | final WebSocketNetworkModule module = new WebSocketNetworkModule(
20 | URI.create(uriString), "mqttv3.1", clientId);
21 | module.start();
22 | System.out.println("OK!");
23 |
24 | module.getOutputStream().write("test".getBytes());
25 | module.getOutputStream().flush();
26 | module.stop();
27 | System.out.println("END");
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------