xuuhaoo / OkSocket
- суббота, 21 октября 2017 г. в 03:14:02
An blocking socket client for Android applications.
An blocking socket client for Android applications.
Read this in other languages: 简体中文
allprojects {
repositories {
jcenter()
}
}
dependencies {
compile 'com.tonystark.android:socket:1.0.0'
}
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
-dontwarn com.xuhao.android.libsocket.**
-keep class com.xuhao.android.socket.impl.abilities.** { *; }
-keep class com.xuhao.android.socket.impl.exceptions.** { *; }
-keep class com.xuhao.android.socket.impl.EnvironmentalManager { *; }
-keep class com.xuhao.android.socket.impl.BlockConnectionManager { *; }
-keep class com.xuhao.android.socket.impl.UnBlockConnectionManager { *; }
-keep class com.xuhao.android.socket.impl.SocketActionHandler { *; }
-keep class com.xuhao.android.socket.impl.PulseManager { *; }
-keep class com.xuhao.android.socket.impl.ManagerHolder { *; }
-keep class com.xuhao.android.socket.interfaces.** { *; }
-keep class com.xuhao.android.socket.sdk.** { *; }
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class com.xuhao.android.socket.sdk.OkSocketOptions$* {
*;
}
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//The main process needs to be distinguished at the beginning of the primary process.
OkSocket.initialize(this);
//If you need to open the Socket debug log, configure the following
//OkSocket.initialize(this,true);
}
}
//Connection parameter Settings (IP, port number), which is also a unique identifier for a connection, with different connections, at least one of the two values in this parameter
ConnectionInfo info = new ConnectionInfo("127.0.0.1", 8088);
//Call OkSocket, open the channel for this connection, and call the channel's connection method for physical connections.
OkSocket.open(info).connect();
//After obtaining the connection manager from the above method...
manager.registerReceiver(new SocketActionAdapter(){
@Override
public void onSocketConnectionSuccess(Context context, ConnectionInfo info, String action) {
Toast.makeText(context, "The connection is successful", LENGTH_SHORT).show();
}
});
//call the channel's connection method for physical connections.
manager.connect();
ConnectionInfo info = new ConnectionInfo("127.0.0.1", 8088);
IConnectionManager manager = OkSocket.open(info);
//Gets the reference object for the current connection channel
OkSocketOptions options= manager.getOption();
//Build a builder class based on the current reference object
OkSocketOptions.Builder builder = new OkSocketOptions.Builder(options);
//Modify the parameter Settings (refer to the class documentation for other references)
builder.setSinglePackageBytes(size);
//Create a new reference object and set it to the channel
manager.option(builder.build());
manager.connect();
//Class A:
//...Define the data structure to be sent...
public class TestSendData implements ISendable {
private String str = "";
public TestSendData() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("cmd", 14);
jsonObject.put("data", "{x:2,y:1}");
str = jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public byte[] parse() {
//Build the byte array according to the server's parsing rules
byte[] body = str.getBytes(Charset.defaultCharset());
ByteBuffer bb = ByteBuffer.allocate(4 + body.length);
bb.order(ByteOrder.BIG_ENDIAN);
bb.putInt(body.length);
bb.put(body);
return bb.array();
}
}
//Class B:
private IConnectionManager mManager;
//...Omit the connection and set the associated code for the callback...
@Override
public void onSocketConnectionSuccess(Context context, ConnectionInfo info, String action) {
//Chain programming call
OkSocket.open(info)
.send(new TestSendData());
============OR==============
//The IConnectManager can also be saved as a member variable.
mManager = OkSocket.open(info);
if(mManager != null){
mManager.send(new TestSendData());
}
}
//Set the custom parsing header
OkSocketOptions.Builder okOptionsBuilder = new OkSocketOptions.Builder(mOkOptions);
okOptionsBuilder.setHeaderProtocol(new IHeaderProtocol() {
@Override
public int getHeaderLength() {
//Returns a custom header length that automatically parses the length of the head
return 0;
}
@Override
public int getBodyLength(byte[] header, ByteOrder byteOrder) {
//The length of the body is parsed from the header, byteOrder is the sequence of bytes that you configured in the parameter, which can be easily parsed using ByteBuffer
return 0;
}
});
//Set the modified parameter to the connection manager
mManager.option(okOptionsBuilder.build());
//...After the parsing header is properly set...
@Override
public void onSocketReadResponse(Context context, ConnectionInfo info, String action, OriginalData data) {
//Follow the above rules, this callback can be normal received the data returned from the server, the data in the OriginalData, for byte [] array, the array data already processed byte sequence problem, can be used in the ByteBuffer directly
}
//Class A:
//...Define the heartbeat data type required by the heartbeat manager...
public class PulseData implements IPulseSendable {
private String str = "pulse";
@Override
public byte[] parse() {
//Build the byte array according to the server's parsing rules
byte[] body = str.getBytes(Charset.defaultCharset());
ByteBuffer bb = ByteBuffer.allocate(4 + body.length);
bb.order(ByteOrder.BIG_ENDIAN);
bb.putInt(body.length);
bb.put(body);
return bb.array();
}
}
//Class B:
private IConnectionManager mManager;
private PulseData mPulseData = new PulseData;
@Override
public void onSocketConnectionSuccess(Context context, ConnectionInfo info, String action) {
//Chain programming call. Set heartbeat data for the heartbeat manager. A connection only has a heartbeat manager, so the data is set only once. If disconnected, please set it again.
OkSocket.open(info)
.getPulseManager()
.setPulseSendable(mPulseData)
.pulse();//Start the heartbeat.
//After the heartbeat, the heartbeat manager will automatically carry out the heartbeat trigger
======OR======
mManager = OkSocket.open(info);
if(mManager != null){
PulseManager pulseManager = mManager.getPulseManager();
//Set the heartbeat data to the heartbeat manager, and a connection has only one heartbeat manager, so the data is only set once, and if disconnected, set it again.
pulseManager.setPulseSendable(mPulseData);
//Start the heartbeat. After the heartbeat, the heartbeat manager will automatically carry out the heartbeat trigger
pulseManager.pulse();
}
}
private IConnectionManager mManager;
//When the client receives the message
@Override
public void onSocketReadResponse(Context context, ConnectionInfo info, String action, OriginalData data) {
if(mManager != null && It's the heartbeat return package){
//Whether it is a heartbeat return package, you need to resolve the data returned by the server to know
//Feed dog
mManager.getPulseManager().feed();
}
}
private IConnectionManager mManager;
//...in anywhere...
mManager = OkSocket.open(info);
if(mManager != null){
PulseManager pulseManager = mManager.getPulseManager();
//Manually trigger a heartbeat (mainly used for situations requiring manual control of trigger timing)
pulseManager.trigger();
}
OkSocketOptions
mIOThreadMode
isConnectionHolden
mWriteOrder
mReadByteOrder
mHeaderProtocol
mSendSinglePackageBytes
mReadSingleTimeBufferBytes
mPulseFrequency
mPulseFeedLoseTimes
mBackgroundLiveMinute
mConnectTimeoutSecond
mMaxReadDataMB
mReconnectionManager
ISocketActionListener
onSocketIOThreadStart
onSocketIOThreadShutdown
onSocketDisconnection
onSocketConnectionSuccess
onSocketConnectionFailed
onSocketReadResponse
onSocketWriteResponse
onPulseSend