forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
82 lines (68 loc) · 2.47 KB
/
Client.java
File metadata and controls
82 lines (68 loc) · 2.47 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
package examples;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import pubnub.Callback;
import pubnub.Pubnub;
public class Client {
public static void main(String [] params) {
Pubnub pn = new Pubnub( "demo", "demo", "demo", "", true );// (Cipher key is Optional)
Receiver rcv = new Receiver();
System.out.println("Subscribed to 'hello_world' Channel ");
HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", "hello_world");
args.put("callback", rcv);
pn.subscribe( args );
System.out.println("done");
}
}
class Receiver implements Callback {
@Override
public boolean subscribeCallback(String channel, Object message) {
// Print Received Message
//System.out.println(message);
try {
try {
if (message instanceof JSONObject) {
JSONObject obj = (JSONObject) message;
@SuppressWarnings("rawtypes")
Iterator keys = obj.keys();
while (keys.hasNext()) {
System.out.print(obj.get(keys.next().toString()) + " ");
}
System.out.println();
} else if (message instanceof String) {
String obj = (String) message;
System.out.print(obj + " ");
System.out.println();
} else if (message instanceof JSONArray) {
JSONArray obj = (JSONArray) message;
System.out.print(obj.toString() + " ");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
@Override
public void errorCallback(String channel, Object message) {
System.err.println("Channel:" + channel + "-" + message.toString());
}
@Override
public void connectCallback(String channel) {
System.out.println("Connected to channel :" + channel);
}
@Override
public void reconnectCallback(String channel) {
System.out.println("Reconnected to channel :" + channel);
}
@Override
public void disconnectCallback(String channel) {
System.out.println("Disconnected to channel :" + channel);
}
}