Skip to content

Commit 978e824

Browse files
committed
Added pusher (disabled for now)
1 parent a3ccb45 commit 978e824

14 files changed

+1209
-2
lines changed

.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="gen"/>
55
<classpathentry kind="src" path="src-zxing-android"/>
6+
<classpathentry kind="src" path="src-javapusherclient"/>
67
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
78
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
89
<classpathentry kind="output" path="bin/classes"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.justinschultz.examples;
2+
3+
/*
4+
* Copyright (C) 2012 Justin Schultz
5+
* JavaPusherClient, a Pusher (http://pusherapp.com) client for Java
6+
*
7+
* http://justinschultz.com/
8+
* http://publicstaticdroidmain.com/
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
import org.json.JSONObject;
24+
25+
import com.justinschultz.pusherclient.ChannelListener;
26+
import com.justinschultz.pusherclient.Pusher;
27+
import com.justinschultz.pusherclient.PusherListener;
28+
import com.justinschultz.pusherclient.Pusher.Channel;
29+
30+
public class PusherTest {
31+
private static final String PUSHER_API_KEY = "80bbbe17a2e65338705a";
32+
private static final String PUSHER_CHANNEL = "test-channel";
33+
private static Pusher pusher;
34+
35+
public static void main(String[] args) {
36+
PusherListener eventListener = new PusherListener() {
37+
Channel channel;
38+
39+
@Override
40+
public void onConnect(String socketId) {
41+
System.out.println("Pusher connected. Socket Id is: " + socketId);
42+
channel = pusher.subscribe(PUSHER_CHANNEL);
43+
System.out.println("Subscribed to channel: " + channel);
44+
channel.send("client-event-test", new JSONObject());
45+
46+
channel.bind("price-updated", new ChannelListener() {
47+
@Override
48+
public void onMessage(String message) {
49+
System.out.println("Received bound channel message: " + message);
50+
}
51+
});
52+
}
53+
54+
@Override
55+
public void onMessage(String message) {
56+
System.out.println("Received message from Pusher: " + message);
57+
}
58+
59+
@Override
60+
public void onDisconnect() {
61+
System.out.println("Pusher disconnected.");
62+
}
63+
};
64+
65+
pusher = new Pusher(PUSHER_API_KEY);
66+
pusher.setPusherListener(eventListener);
67+
pusher.connect();
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.justinschultz.pusherclient;
2+
3+
/*
4+
* Copyright (C) 2012 Justin Schultz
5+
* JavaPusherClient, a Pusher (http://pusherapp.com) client for Java
6+
*
7+
* http://justinschultz.com/
8+
* http://publicstaticdroidmain.com/
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
public interface ChannelListener {
24+
public void onMessage(String message);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package com.justinschultz.pusherclient;
2+
3+
/*
4+
* Copyright (C) 2012 Justin Schultz
5+
* JavaPusherClient, a Pusher (http://pusherapp.com) client for Java
6+
*
7+
* http://justinschultz.com/
8+
* http://publicstaticdroidmain.com/
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
import java.net.URI;
24+
import java.util.HashMap;
25+
26+
import org.json.JSONObject;
27+
28+
import com.justinschultz.websocket.WebSocket;
29+
import com.justinschultz.websocket.WebSocketConnection;
30+
import com.justinschultz.websocket.WebSocketEventHandler;
31+
import com.justinschultz.websocket.WebSocketMessage;
32+
33+
public class Pusher {
34+
private static final String PUSHER_CLIENT = "java-android-client";
35+
private final String VERSION = "1.11";
36+
private final String HOST = "ws.pusherapp.com";
37+
private final int WS_PORT = 80;
38+
private final String PREFIX = "ws://";
39+
40+
private WebSocket webSocket;
41+
private String apiKey;
42+
private final HashMap<String, Channel> channels;
43+
44+
private PusherListener pusherEventListener;
45+
46+
public Pusher(String key) {
47+
apiKey = key;
48+
channels = new HashMap<String, Channel>();
49+
}
50+
51+
public void connect() {
52+
String path = "/app/" + apiKey + "?client=" + PUSHER_CLIENT + "&version=" + VERSION;
53+
54+
try {
55+
URI url = new URI(PREFIX + HOST + ":" + WS_PORT + path);
56+
webSocket = new WebSocketConnection(url);
57+
webSocket.setEventHandler(new WebSocketEventHandler() {
58+
@Override
59+
public void onOpen() {
60+
// Pusher's onOpen is invoked after we've received a
61+
// socket_id in onMessage()
62+
}
63+
64+
@Override
65+
public void onMessage(WebSocketMessage message) {
66+
try {
67+
JSONObject jsonMessage = new JSONObject(message.getText());
68+
String event = jsonMessage.optString("event", null);
69+
70+
if(event.equals("pusher:connection_established" ))
71+
{
72+
JSONObject data = new JSONObject(jsonMessage.getString("data"));
73+
pusherEventListener.onConnect(data.getString("socket_id"));
74+
} else {
75+
pusherEventListener.onMessage(jsonMessage.toString());
76+
dispatchChannelEvent(jsonMessage, event);
77+
}
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
83+
@Override
84+
public void onClose() {
85+
pusherEventListener.onDisconnect();
86+
}
87+
});
88+
89+
webSocket.connect();
90+
91+
} catch (Exception e) {
92+
e.printStackTrace();
93+
}
94+
}
95+
96+
public void disconnect() {
97+
try {
98+
webSocket.close();
99+
} catch (Exception e) {
100+
e.printStackTrace();
101+
}
102+
}
103+
104+
public boolean isConnected() {
105+
return (webSocket != null && webSocket.isConnected());
106+
}
107+
108+
public void setPusherListener(PusherListener listener) {
109+
pusherEventListener = listener;
110+
}
111+
112+
public Channel subscribe(String channelName) {
113+
Channel c = new Channel(channelName);
114+
115+
if (webSocket != null && webSocket.isConnected()) {
116+
try {
117+
sendSubscribeMessage(c);
118+
} catch (Exception e) {
119+
e.printStackTrace();
120+
}
121+
}
122+
123+
channels.put(channelName, c);
124+
return c;
125+
}
126+
127+
public Channel subscribe(String channelName, String authToken) {
128+
Channel c = new Channel(channelName);
129+
130+
if (webSocket != null && webSocket.isConnected()) {
131+
try {
132+
sendSubscribeMessage(c, authToken);
133+
} catch (Exception e) {
134+
e.printStackTrace();
135+
}
136+
}
137+
138+
channels.put(channelName, c);
139+
return c;
140+
}
141+
142+
public Channel subscribe(String channelName, String authToken, int userId) {
143+
Channel c = new Channel(channelName);
144+
145+
if (webSocket != null && webSocket.isConnected()) {
146+
try {
147+
sendSubscribeMessage(c, authToken, userId);
148+
} catch (Exception e) {
149+
e.printStackTrace();
150+
}
151+
}
152+
153+
channels.put(channelName, c);
154+
return c;
155+
}
156+
157+
public void unsubscribe(String channelName) {
158+
if (channels.containsKey(channelName)) {
159+
if (webSocket != null && webSocket.isConnected()) {
160+
try {
161+
sendUnsubscribeMessage(channels.get(channelName));
162+
} catch (Exception e) {
163+
e.printStackTrace();
164+
}
165+
}
166+
167+
channels.remove(channelName);
168+
}
169+
}
170+
171+
private void sendSubscribeMessage(Channel c) {
172+
JSONObject data = new JSONObject();
173+
c.send("pusher:subscribe", data);
174+
}
175+
176+
private void sendSubscribeMessage(Channel c, String authToken) {
177+
JSONObject data = new JSONObject();
178+
try {
179+
data.put("auth", authToken);
180+
} catch(Exception ex) {
181+
182+
}
183+
184+
c.send("pusher:subscribe", data);
185+
}
186+
187+
private void sendSubscribeMessage(Channel c, String authToken, int userId) {
188+
JSONObject data = new JSONObject();
189+
try {
190+
data.put("auth", authToken);
191+
data.put("channel_data", new JSONObject().put("user_id", userId));
192+
} catch(Exception ex) {
193+
194+
}
195+
196+
c.send("pusher:subscribe", data);
197+
}
198+
199+
private void sendUnsubscribeMessage(Channel c) {
200+
JSONObject data = new JSONObject();
201+
c.send("pusher:unsubscribe", data);
202+
}
203+
204+
private void dispatchChannelEvent(JSONObject jsonMessage, String event) {
205+
String channelName = jsonMessage.optString("channel", null);
206+
207+
Channel channel = channels.get(channelName);
208+
if(channel != null) {
209+
ChannelListener channelListener = channel.channelEvents.get(event);
210+
211+
if(channelListener != null)
212+
channelListener.onMessage(jsonMessage.toString());
213+
}
214+
}
215+
216+
public void send(String event_name, JSONObject data) {
217+
JSONObject message = new JSONObject();
218+
219+
try {
220+
message.put("event", event_name);
221+
message.put("data", data);
222+
webSocket.send(message.toString());
223+
} catch (Exception e) {
224+
e.printStackTrace();
225+
}
226+
}
227+
228+
public class Channel {
229+
private String channelName;
230+
private final HashMap<String, ChannelListener> channelEvents;
231+
232+
public Channel(String _name) {
233+
channelName = _name;
234+
channelEvents = new HashMap<String, ChannelListener>();
235+
}
236+
237+
public void send(String eventName, JSONObject data) {
238+
JSONObject message = new JSONObject();
239+
240+
try {
241+
data.put("channel", channelName);
242+
message.put("event", eventName);
243+
message.put("data", data);
244+
webSocket.send(message.toString());
245+
} catch (Exception e) {
246+
e.printStackTrace();
247+
}
248+
}
249+
250+
public void bind(String eventName, ChannelListener channelListener) {
251+
channelEvents.put(eventName, channelListener);
252+
}
253+
254+
@Override
255+
public String toString() {
256+
return channelName;
257+
}
258+
}
259+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.justinschultz.pusherclient;
2+
3+
/*
4+
* Copyright (C) 2012 Justin Schultz
5+
* JavaPusherClient, a Pusher (http://pusherapp.com) client for Java
6+
*
7+
* http://justinschultz.com/
8+
* http://publicstaticdroidmain.com/
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
public interface PusherListener {
24+
public void onConnect(String socketId);
25+
public void onMessage(String message);
26+
public void onDisconnect();
27+
}

0 commit comments

Comments
 (0)