|
| 1 | +# Socket.IO-Client for Java |
| 2 | + |
| 3 | +io.socket is a simple implementation of [socket.io](http://socket.io) for Java. |
| 4 | + |
| 5 | +It uses [Java-WebSocket](https://github.com/TooTallNate/Java-WebSocket) as transport backend, but it's easy |
| 6 | +to write your own transport. See description below. |
| 7 | + |
| 8 | +The API is inspired by [java-socket.io.client](https://github.com/benkay/java-socket.io.client) but as the license |
| 9 | +of this project was unclear and it had some nasty bugs, I decided to write io.socket from the scratch. |
| 10 | + |
| 11 | +## How to use |
| 12 | + |
| 13 | +Using io.socket is quite simple. But lets see: |
| 14 | + |
| 15 | + // Initialise a socket: |
| 16 | + SocketIO socket = new IOSocket("http://127.0.0.1:3001") |
| 17 | + socket.go(new IOCallback() { |
| 18 | + @Override |
| 19 | + public void onMessage(JSONObject json) { |
| 20 | + System.out.println("We received a message: " + json.toString(2)); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public void onMessage(String data) { |
| 25 | + System.out.println("We received a message:" + data); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void onError(SocketIOException socketIOException) { |
| 30 | + System.out.println("Something went wrong"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onDisconnect() { |
| 35 | + System.out.println("Disconnected"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void onConnect() { |
| 40 | + System.out.println("Connected"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void on(String event, JSONObject... args) { |
| 45 | + try { |
| 46 | + socket.emit("answer", new JSONObject().put("msg", "Hello again Socket.io!")); |
| 47 | + } catch (JSONException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + socket.emit("hello", new JSONObject().put("msg", "Hello Socket.io! :D")); |
| 54 | + |
| 55 | +For further informations, read the Javadoc. For end users the interesting parts are io.socket.IOCallback and io.socket.SocketIO. |
| 56 | + |
| 57 | +## What is the architecture? |
| 58 | + |
| 59 | + |
| 60 | +Yea, I know, this is a stub... |
| 61 | + |
| 62 | +## How to implement a transport? |
| 63 | + |
| 64 | +An example can be found in [WebsocketTransport.java](src/io/socket/transports/WebsocketTransport.java) |
| 65 | + |
| 66 | +Create a class implementing the IOTransport interface. |
| 67 | + |
| 68 | + * static IOTransport create(URL url, IOConnection connection) |
| 69 | + |
| 70 | + Called by IOConnector to create a new Instance of the transport. The URL is the one you should connect to. The WebsocketTransport |
| 71 | + rewrites the url, so it uses "ws://" instead of "http://". The IOConnection instance should be saved, as the need to call functions |
| 72 | + when the status of the transport changes. |
| 73 | + |
| 74 | + * void connect(); |
| 75 | + |
| 76 | + Called by IOConnection. Here you should set up the connection. |
| 77 | + |
| 78 | + * void disconnect(); |
| 79 | + |
| 80 | + Called by IOConnection. This should shut down the connection. I'm currently not sure if this function is called multiple times. |
| 81 | + So make sure, it doesn't crash if it's called more than once. |
| 82 | + |
| 83 | + * void send(String text) throws IOException; |
| 84 | + |
| 85 | + Called by IOConnection. This call request you to send data to the connection endpoint |
| 86 | + |
| 87 | + * boolean canSendBulk(); |
| 88 | + |
| 89 | + If you can send more than one message at a time, return true. If not return false. |
| 90 | + |
| 91 | + * void sendBulk(String[] texts) throws IOException; |
| 92 | + |
| 93 | + Basicly the same as send() but for multiple messages at a time. This is only called when canSendBulk returns true. |
| 94 | + |
| 95 | +Ok, now we know when our functions are called. But how do we tell io.socket to process messages we get? IOConnection which the |
| 96 | +create() method gets provides methods do to this. |
| 97 | + |
| 98 | + * IOConnection.transportConnect() |
| 99 | + |
| 100 | + Call this method when the connection is established an the socket is ready to send and receive data. |
| 101 | + |
| 102 | + * IOConnection.transportDisconnected() |
| 103 | + |
| 104 | + Call this method when the connection is shot down. IOConnection will care about reconnecting, if it's feasibility. |
| 105 | + |
| 106 | + * IOConnection.transportError(Exception error) |
| 107 | + |
| 108 | + Call this method when the connection is experiencing an error. IOConnection will take care about reconnecting or throwing an |
| 109 | + error to the callbacks. Whatever makes more sense ;) |
| 110 | + |
| 111 | + * IOConnection.transportMessage(String message) |
| 112 | + |
| 113 | + This should be called as soon as the transport has received data. IOConnection will take care about parsing the information and |
| 114 | + calling the callbacks of the sockets. |
| 115 | + |
| 116 | +So now try to build a transport. :) |
| 117 | + |
| 118 | +## License - the boring stuff... |
| 119 | + |
| 120 | +This library is distributed under MIT Licence. |
0 commit comments