Skip to content

Commit 33cb089

Browse files
committed
docs: create README
1 parent d07a6a5 commit 33cb089

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# RxSocketClient
2+
3+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/codeestX/RxSocketClient/pulls)[![API](https://img.shields.io/badge/API-20%2B-brightgreen.svg)](https://android-arsenal.com/api?level=20)[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
4+
5+
RxSocketClient, Reactive Socket APIs for Android, Java and Kotlin, powered by RxJava2
6+
RxSocketClient,支持Android,Java和Kotlin的响应式Socket APIs封装,基于RxJava2
7+
8+
RxJava2 Version: 2.1.1
9+
10+
# Usage
11+
12+
### init
13+
```
14+
SocketClient mClient = RxSocketClient
15+
.create(new SocketConfig.Builder()
16+
.setIp(IP)
17+
.setPort(PORT)
18+
.setCharset(Charsets.UTF_8)
19+
.setThreadStrategy(ThreadStrategy.ASYNC)
20+
.setTimeout(30 * 1000)
21+
.build())
22+
.option(new SocketOption.Builder()
23+
.setHeartBeat(HEART_BEAT, 60 * 1000)
24+
.setHead(HEAD)
25+
.setTail(TAIL)
26+
.build());
27+
28+
```
29+
| value | default | description |
30+
| :--: | :--: | :--: |
31+
| Ip | required | host address |
32+
| Port | required | port number |
33+
| Charset | UTF_8 | the charset when encode a String to byte[] |
34+
| ThreadStrategy | Async | sending data asynchronously or synchronously|
35+
| Timeout | 0 | the timeout of a connection, millisecond |
36+
| HeartBeat | Optional | value and interval of heartbeat, millisecond |
37+
| Head | Optional | appending bytes at head when sending data, not included heartbeat |
38+
| Tail | Optional | appending bytes at last when sending data, not included heartbeat |
39+
40+
### connect
41+
```
42+
Disposable ref = mClient.connect()
43+
.observeOn(AndroidSchedulers.mainThread())
44+
.subscribe(new SocketSubscriber() {
45+
@Override
46+
public void onConnected() {
47+
Log.e(TAG, "onConnected");
48+
}
49+
50+
@Override
51+
public void onDisconnected() {
52+
Log.e(TAG, "onDisconnected");
53+
}
54+
55+
@Override
56+
public void onResponse(@NotNull byte[] data) {
57+
// receive data
58+
Log.e(TAG, Arrays.toString(data));
59+
}
60+
});
61+
```
62+
63+
### disconnect
64+
```
65+
mClient.disconnect();
66+
//or
67+
ref.dispose();
68+
```
69+
70+
### sendData
71+
```
72+
mClient.sendData(bytes);
73+
//or
74+
mClient.sendData(string);
75+
```
76+
77+
# License
78+
79+
Copyright (c) 2017 codeestX
80+
81+
Licensed under the Apache License, Version 2.0 (the "License");
82+
you may not use this file except in compliance with the License.
83+
You may obtain a copy of the License at
84+
85+
http://www.apache.org/licenses/LICENSE-2.0
86+
87+
Unless required by applicable law or agreed to in writing, software
88+
distributed under the License is distributed on an "AS IS" BASIS,
89+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90+
See the License for the specific language governing permissions and
91+
limitations under the License.
92+

RxSocketClient/src/main/java/moe/codeest/rxsocketclient/SocketClient.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import moe.codeest.rxsocketclient.meta.DataWrapper
2121
import moe.codeest.rxsocketclient.meta.SocketConfig
2222
import moe.codeest.rxsocketclient.meta.SocketOption
2323
import moe.codeest.rxsocketclient.meta.ThreadStrategy
24-
import moe.codeest.rxsocketclient.post.AsyncIPoster
24+
import moe.codeest.rxsocketclient.post.AsyncPoster
2525
import moe.codeest.rxsocketclient.post.IPoster
26-
import moe.codeest.rxsocketclient.post.SyncIPoster
26+
import moe.codeest.rxsocketclient.post.SyncPoster
2727
import java.net.Socket
2828
import java.util.concurrent.Executor
2929
import java.util.concurrent.Executors
@@ -50,7 +50,7 @@ class SocketClient(val mConfig: SocketConfig) {
5050

5151
fun connect(): Observable<DataWrapper> {
5252
mObservable = SocketObservable(mConfig, mSocket)
53-
mIPoster = if (mConfig.mThreadStrategy == ThreadStrategy.ASYNC) AsyncIPoster(this, mExecutor) else SyncIPoster(this, mExecutor)
53+
mIPoster = if (mConfig.mThreadStrategy == ThreadStrategy.ASYNC) AsyncPoster(this, mExecutor) else SyncPoster(this, mExecutor)
5454
initHeartBeat()
5555
return mObservable
5656
}
@@ -66,7 +66,7 @@ class SocketClient(val mConfig: SocketConfig) {
6666
if (mHeartBeatConfig != null) {
6767
val disposable = Observable.interval(mHeartBeatConfig.interval, TimeUnit.MILLISECONDS)
6868
.subscribe({
69-
sendData(mHeartBeatConfig.data?: ByteArray(0))
69+
mIPoster.enqueue(mHeartBeatConfig.data?: ByteArray(0))
7070
})
7171
if (mObservable is SocketObservable) {
7272
(mObservable as SocketObservable).setHeartBeatRef(disposable)

RxSocketClient/src/main/java/moe/codeest/rxsocketclient/post/AsyncIPoster.kt renamed to RxSocketClient/src/main/java/moe/codeest/rxsocketclient/post/AsyncPoster.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import java.util.concurrent.Executor
2626
* @description:
2727
*/
2828

29-
class AsyncIPoster(private val mSocketClient: SocketClient, private val mExecutor: Executor) : Runnable, IPoster {
29+
class AsyncPoster(private val mSocketClient: SocketClient, private val mExecutor: Executor) : Runnable, IPoster {
3030

3131
private val queue: PendingPostQueue = PendingPostQueue()
3232

RxSocketClient/src/main/java/moe/codeest/rxsocketclient/post/SyncIPoster.kt renamed to RxSocketClient/src/main/java/moe/codeest/rxsocketclient/post/SyncPoster.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
package moe.codeest.rxsocketclient.post
2-
3-
import moe.codeest.rxsocketclient.SocketClient
4-
import java.util.concurrent.Executor
5-
61
/*
72
* Copyright (C) 2017 codeestX
83
*
@@ -19,13 +14,18 @@ import java.util.concurrent.Executor
1914
* limitations under the License.
2015
*/
2116

17+
package moe.codeest.rxsocketclient.post
18+
19+
import moe.codeest.rxsocketclient.SocketClient
20+
import java.util.concurrent.Executor
21+
2222
/**
2323
* @author: Est <codeest.dev@gmail.com>
2424
* @date: 2017/7/9
2525
* @description:
2626
*/
2727

28-
class SyncIPoster(private val mSocketClient: SocketClient, private val mExecutor: Executor) : Runnable, IPoster {
28+
class SyncPoster(private val mSocketClient: SocketClient, private val mExecutor: Executor) : Runnable, IPoster {
2929

3030
private val queue: PendingPostQueue = PendingPostQueue()
3131

app/src/main/java/moe/codeest/rxsocketclientdemo/JavaActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class JavaActivity extends AppCompatActivity implements View.OnClickListe
3131
private static final String TAG = "JavaActivity";
3232

3333
private static final String IP = "192.168.1.101";
34-
private static final int PORT = 8080;
34+
private static final int PORT = 55731;
3535
private static final byte[] HEART_BEAT = {1, 3, 4};
3636
private static final byte[] HEAD = {1, 2};
3737
private static final byte[] TAIL = {5, 7};

0 commit comments

Comments
 (0)