├── .github └── workflows │ └── android.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── doc ├── allclasses-frame.html ├── allclasses-noframe.html ├── com │ └── zhangke │ │ └── websocket │ │ ├── DefaultReconnectManager.html │ │ ├── NetworkChangedReceiver.html │ │ ├── ReconnectManager.OnConnectListener.html │ │ ├── ReconnectManager.html │ │ ├── SimpleDispatcher.html │ │ ├── SimpleListener.html │ │ ├── SocketListener.html │ │ ├── SocketWrapperListener.html │ │ ├── WebSocketEngine.html │ │ ├── WebSocketHandler.html │ │ ├── WebSocketManager.html │ │ ├── WebSocketSetting.html │ │ ├── WebSocketWrapper.html │ │ ├── dispatcher │ │ ├── DefaultResponseDispatcher.html │ │ ├── EngineThread.html │ │ ├── IResponseDispatcher.html │ │ ├── MainThreadResponseDelivery.html │ │ ├── ResponseDelivery.html │ │ ├── ResponseProcessEngine.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ ├── package-tree.html │ │ ├── request │ │ ├── ByteArrayRequest.html │ │ ├── ByteBufferRequest.html │ │ ├── CollectionFrameDataRequest.html │ │ ├── FrameDataRequest.html │ │ ├── PingRequest.html │ │ ├── PongRequest.html │ │ ├── Request.html │ │ ├── RequestFactory.html │ │ ├── StringRequest.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html │ │ ├── response │ │ ├── ByteBufferResponse.html │ │ ├── ErrorResponse.html │ │ ├── PingResponse.html │ │ ├── PongResponse.html │ │ ├── Response.html │ │ ├── ResponseFactory.html │ │ ├── TextResponse.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html │ │ └── util │ │ ├── LogImpl.html │ │ ├── LogUtil.html │ │ ├── Logable.html │ │ ├── PermissionUtil.html │ │ ├── ThreadUtil.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html ├── constant-values.html ├── deprecated-list.html ├── help-doc.html ├── index-files │ ├── index-1.html │ ├── index-10.html │ ├── index-11.html │ ├── index-12.html │ ├── index-13.html │ ├── index-14.html │ ├── index-15.html │ ├── index-16.html │ ├── index-17.html │ ├── index-18.html │ ├── index-2.html │ ├── index-3.html │ ├── index-4.html │ ├── index-5.html │ ├── index-6.html │ ├── index-7.html │ ├── index-8.html │ └── index-9.html ├── index.html ├── overview-frame.html ├── overview-summary.html ├── overview-tree.html ├── package-list ├── script.js └── stylesheet.css ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image ├── dispatcher.png ├── qrcode_for_account.jpg ├── qrcode_for_gsubscription.jpg ├── websocketservice.png └── websocketthread.png ├── samples ├── dispatersample │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── zhangke │ │ │ └── smaple │ │ │ └── dispatersample │ │ │ ├── App.java │ │ │ ├── AppResponseDispatcher.java │ │ │ ├── CommonResponseEntity.java │ │ │ └── MainActivity.java │ │ └── res │ │ ├── drawable │ │ └── ic_launcher.png │ │ ├── layout │ │ └── activity_main.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml └── simple │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zhangke │ │ └── websocketdemo │ │ ├── App.java │ │ ├── TestActivity.java │ │ └── UiUtil.java │ └── res │ ├── drawable │ └── ic_launcher.png │ ├── layout │ └── activity_test.xml │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── settings.gradle └── websocketlib ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── com │ └── zhangke │ └── websocket │ ├── DefaultReconnectManager.java │ ├── NetworkChangedReceiver.java │ ├── ReconnectManager.java │ ├── SimpleDispatcher.java │ ├── SimpleListener.java │ ├── SocketListener.java │ ├── SocketWrapperListener.java │ ├── WebSocketEngine.java │ ├── WebSocketHandler.java │ ├── WebSocketManager.java │ ├── WebSocketSetting.java │ ├── WebSocketWrapper.java │ ├── dispatcher │ ├── DefaultResponseDispatcher.java │ ├── EngineThread.java │ ├── IResponseDispatcher.java │ ├── MainThreadResponseDelivery.java │ ├── ResponseDelivery.java │ └── ResponseProcessEngine.java │ ├── request │ ├── ByteArrayRequest.java │ ├── ByteBufferRequest.java │ ├── CollectionFrameDataRequest.java │ ├── FrameDataRequest.java │ ├── PingRequest.java │ ├── PongRequest.java │ ├── Request.java │ ├── RequestFactory.java │ └── StringRequest.java │ ├── response │ ├── ByteBufferResponse.java │ ├── ErrorResponse.java │ ├── PingResponse.java │ ├── PongResponse.java │ ├── Response.java │ ├── ResponseFactory.java │ └── TextResponse.java │ └── util │ ├── LogImpl.java │ ├── LogUtil.java │ ├── Logable.java │ ├── PermissionUtil.java │ └── ThreadUtil.java └── res └── values └── strings.xml /.github/workflows/android.yml: -------------------------------------------------------------------------------- 1 | name: Android CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: set up JDK 1.8 13 | uses: actions/setup-java@v1 14 | with: 15 | java-version: 1.8 16 | - name: Build with Gradle 17 | run: ./gradlew build 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | /.gradle 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 ZhangKe 2 | 3 | 996 License Version 1.0 (Draft) 4 | 5 | Permission is hereby granted to any individual or legal entity 6 | obtaining a copy of this licensed work (including the source code, 7 | documentation and/or related items, hereinafter collectively referred 8 | to as the "licensed work"), free of charge, to deal with the licensed 9 | work for any purpose, including without limitation, the rights to use, 10 | reproduce, modify, prepare derivative works of, distribute, publish 11 | and sublicense the licensed work, subject to the following conditions: 12 | 13 | 1. The individual or the legal entity must conspicuously display, 14 | without modification, this License and the notice on each redistributed 15 | or derivative copy of the Licensed Work. 16 | 17 | 2. The individual or the legal entity must strictly comply with all 18 | applicable laws, regulations, rules and standards of the jurisdiction 19 | relating to labor and employment where the individual is physically 20 | located or where the individual was born or naturalized; or where the 21 | legal entity is registered or is operating (whichever is stricter). In 22 | case that the jurisdiction has no such laws, regulations, rules and 23 | standards or its laws, regulations, rules and standards are 24 | unenforceable, the individual or the legal entity are required to 25 | comply with Core International Labor Standards. 26 | 27 | 3. The individual or the legal entity shall not induce or force its 28 | employee(s), whether full-time or part-time, or its independent 29 | contractor(s), in any methods, to agree in oral or written form, to 30 | directly or indirectly restrict, weaken or relinquish his or her 31 | rights or remedies under such laws, regulations, rules and standards 32 | relating to labor and employment as mentioned above, no matter whether 33 | such written or oral agreement are enforceable under the laws of the 34 | said jurisdiction, nor shall such individual or the legal entity 35 | limit, in any methods, the rights of its employee(s) or independent 36 | contractor(s) from reporting or complaining to the copyright holder or 37 | relevant authorities monitoring the compliance of the license about 38 | its violation(s) of the said license. 39 | 40 | THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 41 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 42 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 43 | IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, 44 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 45 | OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE 46 | LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK. 47 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext { 5 | compileSdkVersion = 28 6 | minSdkVersion = 15 7 | targetSdkVersion = 28 8 | buildToolsVersion = '28.0.3' 9 | } 10 | repositories { 11 | google() 12 | mavenCentral() 13 | jcenter() 14 | } 15 | dependencies { 16 | classpath 'com.android.tools.build:gradle:3.1.4' 17 | 18 | 19 | // NOTE: Do not place your application dependencies here; they belong 20 | // in the individual module build.gradle files 21 | } 22 | } 23 | 24 | allprojects { 25 | repositories { 26 | google() 27 | mavenCentral() 28 | jcenter() 29 | } 30 | } 31 | 32 | task clean(type: Delete) { 33 | delete rootProject.buildDir 34 | } 35 | -------------------------------------------------------------------------------- /doc/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 56 |
57 | 58 | 59 | -------------------------------------------------------------------------------- /doc/allclasses-noframe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 所有类 7 | 8 | 9 | 10 | 11 | 12 |

所有类

13 |
14 | 56 |
57 | 58 | 59 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/WebSocketWrapper.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | WebSocketWrapper 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 85 | 86 | 87 |
88 |
com.zhangke.websocket
89 |

类 WebSocketWrapper

90 |
91 |
92 | 100 |
101 | 112 |
113 |
114 | 133 |
134 |
135 | 136 | 137 |
138 | 139 | 140 |
跳过导航链接
141 | 142 | 143 | 144 | 153 |
154 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/dispatcher/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket.dispatcher 7 | 8 | 9 | 10 | 11 | 12 |

com.zhangke.websocket.dispatcher

13 |
14 |

接口

15 | 19 |

20 | 26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket 7 | 8 | 9 | 10 | 11 | 12 |

com.zhangke.websocket

13 |
14 |

接口

15 | 21 |

22 | 33 |
34 | 35 | 36 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/request/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket.request 7 | 8 | 9 | 10 | 11 | 12 |

com.zhangke.websocket.request

13 |
14 |

接口

15 | 18 |

19 | 29 |
30 | 31 | 32 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/response/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket.response 7 | 8 | 9 | 10 | 11 | 12 |

com.zhangke.websocket.response

13 |
14 |

接口

15 | 18 |

19 | 27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/util/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket.util 7 | 8 | 9 | 10 | 11 | 12 |

com.zhangke.websocket.util

13 |
14 |

接口

15 | 18 |

19 | 25 |
26 | 27 | 28 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/util/package-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket.util 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
72 |

程序包 com.zhangke.websocket.util

73 |
74 |
75 | 139 |
140 | 141 |
142 | 143 | 144 |
跳过导航链接
145 | 146 | 147 | 148 | 157 |
158 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /doc/com/zhangke/websocket/util/package-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.zhangke.websocket.util 类分层结构 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
72 |

程序包com.zhangke.websocket.util的分层结构

73 | 程序包分层结构: 74 | 77 |
78 |
79 |

类分层结构

80 | 90 |

接口分层结构

91 | 94 |
95 | 96 |
97 | 98 | 99 |
跳过导航链接
100 | 101 | 102 | 103 | 112 |
113 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /doc/constant-values.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 常量字段值 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
72 |

常量字段值

73 |

目录

74 | 77 |
78 |
79 | 80 | 81 |

com.zhangke.*

82 | 117 |
118 | 119 |
120 | 121 | 122 |
跳过导航链接
123 | 124 | 125 | 126 | 135 |
136 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /doc/deprecated-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 已过时的列表 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
72 |

已过时的 API

73 |

目录

74 |
75 | 76 |
77 | 78 | 79 |
跳过导航链接
80 | 81 | 82 | 83 | 92 |
93 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /doc/help-doc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | API 帮助 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
72 |

此 API 文档的组织方式

73 |
此 API (应用程序编程接口) 文档包含对应于导航栏中的项目的页面, 如下所述。
74 |
75 |
76 | 175 | 此帮助文件适用于使用标准 doclet 生成的 API 文档。
176 | 177 |
178 | 179 | 180 |
跳过导航链接
181 | 182 | 183 | 184 | 193 |
194 | 221 | 222 | 223 | 224 | -------------------------------------------------------------------------------- /doc/index-files/index-1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | A - 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
A B C D E F G I L M N O P R S T V W  72 | 73 | 74 |

A

75 |
76 |
addListener(SocketListener) - 类 中的方法com.zhangke.websocket.dispatcher.MainThreadResponseDelivery
77 |
 
78 |
addListener(SocketListener) - 接口 中的方法com.zhangke.websocket.dispatcher.ResponseDelivery
79 |
 
80 |
addListener(SocketListener) - 类 中的方法com.zhangke.websocket.WebSocketManager
81 |
82 |
添加一个监听器,使用完成之后需要调用 83 | WebSocketManager.removeListener(SocketListener) 方法移除监听器
84 |
85 |
86 | A B C D E F G I L M N O P R S T V W 
87 | 88 |
89 | 90 | 91 |
跳过导航链接
92 | 93 | 94 | 95 | 104 |
105 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /doc/index-files/index-10.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | M - 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
A B C D E F G I L M N O P R S T V W  72 | 73 | 74 |

M

75 |
76 |
MainThreadResponseDelivery - com.zhangke.websocket.dispatcher中的类
77 |
78 |
用户注册的消息发射器, 79 | 内部维护一个 SocketListener 的 List, 80 | 调用每一个方法都会通知 List 中所有的 Listener, 81 | 这么做主要为了统一控制消息的回调线程以及简化代码。
82 |
83 |
MainThreadResponseDelivery() - 类 的构造器com.zhangke.websocket.dispatcher.MainThreadResponseDelivery
84 |
 
85 |
86 | A B C D E F G I L M N O P R S T V W 
87 | 88 |
89 | 90 | 91 |
跳过导航链接
92 | 93 | 94 | 95 | 104 |
105 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /doc/index-files/index-11.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | N - 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
A B C D E F G I L M N O P R S T V W  72 | 73 | 74 |

N

75 |
76 |
NetworkChangedReceiver - com.zhangke.websocket中的类
77 |
78 |
监听网络变化广播,网络变化时自动重连 79 | Created by ZhangKe on 2018/7/2.
80 |
81 |
NetworkChangedReceiver() - 类 的构造器com.zhangke.websocket.NetworkChangedReceiver
82 |
 
83 |
84 | A B C D E F G I L M N O P R S T V W 
85 | 86 |
87 | 88 | 89 |
跳过导航链接
90 | 91 | 92 | 93 | 102 |
103 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /doc/index-files/index-2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | B - 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
A B C D E F G I L M N O P R S T V W  72 | 73 | 74 |

B

75 |
76 |
ByteArrayRequest - com.zhangke.websocket.request中的类
77 |
78 |
byte[] 类型的请求 79 | 80 | Created by ZhangKe on 2019/3/22.
81 |
82 |
ByteBufferRequest - com.zhangke.websocket.request中的类
83 |
84 |
ByteBuffer 类型的请求 85 | 86 | Created by ZhangKe on 2019/3/22.
87 |
88 |
ByteBufferResponse - com.zhangke.websocket.response中的类
89 |
90 |
接收到二进制数据 91 | 92 | Created by ZhangKe on 2019/3/22.
93 |
94 |
95 | A B C D E F G I L M N O P R S T V W 
96 | 97 |
98 | 99 | 100 |
跳过导航链接
101 | 102 | 103 | 104 | 113 |
114 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /doc/index-files/index-6.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | F - 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
A B C D E F G I L M N O P R S T V W  72 | 73 | 74 |

F

75 |
76 |
FrameDataRequest - com.zhangke.websocket.request中的类
77 |
78 |
发送 Framedata 79 | 80 | Created by ZhangKe on 2019/3/28.
81 |
82 |
83 | A B C D E F G I L M N O P R S T V W 
84 | 85 |
86 | 87 | 88 |
跳过导航链接
89 | 90 | 91 | 92 | 101 |
102 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /doc/index-files/index-9.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | L - 索引 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
A B C D E F G I L M N O P R S T V W  72 | 73 | 74 |

L

75 |
76 |
Logable - com.zhangke.websocket.util中的接口
77 |
78 |
打印日志接口 79 | 80 | Created by ZhangKe on 2019/4/29.
81 |
82 |
LogImpl - com.zhangke.websocket.util中的类
83 |
84 |
Logable 默认实现类 85 | 86 | Created by ZhangKe on 2019/4/29.
87 |
88 |
LogImpl() - 类 的构造器com.zhangke.websocket.util.LogImpl
89 |
 
90 |
LogUtil - com.zhangke.websocket.util中的类
91 |
92 |
日志工具类 93 | 94 | Created by ZhangKe on 2019/3/21.
95 |
96 |
LogUtil() - 类 的构造器com.zhangke.websocket.util.LogUtil
97 |
 
98 |
99 | A B C D E F G I L M N O P R S T V W 
100 | 101 |
102 | 103 | 104 |
跳过导航链接
105 | 106 | 107 | 108 | 117 |
118 | 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 生成的文档 (无标题) 7 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | <noscript> 69 | <div>您的浏览器已禁用 JavaScript。</div> 70 | </noscript> 71 | <h2>框架预警</h2> 72 | <p>请使用框架功能查看此文档。如果看到此消息, 则表明您使用的是不支持框架的 Web 客户机。链接到<a href="overview-summary.html">非框架版本</a>。</p> 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /doc/overview-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 概览列表 7 | 8 | 9 | 10 | 11 | 12 |
所有类
13 |
14 |

程序包

15 | 22 |
23 |

 

24 | 25 | 26 | -------------------------------------------------------------------------------- /doc/overview-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 概览 7 | 8 | 9 | 10 | 11 | 12 | 22 | 25 | 26 |
27 | 28 | 29 |
跳过导航链接
30 | 31 | 32 | 33 | 42 |
43 | 70 | 71 |
72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 |
程序包 
程序包说明
com.zhangke.websocket 
com.zhangke.websocket.dispatcher 
com.zhangke.websocket.request 
com.zhangke.websocket.response 
com.zhangke.websocket.util 
101 |
102 | 103 |
104 | 105 | 106 |
跳过导航链接
107 | 108 | 109 | 110 | 119 |
120 | 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /doc/package-list: -------------------------------------------------------------------------------- 1 | com.zhangke.websocket 2 | com.zhangke.websocket.dispatcher 3 | com.zhangke.websocket.request 4 | com.zhangke.websocket.response 5 | com.zhangke.websocket.util 6 | -------------------------------------------------------------------------------- /doc/script.js: -------------------------------------------------------------------------------- 1 | function show(type) 2 | { 3 | count = 0; 4 | for (var key in methods) { 5 | var row = document.getElementById(key); 6 | if ((methods[key] & type) != 0) { 7 | row.style.display = ''; 8 | row.className = (count++ % 2) ? rowColor : altColor; 9 | } 10 | else 11 | row.style.display = 'none'; 12 | } 13 | updateTabs(type); 14 | } 15 | 16 | function updateTabs(type) 17 | { 18 | for (var value in tabs) { 19 | var sNode = document.getElementById(tabs[value][0]); 20 | var spanNode = sNode.firstChild; 21 | if (value == type) { 22 | sNode.className = activeTableTab; 23 | spanNode.innerHTML = tabs[value][1]; 24 | } 25 | else { 26 | sNode.className = tableTab; 27 | spanNode.innerHTML = "" + tabs[value][1] + ""; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Nov 07 10:33:11 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /image/dispatcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/image/dispatcher.png -------------------------------------------------------------------------------- /image/qrcode_for_account.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/image/qrcode_for_account.jpg -------------------------------------------------------------------------------- /image/qrcode_for_gsubscription.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/image/qrcode_for_gsubscription.jpg -------------------------------------------------------------------------------- /image/websocketservice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/image/websocketservice.png -------------------------------------------------------------------------------- /image/websocketthread.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/image/websocketthread.png -------------------------------------------------------------------------------- /samples/dispatersample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /samples/dispatersample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion rootProject.ext.compileSdkVersion 5 | buildToolsVersion rootProject.ext.buildToolsVersion 6 | defaultConfig { 7 | applicationId "com.zhangke.smaple.dispatersample" 8 | minSdkVersion rootProject.ext.minSdkVersion 9 | targetSdkVersion rootProject.ext.targetSdkVersion 10 | versionCode 3 11 | versionName "3.0" 12 | } 13 | 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | } 22 | 23 | dependencies { 24 | implementation fileTree(dir: 'libs', include: ['*.jar']) 25 | 26 | implementation 'androidx.appcompat:appcompat:1.0.2' 27 | implementation 'com.alibaba:fastjson:1.2.33' 28 | implementation project(':websocketlib') 29 | } 30 | -------------------------------------------------------------------------------- /samples/dispatersample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /samples/dispatersample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /samples/dispatersample/src/main/java/com/zhangke/smaple/dispatersample/App.java: -------------------------------------------------------------------------------- 1 | package com.zhangke.smaple.dispatersample; 2 | 3 | import android.app.Application; 4 | 5 | import com.zhangke.websocket.WebSocketHandler; 6 | import com.zhangke.websocket.WebSocketManager; 7 | import com.zhangke.websocket.WebSocketSetting; 8 | 9 | /** 10 | * Created by ZhangKe on 2018/6/27. 11 | */ 12 | public class App extends Application { 13 | 14 | @Override 15 | public void onCreate() { 16 | super.onCreate(); 17 | 18 | initWebSocket(); 19 | } 20 | 21 | private void initWebSocket(){ 22 | WebSocketSetting setting = new WebSocketSetting(); 23 | //连接地址,必填,例如 wss://echo.websocket.org 24 | setting.setConnectUrl("your connect url");//必填 25 | 26 | //设置连接超时时间 27 | setting.setConnectTimeout(15 * 1000); 28 | 29 | //设置心跳间隔时间 30 | setting.setConnectionLostTimeout(60); 31 | 32 | //设置断开后的重连次数,可以设置的很大,不会有什么性能上的影响 33 | setting.setReconnectFrequency(60); 34 | 35 | // //设置Header 36 | // setting.setHttpHeaders(header); 37 | 38 | //设置消息分发器,接收到数据后先进入该类中处理,处理完再发送到下游 39 | setting.setResponseProcessDispatcher(new AppResponseDispatcher()); 40 | //接收到数据后是否放入子线程处理,只有设置了 ResponseProcessDispatcher 才有意义 41 | setting.setProcessDataOnBackground(true); 42 | 43 | //网络状态发生变化后是否重连, 44 | //需要调用 WebSocketHandler.registerNetworkChangedReceiver(context) 方法注册网络监听广播 45 | setting.setReconnectWithNetworkChanged(true); 46 | 47 | //通过 init 方法初始化默认的 WebSocketManager 对象 48 | WebSocketManager manager = WebSocketHandler.init(setting); 49 | //启动连接 50 | manager.start(); 51 | 52 | //注意,需要在 AndroidManifest 中配置网络状态获取权限 53 | //注册网路连接状态变化广播 54 | WebSocketHandler.registerNetworkChangedReceiver(this); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /samples/dispatersample/src/main/java/com/zhangke/smaple/dispatersample/AppResponseDispatcher.java: -------------------------------------------------------------------------------- 1 | package com.zhangke.smaple.dispatersample; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONException; 5 | import com.alibaba.fastjson.TypeReference; 6 | import com.zhangke.websocket.SimpleDispatcher; 7 | import com.zhangke.websocket.dispatcher.ResponseDelivery; 8 | import com.zhangke.websocket.response.ErrorResponse; 9 | import com.zhangke.websocket.response.Response; 10 | import com.zhangke.websocket.response.ResponseFactory; 11 | 12 | /** 13 | * 消息分发器 14 | *

15 | * Created by ZhangKe on 2018/6/27. 16 | */ 17 | public class AppResponseDispatcher extends SimpleDispatcher { 18 | 19 | /** 20 | * JSON 数据格式错误 21 | */ 22 | public static final int JSON_ERROR = 11; 23 | /** 24 | * code 码错误 25 | */ 26 | public static final int CODE_ERROR = 12; 27 | 28 | @Override 29 | public void onMessage(String message, ResponseDelivery delivery) { 30 | try { 31 | CommonResponseEntity response = JSON.parseObject(message, new TypeReference() { 32 | }); 33 | if (response.getCode() >= 1000 && response.getCode() < 2000) { 34 | delivery.onMessage(message, response); 35 | } else { 36 | ErrorResponse errorResponse = ResponseFactory.createErrorResponse(); 37 | errorResponse.setErrorCode(CODE_ERROR); 38 | Response textResponse = ResponseFactory.createTextResponse(); 39 | textResponse.setResponseData(message); 40 | errorResponse.setResponseData(textResponse); 41 | errorResponse.setReserved(response); 42 | onSendDataError(errorResponse, delivery); 43 | } 44 | } catch (JSONException e) { 45 | ErrorResponse errorResponse = ResponseFactory.createErrorResponse(); 46 | Response textResponse = ResponseFactory.createTextResponse(); 47 | textResponse.setResponseData(message); 48 | errorResponse.setResponseData(textResponse); 49 | errorResponse.setErrorCode(JSON_ERROR); 50 | errorResponse.setCause(e); 51 | onSendDataError(errorResponse, delivery); 52 | } 53 | } 54 | 55 | /** 56 | * 统一处理错误信息, 57 | * 界面上可使用 ErrorResponse#getDescription() 来当做提示语 58 | */ 59 | @Override 60 | public void onSendDataError(ErrorResponse error, ResponseDelivery delivery) { 61 | switch (error.getErrorCode()) { 62 | case ErrorResponse.ERROR_NO_CONNECT: 63 | error.setDescription("网络错误"); 64 | break; 65 | case ErrorResponse.ERROR_UN_INIT: 66 | error.setDescription("连接未初始化"); 67 | break; 68 | case ErrorResponse.ERROR_UNKNOWN: 69 | error.setDescription("未知错误"); 70 | break; 71 | case JSON_ERROR: 72 | error.setDescription("数据格式异常"); 73 | break; 74 | case CODE_ERROR: 75 | error.setDescription("响应码错误"); 76 | break; 77 | } 78 | delivery.onSendDataError(error); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /samples/dispatersample/src/main/java/com/zhangke/smaple/dispatersample/CommonResponseEntity.java: -------------------------------------------------------------------------------- 1 | package com.zhangke.smaple.dispatersample; 2 | 3 | /** 4 | * 后台接口返回的数据格式 5 | * Created by ZhangKe on 2018/6/27. 6 | */ 7 | public class CommonResponseEntity { 8 | 9 | private String message; 10 | private String data; 11 | private int code; 12 | private String path; 13 | 14 | public String getMessage() { 15 | return message; 16 | } 17 | 18 | public void setMessage(String message) { 19 | this.message = message; 20 | } 21 | 22 | public String getData() { 23 | return data; 24 | } 25 | 26 | public void setData(String data) { 27 | this.data = data; 28 | } 29 | 30 | public int getCode() { 31 | return code; 32 | } 33 | 34 | public void setCode(int code) { 35 | this.code = code; 36 | } 37 | 38 | public String getPath() { 39 | return path; 40 | } 41 | 42 | public void setPath(String path) { 43 | this.path = path; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /samples/dispatersample/src/main/java/com/zhangke/smaple/dispatersample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhangke.smaple.dispatersample; 2 | 3 | import android.os.Bundle; 4 | import android.text.TextUtils; 5 | import android.view.View; 6 | import android.widget.EditText; 7 | import android.widget.ScrollView; 8 | import android.widget.TextView; 9 | import android.widget.Toast; 10 | 11 | import androidx.appcompat.app.AppCompatActivity; 12 | 13 | import com.zhangke.websocket.SimpleListener; 14 | import com.zhangke.websocket.SocketListener; 15 | import com.zhangke.websocket.WebSocketHandler; 16 | import com.zhangke.websocket.response.ErrorResponse; 17 | 18 | import org.java_websocket.framing.Framedata; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | private EditText etContent; 25 | private TextView tvMsg; 26 | private ScrollView scrollView; 27 | 28 | private SocketListener socketListener = new SimpleListener() { 29 | @Override 30 | public void onConnected() { 31 | appendMsgDisplay("onConnected"); 32 | } 33 | 34 | @Override 35 | public void onConnectFailed(Throwable e) { 36 | if (e != null) { 37 | appendMsgDisplay("onConnectFailed:" + e.toString()); 38 | } else { 39 | appendMsgDisplay("onConnectFailed:null"); 40 | } 41 | } 42 | 43 | @Override 44 | public void onDisconnect() { 45 | appendMsgDisplay("onDisconnect"); 46 | } 47 | 48 | @Override 49 | public void onSendDataError(ErrorResponse errorResponse) { 50 | appendMsgDisplay(errorResponse.getDescription()); 51 | errorResponse.release(); 52 | } 53 | 54 | @Override 55 | public void onMessage(String message, T data) { 56 | if (data instanceof CommonResponseEntity) { 57 | CommonResponseEntity responseEntity = (CommonResponseEntity) data; 58 | appendMsgDisplay(responseEntity.getMessage()); 59 | } 60 | } 61 | }; 62 | 63 | @Override 64 | protected void onCreate(Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | setContentView(R.layout.activity_main); 67 | 68 | initView(); 69 | 70 | WebSocketHandler.getDefault().addListener(socketListener); 71 | 72 | } 73 | 74 | private void initView() { 75 | etContent = (EditText) findViewById(R.id.et_content); 76 | tvMsg = (TextView) findViewById(R.id.tv_msg); 77 | scrollView = (ScrollView) findViewById(R.id.scroll_view); 78 | 79 | findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() { 80 | @Override 81 | public void onClick(View v) { 82 | String text = etContent.getText().toString(); 83 | if (TextUtils.isEmpty(text)) { 84 | Toast.makeText(MainActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show(); 85 | return; 86 | } 87 | WebSocketHandler.getDefault().send(text); 88 | } 89 | }); 90 | } 91 | 92 | @Override 93 | protected void onDestroy() { 94 | super.onDestroy(); 95 | WebSocketHandler.getDefault().removeListener(socketListener); 96 | } 97 | 98 | private void appendMsgDisplay(String msg) { 99 | StringBuilder textBuilder = new StringBuilder(); 100 | if (!TextUtils.isEmpty(tvMsg.getText())) { 101 | textBuilder.append(tvMsg.getText().toString()); 102 | textBuilder.append("\n"); 103 | } 104 | textBuilder.append(msg); 105 | textBuilder.append("\n"); 106 | tvMsg.setText(textBuilder.toString()); 107 | tvMsg.post(new Runnable() { 108 | @Override 109 | public void run() { 110 | scrollView.fullScroll(ScrollView.FOCUS_DOWN); 111 | } 112 | }); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /samples/dispatersample/src/main/res/drawable/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xZhangKe/WebSocketDemo/df9f1b8c0fa40444198aa6c6f4c9c0f7332b2539/samples/dispatersample/src/main/res/drawable/ic_launcher.png -------------------------------------------------------------------------------- /samples/dispatersample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 19 | 20 |