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