Skip to content

Commit 643b7f0

Browse files
committed
MQTT : add onUnsubscribe() event
1 parent c4c819f commit 643b7f0

5 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/javaforce/MQTT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ private void process(byte[] packet, int totalLength, int packetLength) throws Ex
596596
events.onSubscribe(null);
597597
}
598598
break;
599+
case CMD_UNSUBSCRIBE_ACK:
600+
//TODO : decode topic ?
601+
if (debug_msg) JFLog.log("subscribe_ack");
602+
if (events != null) {
603+
events.onUnsubscribe(null);
604+
}
605+
break;
599606
case CMD_PING:
600607
reply = new byte[2];
601608
reply[0] = (byte)(CMD_PONG << 4);
@@ -640,6 +647,7 @@ public void onDisconnect() {
640647
JFLog.log("disconnected");
641648
}
642649
public void onSubscribe(String topic) {}
650+
public void onUnsubscribe(String topic) {}
643651
public void onMessage(String topic, String msg) {
644652
JFLog.log("msg:" + topic + "=" + msg);
645653
resub = topic;

src/javaforce/MQTTEvents.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface MQTTEvents {
99
public void onConnect();
1010
public void onDisconnect();
1111
public void onSubscribe(String topic);
12+
public void onUnsubscribe(String topic);
1213
public void onMessage(String topic, String msg);
1314
}

src/javaforce/MQTTForward.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ public void onDisconnect() {
253253
public void onSubscribe(String topic) {
254254
}
255255

256+
/** MQTTEvents.onUnsubscribe() */
257+
public void onUnsubscribe(String topic) {
258+
}
259+
256260
/** MQTTEvents.onMessage() */
257261
public void onMessage(String topic, String msg) {
258262
}

src/javaforce/awt/MQTTViewer.form

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<EmptySpace max="-2" attributes="0"/>
3434
<Component id="topic" max="32767" attributes="0"/>
3535
<EmptySpace max="-2" attributes="0"/>
36+
<Component id="jButton1" min="-2" max="-2" attributes="0"/>
37+
<EmptySpace max="-2" attributes="0"/>
3638
<Component id="subscribe" min="-2" max="-2" attributes="0"/>
3739
</Group>
3840
<Group type="102" attributes="0">
@@ -94,7 +96,10 @@
9496
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
9597
<Component id="topic" alignment="3" min="-2" max="-2" attributes="0"/>
9698
</Group>
97-
<Component id="subscribe" alignment="1" min="-2" max="-2" attributes="0"/>
99+
<Group type="103" alignment="1" groupAlignment="3" attributes="0">
100+
<Component id="subscribe" alignment="3" min="-2" max="-2" attributes="0"/>
101+
<Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
102+
</Group>
98103
</Group>
99104
<EmptySpace max="-2" attributes="0"/>
100105
<Component id="jScrollPane1" pref="603" max="32767" attributes="0"/>
@@ -245,5 +250,13 @@
245250
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="subscribeActionPerformed"/>
246251
</Events>
247252
</Component>
253+
<Component class="javax.swing.JButton" name="jButton1">
254+
<Properties>
255+
<Property name="text" type="java.lang.String" value="Unsubscribe"/>
256+
</Properties>
257+
<Events>
258+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
259+
</Events>
260+
</Component>
248261
</SubComponents>
249262
</Form>

src/javaforce/awt/MQTTViewer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private void initComponents() {
7878
send_msg = new javax.swing.JTextField();
7979
send = new javax.swing.JButton();
8080
subscribe = new javax.swing.JButton();
81+
jButton1 = new javax.swing.JButton();
8182

8283
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
8384
setTitle("MQTT Viewer");
@@ -146,6 +147,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
146147
}
147148
});
148149

150+
jButton1.setText("Unsubscribe");
151+
jButton1.addActionListener(new java.awt.event.ActionListener() {
152+
public void actionPerformed(java.awt.event.ActionEvent evt) {
153+
jButton1ActionPerformed(evt);
154+
}
155+
});
156+
149157
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
150158
getContentPane().setLayout(layout);
151159
layout.setHorizontalGroup(
@@ -159,6 +167,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
159167
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
160168
.addComponent(topic)
161169
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
170+
.addComponent(jButton1)
171+
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
162172
.addComponent(subscribe))
163173
.addGroup(layout.createSequentialGroup()
164174
.addComponent(jLabel1)
@@ -212,7 +222,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
212222
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
213223
.addComponent(jLabel2)
214224
.addComponent(topic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
215-
.addComponent(subscribe, javax.swing.GroupLayout.Alignment.TRAILING))
225+
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
226+
.addComponent(subscribe)
227+
.addComponent(jButton1)))
216228
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
217229
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
218230
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
@@ -244,6 +256,10 @@ private void subscribeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
244256
subscribe();
245257
}//GEN-LAST:event_subscribeActionPerformed
246258

259+
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
260+
unsubscribe();
261+
}//GEN-LAST:event_jButton1ActionPerformed
262+
247263
/**
248264
* @param args the command line arguments
249265
*/
@@ -260,6 +276,7 @@ public void run() {
260276
// Variables declaration - do not modify//GEN-BEGIN:variables
261277
private javax.swing.JCheckBox authenticate;
262278
private javax.swing.JButton clear;
279+
private javax.swing.JButton jButton1;
263280
private javax.swing.JLabel jLabel1;
264281
private javax.swing.JLabel jLabel2;
265282
private javax.swing.JLabel jLabel3;
@@ -305,6 +322,10 @@ public void onSubscribe(String topic) {
305322
addText("subscribe accepted!\r\n");
306323
}
307324

325+
public void onUnsubscribe(String topic) {
326+
addText("unsubscribe accepted!\r\n");
327+
}
328+
308329
public void onMessage(String topic, String msg) {
309330
String ln = topic + "=" + msg + "\r\n";
310331
addText(ln);
@@ -378,6 +399,13 @@ public void subscribe() {
378399
addText("Subscribed to : " + _topic + "\r\n");
379400
}
380401

402+
public void unsubscribe() {
403+
if (client == null) return;
404+
String _topic = topic.getText().trim();
405+
client.unsubscribe(_topic);
406+
addText("Unsubscribed to : " + _topic + "\r\n");
407+
}
408+
381409
private void setState(boolean state) {
382410
server.setEditable(state);
383411
authenticate.setEnabled(state);

0 commit comments

Comments
 (0)