Skip to content
徐昊 edited this page Nov 10, 2018 · 6 revisions

Simple connections

  • OkSocket will default to each new channels for cache management, only in the first call to the Open() method is created when the ConnectionManager manager, after calling the method the manager will be cached, and you can pass retrieves a reference to the ConnectionManager, continue to call the related method.
//Connection parameter Settings (IP, port number), which is also a unique identifier for a connection. 
ConnectionInfo info = new ConnectionInfo("104.238.184.237", 8080);
//Call OkSocket open() the channel for this connection, and the physical connections will be connected.
IConnectionManager manager = OkSocket.open(info);
manager.connect();
  • ConnectionManager is mainly responsible for the Socket connection, disconnect, send the message, heartbeat management, etc. It is the one of importent object you need to care about.

Connection with callback

  • Registered Socket channel listener, each Socket channel listener in isolation from each other, so if in a project, to connect multiple Socket channel, need to be registered in each Socket channel listeners own connections, connection listener OkSocket library is the only way to interact with the caller
IConnectionManager manager = OkSocket.open(info);
manager.registerReceiver(new SocketActionAdapter(){
	@Override
	public void onSocketConnectionSuccess(ConnectionInfo info, String action) {
	 Toast.makeText(context, "The connection is successful", LENGTH_SHORT).show();
	}
});
// You should call the connect() method at the end. 
// Otherwise you maybe could'nt receive the connection callback.
manager.connect();

ATTENTION: You should call the unRegisterReceiver() method when Activity OR Service destroy.