|
4 | 4 |
|
5 | 5 | import com.pusher.client.Pusher; |
6 | 6 | import com.pusher.client.PusherOptions; |
| 7 | +import com.pusher.client.channel.PrivateChannelEventListener; |
7 | 8 | import com.pusher.client.channel.PusherEvent; |
8 | 9 | import com.pusher.client.channel.PresenceChannel; |
9 | 10 | import com.pusher.client.channel.PresenceChannelEventListener; |
|
12 | 13 | import com.pusher.client.connection.ConnectionStateChange; |
13 | 14 | import com.pusher.client.util.HttpAuthorizer; |
14 | 15 |
|
15 | | -public class PresenceChannelExampleApp implements ConnectionEventListener, PresenceChannelEventListener { |
| 16 | +/* |
| 17 | +This app demonstrates how to use Presence Channels. |
16 | 18 |
|
17 | | - private final Pusher pusher; |
18 | | - private final String channelName; |
19 | | - private final String eventName; |
| 19 | +Please ensure you update this relevant parts below with your Pusher credentials before running. |
| 20 | +Your Pusher credentials can be found at https://dashboard.pusher.com, selecting the channels project, |
| 21 | +and visiting the App Keys tab. |
| 22 | +
|
| 23 | +A demonstration authorization endpoint using nodejs can be found |
| 24 | +https://github.com/pusher/pusher-channels-auth-example |
| 25 | +
|
| 26 | +For more information on private encrypted channels please read |
| 27 | +https://pusher.com/docs/channels/using_channels/presence-channels |
| 28 | +*/ |
| 29 | +public class PresenceChannelExampleApp { |
| 30 | + |
| 31 | + // make sure the following variables are configured for your instance: |
| 32 | + private String channelsKey = "FILL_ME_IN"; |
| 33 | + private String channelName = "my-channel"; |
| 34 | + private String eventName = "my-event"; |
| 35 | + private String cluster = "eu"; |
| 36 | + private String authorizationEndpoint = "http://localhost:3030/pusher/auth"; |
20 | 37 |
|
21 | 38 | private final PresenceChannel channel; |
22 | 39 |
|
23 | 40 | public static void main(final String[] args) { |
24 | 41 | new PresenceChannelExampleApp(args); |
25 | 42 | } |
26 | 43 |
|
27 | | - public PresenceChannelExampleApp(final String[] args) { |
28 | | - |
29 | | - final String apiKey = args.length > 0 ? args[0] : "a87fe72c6f36272aa4b1"; |
30 | | - channelName = args.length > 1 ? args[1] : "presence-my-channel"; |
31 | | - eventName = args.length > 2 ? args[2] : "my-event"; |
32 | | - |
33 | | - final HttpAuthorizer authorizer = new HttpAuthorizer( |
34 | | - "http://www.leggetter.co.uk/pusher/pusher-examples/php/authentication/src/presence_auth.php"); |
35 | | - final PusherOptions options = new PusherOptions().setAuthorizer(authorizer).setEncrypted(true); |
36 | | - |
37 | | - pusher = new Pusher(apiKey, options); |
38 | | - pusher.connect(this); |
| 44 | + private PresenceChannelExampleApp(final String[] args) { |
39 | 45 |
|
40 | | - channel = pusher.subscribePresence(channelName, this, eventName); |
41 | | - |
42 | | - // Keep main thread asleep while we watch for events or application will |
43 | | - // terminate |
44 | | - while (true) { |
45 | | - try { |
46 | | - Thread.sleep(1000); |
47 | | - } |
48 | | - catch (final InterruptedException e) { |
49 | | - e.printStackTrace(); |
50 | | - } |
| 46 | + // if using from the command line, these variables need to be passed |
| 47 | + switch (args.length) { |
| 48 | + case 4: cluster = args[3]; |
| 49 | + case 3: eventName = args[2]; |
| 50 | + case 2: channelName = args[1]; |
| 51 | + case 1: channelsKey = args[0]; |
51 | 52 | } |
52 | | - } |
53 | | - |
54 | | - /* ConnectionEventListener implementation */ |
55 | | - |
56 | | - @Override |
57 | | - public void onConnectionStateChange(final ConnectionStateChange change) { |
58 | | - |
59 | | - System.out.println(String.format("Connection state changed from [%s] to [%s]", change.getPreviousState(), |
60 | | - change.getCurrentState())); |
61 | | - } |
62 | | - |
63 | | - @Override |
64 | | - public void onError(final String message, final String code, final Exception e) { |
65 | | - |
66 | | - System.out.println(String.format("An error was received with message [%s], code [%s], exception [%s]", message, |
67 | | - code, e)); |
68 | | - } |
69 | | - |
70 | | - /* PresenceChannelEventListener implementation */ |
71 | | - |
72 | | - @Override |
73 | | - public void onUsersInformationReceived(final String channelName, final Set<User> users) { |
74 | 53 |
|
75 | | - System.out.println("Received user information"); |
76 | | - |
77 | | - printCurrentlySubscribedUsers(); |
78 | | - } |
79 | | - |
80 | | - @Override |
81 | | - public void userSubscribed(final String channelName, final User user) { |
| 54 | + // create a HttpAuthorizer that points to your authorization server |
| 55 | + final HttpAuthorizer authorizer = new HttpAuthorizer(authorizationEndpoint); |
| 56 | + |
| 57 | + // configure your Pusher connection with the options you want |
| 58 | + final PusherOptions options = new PusherOptions() |
| 59 | + .setEncrypted(true) |
| 60 | + .setCluster(cluster) |
| 61 | + .setAuthorizer(authorizer); |
| 62 | + Pusher pusher = new Pusher(channelsKey, options); |
| 63 | + |
| 64 | + // set up a ConnectionEventListener to listen for connection changes to Pusher |
| 65 | + ConnectionEventListener connectionEventListener = new ConnectionEventListener() { |
| 66 | + @Override |
| 67 | + public void onConnectionStateChange(ConnectionStateChange change) { |
| 68 | + System.out.println(String.format("Connection state changed from [%s] to [%s]", |
| 69 | + change.getPreviousState(), change.getCurrentState())); |
| 70 | + } |
82 | 71 |
|
83 | | - System.out.println(String.format("A new user has joined channel [%s]: %s", channelName, user.toString())); |
| 72 | + @Override |
| 73 | + public void onError(String message, String code, Exception e) { |
| 74 | + System.out.println(String.format("An error was received with message [%s], code [%s], exception [%s]", |
| 75 | + message, code, e)); |
| 76 | + } |
| 77 | + }; |
84 | 78 |
|
85 | | - printCurrentlySubscribedUsers(); |
86 | | - } |
| 79 | + // connect to Pusher |
| 80 | + pusher.connect(connectionEventListener); |
87 | 81 |
|
88 | | - @Override |
89 | | - public void userUnsubscribed(final String channelName, final User user) { |
| 82 | + // set up a PresenceChannelEventListener to listen for messages to the channel and event we are interested in |
| 83 | + PresenceChannelEventListener presenceChannelEventListener = new PresenceChannelEventListener() { |
| 84 | + @Override |
| 85 | + public void onSubscriptionSucceeded(String channelName) { |
| 86 | + System.out.println(String.format( |
| 87 | + "Subscription to channel [%s] succeeded", channelName)); |
| 88 | + } |
90 | 89 |
|
91 | | - System.out.println(String.format("A user has left channel [%s]: %s", channelName, user)); |
| 90 | + @Override |
| 91 | + public void onEvent(PusherEvent event) { |
| 92 | + System.out.println(String.format( |
| 93 | + "Received event [%s]", event.toString())); |
| 94 | + } |
92 | 95 |
|
93 | | - printCurrentlySubscribedUsers(); |
94 | | - } |
| 96 | + @Override |
| 97 | + public void onAuthenticationFailure(String message, Exception e) { |
| 98 | + System.out.println(String.format( |
| 99 | + "Authentication failure due to [%s], exception was [%s]", message, e)); |
| 100 | + } |
95 | 101 |
|
96 | | - @Override |
97 | | - public void onEvent(final PusherEvent event) { |
| 102 | + @Override |
| 103 | + public void onUsersInformationReceived(String channelName, Set<User> users) { |
| 104 | + System.out.println("Received user information"); |
| 105 | + printCurrentlySubscribedUsers(); |
| 106 | + } |
98 | 107 |
|
99 | | - System.out.println(String.format("Received event [%s]", event.toString())); |
100 | | - } |
| 108 | + @Override |
| 109 | + public void userSubscribed(String channelName, User user) { |
| 110 | + System.out.println(String.format("A new user has joined channel [%s]: %s", channelName, user.toString())); |
| 111 | + printCurrentlySubscribedUsers(); |
| 112 | + } |
101 | 113 |
|
102 | | - @Override |
103 | | - public void onSubscriptionSucceeded(final String channelName) { |
| 114 | + @Override |
| 115 | + public void userUnsubscribed(String channelName, User user) { |
| 116 | + System.out.println(String.format("A user has left channel [%s]: %s", channelName, user)); |
| 117 | + printCurrentlySubscribedUsers(); |
| 118 | + } |
| 119 | + }; |
104 | 120 |
|
105 | | - System.out.println(String.format("Subscription to channel [%s] succeeded", channel.getName())); |
106 | | - } |
| 121 | + // subscribe to the channel and with the event listener for the event name |
| 122 | + channel = pusher.subscribePresence(channelName, presenceChannelEventListener, eventName); |
107 | 123 |
|
108 | | - @Override |
109 | | - public void onAuthenticationFailure(final String message, final Exception e) { |
110 | 124 |
|
111 | | - System.out.println(String.format("Authentication failure due to [%s], exception was [%s]", message, e)); |
| 125 | + // Keep main thread asleep while we watch for events or application will terminate |
| 126 | + while (true) { |
| 127 | + try { |
| 128 | + Thread.sleep(1000); |
| 129 | + } |
| 130 | + catch (final InterruptedException e) { |
| 131 | + e.printStackTrace(); |
| 132 | + } |
| 133 | + } |
112 | 134 | } |
113 | 135 |
|
114 | 136 | private void printCurrentlySubscribedUsers() { |
115 | 137 | final StringBuilder sb = new StringBuilder("Users now subscribed to the channel:"); |
116 | | - for (final User remainingUser : channel.getUsers()) { |
| 138 | + for (final User user : channel.getUsers()) { |
117 | 139 | sb.append("\n\t"); |
118 | | - sb.append(remainingUser.toString()); |
| 140 | + sb.append(user.toString()); |
119 | 141 |
|
120 | | - if (remainingUser.equals(channel.getMe())) { |
| 142 | + if (user.equals(channel.getMe())) { |
121 | 143 | sb.append(" (me)"); |
122 | 144 | } |
123 | 145 | } |
|
0 commit comments