SocketClient,支持Android,Java和Kotlin的响应式Socket APIs封装,基于RxJava2
RxJava2 Version: 2.1.1
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.NicoLiutong:SocketClient:v1.0.6'
}
SocketClient mClient = CreatSocketClient
.create(new SocketConfig.Builder()
.setIp(IP)
.setPort(PORT)
.setCharset(Charsets.UTF_8)
.setThreadStrategy(ThreadStrategy.ASYNC)
.setTimeout(30 * 1000)
.build())
.option(new SocketOption.Builder()
.setHeartBeat(HEART_BEAT, 60 * 1000)
.setHead(HEAD)
.setTail(TAIL)
.build());
value | default | description |
---|---|---|
Ip | required | host address |
Port | required | port number |
Charset | UTF_8 | the charset when encode a String to byte[] |
ThreadStrategy | Async | sending data asynchronously or synchronously |
Timeout | 0 | the timeout of a connection, millisecond |
HeartBeat | Optional | value and interval of heartbeat, millisecond |
Head | Optional | appending bytes at head when sending data, not included heartbeat |
Tail | Optional | appending bytes at last when sending data, not included heartbeat |
Disposable ref = mClient.connect()
... // anything else what you can do with RxJava
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SocketSubscriber() {
@Override
public void onFailConnected() {
//onConnected
Log.e(TAG, "onFailConnected");
}
@Override
public void onConnected() {
//onConnected
Log.e(TAG, "onConnected");
}
@Override
public void onDisconnected() {
//onDisconnected
Log.e(TAG, "onDisconnected");
}
@Override
public void onResponse(@NotNull byte[] data) {
//receive data
Log.e(TAG, Arrays.toString(data));
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
//onError
Log.e(TAG, throwable.toString());
}
});
mClient.disconnect();
//or
ref.dispose();
mClient.sendData(bytes);
//or
mClient.sendData(string);