Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 # WebSocket and Socket.IO client for Android
3 A very simple bare-minimum WebSocket and Socket.IO client for Android.
5 ## Credits
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!
9 The hybi parser was ported from JavaScript to Java by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>.
11 The WebSocket client was written by [Eric Butler](https://twitter.com/codebutler) <eric@codebutler.com>.
13 The Socket.IO client was written by [Koushik Dutta](https://twitter.com/koush).
15 ## WebSocket Usage
17 ```java
18 List<BasicNameValuePair> extraHeaders = Arrays.asList(
19 new BasicNameValuePair("Cookie", "session=abcd")
20 );
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 }
28 @Override
29 public void onMessage(String message) {
30 Log.d(TAG, String.format("Got string message! %s", message));
31 }
33 @Override
34 public void onMessage(byte[] data) {
35 Log.d(TAG, String.format("Got binary message! %s", toHexString(data)));
36 }
38 @Override
39 public void onDisconnect(int code, String reason) {
40 Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason));
41 }
43 @Override
44 public void onError(Exception error) {
45 Log.e(TAG, "Error!", error);
46 }
47 }, extraHeaders);
49 client.connect();
51 // Later…
52 client.send("hello!");
53 client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF });
54 client.disconnect();
55 ```
57 ## Socket.IO Usage
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 }
66 @Override
67 public void on(String event, JSONArray arguments) {
68 Log.d(TAG, String.format("Got event %s: %s", event, arguments.toString()));
69 }
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 }
79 @Override
80 public void onMessage(String message) {
81 Log.d(TAG, String.format("Got message: %s", message));
82 }
84 @Override
85 public void onDisconnect(int code, String reason) {
86 Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason));
87 }
89 @Override
90 public void onError(Exception error) {
91 Log.e(TAG, "Error!", error);
92 }
93 });
95 client.connect();
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 ```
111 ## TODO
113 * Run [autobahn tests](http://autobahn.ws/testsuite)
114 * Investigate using [naga](http://code.google.com/p/naga/) instead of threads.
116 ## License
118 (The MIT License)
120 Copyright (c) 2009-2012 James Coglan
121 Copyright (c) 2012 Eric Butler
122 Copyright (c) 2012 Koushik Dutta
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:
131 The above copyright notice and this permission notice shall be included in all
132 copies or substantial portions of the Software.
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.