forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallbackExample.java
More file actions
100 lines (72 loc) · 2.86 KB
/
Copy pathCallbackExample.java
File metadata and controls
100 lines (72 loc) · 2.86 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
package examples;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import pubnub.Callback;
import pubnub.Pubnub;
class CallbackExample {
public static void main(String args[]) {
String publish_key = "demo", subscribe_key = "demo", secret_key = "", cipher_key = "";
Boolean ssl_on = false;
Pubnub pubnub_user_supplied_options = new Pubnub(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
CallbackExample pubnub = new CallbackExample();
pubnub.unitTest(pubnub_user_supplied_options);
}
String message = " ~`!@#$%^&*()+=[]\\{}|;\':,./<>?abcd עברית";
Pubnub _pubnub;
ArrayList<String> many_channels = null;
LinkedHashMap<String, Object> status = null;
LinkedHashMap<String, Object> threads = null;
int max_retries = 10;
private void unitTest(Pubnub pubnub) {
many_channels = new ArrayList<String>();
status = new LinkedHashMap<String, Object>();
threads = new LinkedHashMap<String, Object>();
_pubnub = pubnub;
status.put("sent", 0);
status.put("received", 0);
status.put("connections", 0);
final String _channel = "callback_test";
Thread t = new Thread() {
public void run() {
HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", _channel);
args.put("callback", new CallbackExample.CallbackImplementation()); // callback to get response
System.out.println("Subscribing to channel: " + args.get("channel"));
_pubnub.subscribe(args);
}
};
t.start();
threads.put(_channel, t);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Callback Interface when a Message is Received
class CallbackImplementation implements Callback {
@Override
public boolean subscribeCallback(String channel, Object message) {
System.out.println("Receiving message: " + message.toString() + " on channel: " + channel);
return true;
}
@Override
public void errorCallback(String channel, Object message) {
System.out.println("Error received on channel " + channel + " " + message.toString());
}
@Override
public void connectCallback(String channel) {
System.out.println("Connected to channel: " + channel);
return;
}
@Override
public void reconnectCallback(String channel) {
System.out.println("Reconnecting to channel: " + channel);
}
@Override
public void disconnectCallback(String channel) {
System.out.println("Disconnected from channel: " + channel);
}
}
}