Explain the simple use of oksocket and Android

An Android lightweight socket communication framework is another masterpiece after okhttp

Framework open source address: https://github.com/xuuhaoo/OkSocket , welcome to communicate with star, fork and issue

Oksocket introduction

Android oksocket is an overall solution for socket client based on blocking traditional socket. You can use it for simple socket communication based on TCP protocol. Of course, you can also carry out socket communication with large amount of data and complex, and support simplex and duplex communication

Maven configuration

Oksocket currently only supports jcenter warehouses

Add the dependent configuration in the build.gradle file of the module

Parameter configuration

Add permissions in androidmanifest.xml:

Confusion configuration

Please avoid confusing oksocket and add the following configuration in the Proguard file:

Oksocket initialization

Copy the following code into the project application class oncreate(), oksocket will automatically detect the environment and complete the configuration:

Call demo

Test server

This server is a test server for beginners to debug the oksocket library. Beginners can install the app in the project on the mobile phone and click the connect button. This server is only used to be familiar with the communication mode and analysis mode. This server does not support heartbeat return and cannot be used as a commercial server. The server code is in the socketserverdemo folder, Please pay attention to the reference reading

IP: 104.238.184.237

Port: 8080

You can also choose to download the jar file locally and run it locally for debugging download jar

After downloading, use the following code to run it Java - jar socketserverdemo.jar

Simple long connection

OkSocket will default to cache management for every new Open channel, creating ConnectionManager manager only when the Open method is first invoked, then the caller can continue to call the relevant method by obtaining the reference to the ConnectionManager.

Long connection with callback

Register the listeners of this channel. The listeners in each connection channel are isolated from each other. Therefore, if a project is connected to multiple sockets, it is necessary to register its own connection listener in each connection. The connection listener is the only way for the oksocket to interact with the user

Configurable long connection

The behavior of obtaining oksocketoptions belongs to a relatively advanced oksocket calling method. Each connection will correspond to an oksocketoptions. If oksocketoptions are not specified when opening is called for the first time, oksocket will use the default configuration object. For the default configuration, please refer to the advanced call description at the bottom of the document

How to send data

How to receive data

The oksocket client receives server data in a certain format. The oksocketoptions of the client provides an interface to modify the default header parsing rules returned by the server. See the following figure for the default header and body parsing rules

Data structure diagram

As shown in the figure above, the contents in the header are of type int with a length of 4 bytes. The int value identifies the length of the data area of the package body, which is the default header resolution. If you need to customize the header, please follow the following methods

How to keep your heart beating

After receiving the heartbeat, you need to feed the dog

Because our client needs to know that the server has received the heartbeat, the server needs to respond after receiving the heartbeat. After receiving the heartbeat response, we need to feed the dog locally. Otherwise, when the heartbeat is sent for more than a certain number of times and the dog feeding operation is not obtained, the dog will disconnect and reconnect the connection

How to manually trigger a heartbeat at any time

Oksocket configuration options and callback description

OkSocketOptions

ISocketActionListener

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>