|
1 # WebSocket and Socket.IO client for Android |
|
2 |
|
3 A very simple bare-minimum WebSocket and Socket.IO client for Android. |
|
4 |
|
5 ## Credits |
|
6 |
|
7 The hybi parser is based on code from the [faye project](https://github.com/faye/faye-websocket-node). Faye is Copyright (c) 2009-2012 James Coglan. Many thanks for the great open-source library! |
|
8 |
|
9 The hybi parser was ported from JavaScript to Java by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>. |
|
10 |
|
11 The WebSocket client was written by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>. |
|
12 |
|
13 The Socket.IO client was written by [Koushik Dutta](https://twitter.com/koush). |
|
14 |
|
15 ## WebSocket Usage |
|
16 |
|
17 ```java |
|
18 List<BasicNameValuePair> extraHeaders = Arrays.asList( |
|
19 new BasicNameValuePair("Cookie", "session=abcd") |
|
20 ); |
|
21 |
|
22 WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Listener() { |
|
23 @Override |
|
24 public void onConnect() { |
|
25 Log.d(TAG, "Connected!"); |
|
26 } |
|
27 |
|
28 @Override |
|
29 public void onMessage(String message) { |
|
30 Log.d(TAG, String.format("Got string message! %s", message)); |
|
31 } |
|
32 |
|
33 @Override |
|
34 public void onMessage(byte[] data) { |
|
35 Log.d(TAG, String.format("Got binary message! %s", toHexString(data))); |
|
36 } |
|
37 |
|
38 @Override |
|
39 public void onDisconnect(int code, String reason) { |
|
40 Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason)); |
|
41 } |
|
42 |
|
43 @Override |
|
44 public void onError(Exception error) { |
|
45 Log.e(TAG, "Error!", error); |
|
46 } |
|
47 }, extraHeaders); |
|
48 |
|
49 client.connect(); |
|
50 |
|
51 // Later… |
|
52 client.send("hello!"); |
|
53 client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF }); |
|
54 client.disconnect(); |
|
55 ``` |
|
56 |
|
57 ## Socket.IO Usage |
|
58 |
|
59 ```java |
|
60 SocketIOClient client = new SocketIOClient(URI.create("wss://example.com"), new SocketIOClient.Handler() { |
|
61 @Override |
|
62 public void onConnect() { |
|
63 Log.d(TAG, "Connected!"); |
|
64 } |
|
65 |
|
66 @Override |
|
67 public void on(String event, JSONArray arguments) { |
|
68 Log.d(TAG, String.format("Got event %s: %s", event, arguments.toString())); |
|
69 } |
|
70 |
|
71 @Override |
|
72 public void onJSON(JSONObject json) { |
|
73 try { |
|
74 Log.d(TAG, String.format("Got JSON Object: %s", json.toString())); |
|
75 } catch(JSONException e) { |
|
76 } |
|
77 } |
|
78 |
|
79 @Override |
|
80 public void onMessage(String message) { |
|
81 Log.d(TAG, String.format("Got message: %s", message)); |
|
82 } |
|
83 |
|
84 @Override |
|
85 public void onDisconnect(int code, String reason) { |
|
86 Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason)); |
|
87 } |
|
88 |
|
89 @Override |
|
90 public void onError(Exception error) { |
|
91 Log.e(TAG, "Error!", error); |
|
92 } |
|
93 }); |
|
94 |
|
95 client.connect(); |
|
96 |
|
97 // Later… |
|
98 client.emit("Message"); //Message |
|
99 JSONArray arguments = new JSONArray(); |
|
100 arguments.put("first argument"); |
|
101 JSONObject second = new JSONObject(); |
|
102 second.put("dictionary", true); |
|
103 client.emit(second); //JSON Message |
|
104 arguments.put(second); |
|
105 client.emit("hello", arguments); //Event |
|
106 client.disconnect(); |
|
107 ``` |
|
108 |
|
109 |
|
110 |
|
111 ## TODO |
|
112 |
|
113 * Run [autobahn tests](http://autobahn.ws/testsuite) |
|
114 * Investigate using [naga](http://code.google.com/p/naga/) instead of threads. |
|
115 |
|
116 ## License |
|
117 |
|
118 (The MIT License) |
|
119 |
|
120 Copyright (c) 2009-2012 James Coglan |
|
121 Copyright (c) 2012 Eric Butler |
|
122 Copyright (c) 2012 Koushik Dutta |
|
123 |
|
124 Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
125 this software and associated documentation files (the 'Software'), to deal in |
|
126 the Software without restriction, including without limitation the rights to use, |
|
127 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
128 Software, and to permit persons to whom the Software is furnished to do so, |
|
129 subject to the following conditions: |
|
130 |
|
131 The above copyright notice and this permission notice shall be included in all |
|
132 copies or substantial portions of the Software. |
|
133 |
|
134 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
135 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
136 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
137 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
138 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
139 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
140 |