michael@0: # WebSocket and Socket.IO client for Android michael@0: michael@0: A very simple bare-minimum WebSocket and Socket.IO client for Android. michael@0: michael@0: ## Credits michael@0: michael@0: 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: michael@0: The hybi parser was ported from JavaScript to Java by [Eric Butler](https://twitter.com/codebutler) . michael@0: michael@0: The WebSocket client was written by [Eric Butler](https://twitter.com/codebutler) . michael@0: michael@0: The Socket.IO client was written by [Koushik Dutta](https://twitter.com/koush). michael@0: michael@0: ## WebSocket Usage michael@0: michael@0: ```java michael@0: List extraHeaders = Arrays.asList( michael@0: new BasicNameValuePair("Cookie", "session=abcd") michael@0: ); michael@0: michael@0: WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Listener() { michael@0: @Override michael@0: public void onConnect() { michael@0: Log.d(TAG, "Connected!"); michael@0: } michael@0: michael@0: @Override michael@0: public void onMessage(String message) { michael@0: Log.d(TAG, String.format("Got string message! %s", message)); michael@0: } michael@0: michael@0: @Override michael@0: public void onMessage(byte[] data) { michael@0: Log.d(TAG, String.format("Got binary message! %s", toHexString(data))); michael@0: } michael@0: michael@0: @Override michael@0: public void onDisconnect(int code, String reason) { michael@0: Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason)); michael@0: } michael@0: michael@0: @Override michael@0: public void onError(Exception error) { michael@0: Log.e(TAG, "Error!", error); michael@0: } michael@0: }, extraHeaders); michael@0: michael@0: client.connect(); michael@0: michael@0: // Later… michael@0: client.send("hello!"); michael@0: client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF }); michael@0: client.disconnect(); michael@0: ``` michael@0: michael@0: ## Socket.IO Usage michael@0: michael@0: ```java michael@0: SocketIOClient client = new SocketIOClient(URI.create("wss://example.com"), new SocketIOClient.Handler() { michael@0: @Override michael@0: public void onConnect() { michael@0: Log.d(TAG, "Connected!"); michael@0: } michael@0: michael@0: @Override michael@0: public void on(String event, JSONArray arguments) { michael@0: Log.d(TAG, String.format("Got event %s: %s", event, arguments.toString())); michael@0: } michael@0: michael@0: @Override michael@0: public void onJSON(JSONObject json) { michael@0: try { michael@0: Log.d(TAG, String.format("Got JSON Object: %s", json.toString())); michael@0: } catch(JSONException e) { michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void onMessage(String message) { michael@0: Log.d(TAG, String.format("Got message: %s", message)); michael@0: } michael@0: michael@0: @Override michael@0: public void onDisconnect(int code, String reason) { michael@0: Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason)); michael@0: } michael@0: michael@0: @Override michael@0: public void onError(Exception error) { michael@0: Log.e(TAG, "Error!", error); michael@0: } michael@0: }); michael@0: michael@0: client.connect(); michael@0: michael@0: // Later… michael@0: client.emit("Message"); //Message michael@0: JSONArray arguments = new JSONArray(); michael@0: arguments.put("first argument"); michael@0: JSONObject second = new JSONObject(); michael@0: second.put("dictionary", true); michael@0: client.emit(second); //JSON Message michael@0: arguments.put(second); michael@0: client.emit("hello", arguments); //Event michael@0: client.disconnect(); michael@0: ``` michael@0: michael@0: michael@0: michael@0: ## TODO michael@0: michael@0: * Run [autobahn tests](http://autobahn.ws/testsuite) michael@0: * Investigate using [naga](http://code.google.com/p/naga/) instead of threads. michael@0: michael@0: ## License michael@0: michael@0: (The MIT License) michael@0: michael@0: Copyright (c) 2009-2012 James Coglan michael@0: Copyright (c) 2012 Eric Butler michael@0: Copyright (c) 2012 Koushik Dutta michael@0: michael@0: Permission is hereby granted, free of charge, to any person obtaining a copy of michael@0: this software and associated documentation files (the 'Software'), to deal in michael@0: the Software without restriction, including without limitation the rights to use, michael@0: copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the michael@0: Software, and to permit persons to whom the Software is furnished to do so, michael@0: subject to the following conditions: michael@0: michael@0: The above copyright notice and this permission notice shall be included in all michael@0: copies or substantial portions of the Software. michael@0: michael@0: THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS michael@0: FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR michael@0: COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER michael@0: IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN michael@0: CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: