Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

How to implement a transport

Gottox edited this page Mar 16, 2012 · 1 revision

Examples can be found in WebsocketTransport.java or XhrTransport.java Create a class implementing the IOTransport interface.

IOTransport

public static final String TRANSPORT_NAME

This constant should contain the name of the transport.

static IOTransport create(URL url, IOConnection connection)

Called by IOConnector to create a new Instance of the transport. The URL is the one you should connect to. Here you can rewrite the url if needed, i.e. WebsocketTransport rewrites the incoming "http://" address to "ws://"

void connect();

Called by IOConnection. Here you should set up the connection.

void disconnect();

Called by IOConnection. This should shut down the connection. I'm currently not sure if this function is called multiple times. So make sure, it doesn't crash if it's called more than once.

void send(String text) throws IOException;

Called by IOConnection. This call request you to send data to the server.

boolean canSendBulk();

If you can send more than one message at a time, return true. If not return false.

void sendBulk(String[] texts) throws IOException;

Basicly the same as send() but for multiple messages at a time. This is only called when canSendBulk returns true.

void invalidate();

After this call, the transport should not call any methods of IOConnection. It must not disconnect from the server. This is the case when we're forcing a reconnect. If we disconnect gracefully from the server, it will terminate our session.

IOConnection

Ok, now we know when our functions are called. But how do we tell socket.io-java-client to process messages we get? The provided IOConnection does the trick.

IOConnection.transportConnect()

Call this method when the connection is established an the socket is ready to send and receive data.

IOConnection.transportDisconnected()

Call this method when the connection is shot down. IOConnection will care about reconnecting, if it's feasibility.

IOConnection.transportError(Exception error)

Call this method when the connection is experiencing an error. IOConnection will take care about reconnecting or throwing an error to the callbacks. Whatever makes more sense ;)

IOConnection.transportMessage(String message)

This should be called as soon as the transport has received data. IOConnection will take care about parsing the information and calling the callbacks of the sockets.

Changes to IOConnection

Now IOConnection needs to instantiate the transpost look at the sourcecode of IOConnection and search for the connectTransport() method. It's part of the ConnectThread inner class.

add a new else if branch to the section. I.e.:

	...
	else if (protocols.contains(MyTransport.TRANSPORT_NAME))
		transport = MyTransport.create(url, IOConnection.this);
	...