Skip to content
徐昊 edited this page Nov 10, 2018 · 3 revisions
  • The OkSocket receiving data need a specific format, and the library provides the interface for modifying the reading protocol. See below for the default reading protocol we provided. img
  • As above, the headers is 4 bytes of type int, the int value identifies the inclusions the length of the payload, this is the default setting, if you want to custom package header please according to the following method.

For Client

//Set the custom parsing protocol
OkSocketOptions.Builder clientOptions = new OkSocketOptions.Builder();
clientOptions.setReaderProtocol(new IReaderProtocol() {
    @Override
    public int getHeaderLength() {
    	/* 
         * Returns a custom header length that can't be zero or a negative number.
         * The value which you returned should be according 
         * to the fixed length of the header in the server document
         */
        return /*fixed header length*/;
    }

    @Override
    public int getBodyLength(byte[] header, ByteOrder byteOrder) {
        /*
         * Body length is also called Payload length, 
         * This value should be read from the header which is function's input parameters.
    	 * When you parsing the payload length from header, you'd better pay attention to the byteOrder in parameters.
         * We strongly recommend you use java.nio.ByteBuffer to do that. And you need return the payload 
         * length except fixed header length
         */
        return /*payload length except fixed header length*/;
    }
});
//Set the new options to connection manager
mClientManager.option(clientOptions.build());

For Server

//Set the custom parsing protocol
OkServerOptions.Builder serverOptions = new OkServerOptions.Builder();
serverOptions.setReaderProtocol(new IReaderProtocol() {
   //......As same as client definition
});
//Set the new options to server manager when you call the listen() method
mServerManager.listen(serverOptions.build());