forked from yunba/yunba-socket.io-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicExample.java
More file actions
107 lines (89 loc) · 2.72 KB
/
Copy pathBasicExample.java
File metadata and controls
107 lines (89 loc) · 2.72 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package basic;
/*
* socket.io-java-client Test.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
*/
import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.Exception;
import java.lang.Object;
import java.lang.System;
public class BasicExample implements IOCallback {
private SocketIO socket;
/* 从 yunba.io 获取应用的 appkey */
private static String APPKEY = "52fcc04c4dc903d66d6f8f92";
/**
* @param args
*/
public static void main(String[] args) {
try {
new BasicExample();
} catch (Exception e) {
e.printStackTrace();
}
}
public BasicExample() throws Exception {
socket = new SocketIO();
socket.connect("http://sock.yunba.io:3000/", this);
}
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
try {
if (event.equals("socketconnectack")) {
onSocketConnectAck();
} else if (event.equals("connack")) {
onConnAck((JSONObject)args[0]);
} else if (event.equals("puback")) {
onPubAck((JSONObject)args[0]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void onSocketConnectAck() throws Exception {
System.out.println("onSocketConnectAck");
// emit connect
socket.emit("connect", new JSONObject("{'appkey': '" + APPKEY + "'}"));
}
public void onConnAck(JSONObject json) throws Exception {
System.out.println("onConnAck success " + json.get("success"));
socket.emit("publish", new JSONObject("{'topic': 't1', 'msg': 'hello form java socket.io client', 'qos': 1}"));
}
public void onPubAck(JSONObject json) throws Exception {
System.out.println("conPubAck success " + json.get("success") + " msg id " + json.get("messageId"));
}
}