├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── drawable-hdpi │ │ │ │ ├── search1.png │ │ │ │ ├── input_bg_gray.9.png │ │ │ │ ├── jmui_sending_img.png │ │ │ │ ├── send_msg_normal.9.png │ │ │ │ ├── send_msg_press.9.png │ │ │ │ ├── business_card_bg.9.png │ │ │ │ ├── receive_msg_normal.9.png │ │ │ │ ├── receive_msg_press.9.png │ │ │ │ ├── jmui_send_error_normal.png │ │ │ │ └── jmui_send_error_press.png │ │ │ ├── drawable-xhdpi │ │ │ │ ├── group.png │ │ │ │ ├── verification.png │ │ │ │ ├── voip_toast_bg.9.png │ │ │ │ └── contact_head_icon.png │ │ │ ├── drawable-xxhdpi │ │ │ │ ├── back.png │ │ │ │ ├── icon.png │ │ │ │ ├── icon_add_nomal.png │ │ │ │ ├── icon_add_press.png │ │ │ │ ├── icon_face_nomal.png │ │ │ │ ├── icon_voice_nomal.png │ │ │ │ ├── icon_voice_press.png │ │ │ │ └── jmui_chat_detail.png │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable │ │ │ │ ├── jmui_back_btn_bg.xml │ │ │ │ ├── btn_send_bg_disable.xml │ │ │ │ ├── jmui_msg_send_bg.xml │ │ │ │ ├── jmui_msg_receive_bg.xml │ │ │ │ ├── btn_multi_bg.xml │ │ │ │ ├── btn_voice_or_text.xml │ │ │ │ ├── send_message_failed.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── layout │ │ │ │ ├── activity_main.xml │ │ │ │ ├── item_chat_receive_text.xml │ │ │ │ ├── view_keyboard_xhs.xml │ │ │ │ └── item_chat_send_text.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── yxc │ │ │ │ └── websocketclientdemo │ │ │ │ ├── util │ │ │ │ └── Util.java │ │ │ │ ├── modle │ │ │ │ └── ChatMessage.java │ │ │ │ ├── im │ │ │ │ ├── JWebSocketClient.java │ │ │ │ └── JWebSocketClientService.java │ │ │ │ ├── adapter │ │ │ │ └── Adapter_ChatMessage.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── yxc │ │ │ └── websocketclientdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── yxc │ │ └── websocketclientdemo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── screenshot ├── chat.jpg ├── chatntf.jpg ├── lockmsg.jpg ├── notification.jpg └── qrcode_chaoyoung.jpg ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── vcs.xml ├── modules.xml ├── misc.xml ├── runConfigurations.xml └── codeStyles │ └── Project.xml ├── gradle.properties ├── README.md ├── .gitignore ├── gradlew.bat ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /screenshot/chat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/chat.jpg -------------------------------------------------------------------------------- /screenshot/chatntf.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/chatntf.jpg -------------------------------------------------------------------------------- /screenshot/lockmsg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/lockmsg.jpg -------------------------------------------------------------------------------- /screenshot/notification.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/notification.jpg -------------------------------------------------------------------------------- /screenshot/qrcode_chaoyoung.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/screenshot/qrcode_chaoyoung.jpg -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | WebSocketClientDemo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/search1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/search1.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/group.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/back.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/input_bg_gray.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/input_bg_gray.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/verification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/verification.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/jmui_sending_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/jmui_sending_img.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/send_msg_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/send_msg_normal.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/send_msg_press.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/send_msg_press.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/voip_toast_bg.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/voip_toast_bg.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_add_nomal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_add_nomal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_add_press.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_add_press.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_face_nomal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_face_nomal.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/business_card_bg.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/business_card_bg.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/receive_msg_normal.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/receive_msg_normal.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/receive_msg_press.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/receive_msg_press.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/contact_head_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xhdpi/contact_head_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_voice_nomal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_voice_nomal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_voice_press.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/icon_voice_press.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/jmui_chat_detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-xxhdpi/jmui_chat_detail.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/jmui_send_error_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/jmui_send_error_normal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/jmui_send_error_press.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jinhuizxc/WebSocketClient-1/HEAD/app/src/main/res/drawable-hdpi/jmui_send_error_press.png -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Mar 11 16:34:53 CST 2019 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.10.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/jmui_back_btn_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20sp 4 | 5dp 5 | 28dp 6 | 48.5dp 7 | 40dp 8 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_send_bg_disable.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/yxc/websocketclientdemo/util/Util.java: -------------------------------------------------------------------------------- 1 | package com.yxc.websocketclientdemo.util; 2 | 3 | import android.content.Context; 4 | import android.widget.Toast; 5 | 6 | public class Util { 7 | public static final String ws = "ws://echo.websocket.org";//websocket测试地址 8 | 9 | public static void showToast(Context ctx, String msg) { 10 | Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/jmui_msg_send_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/jmui_msg_receive_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/test/java/com/yxc/websocketclientdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.yxc.websocketclientdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | #F5F5F5 8 | #ffffffff 9 | #ff000000 10 | #3072F3 11 | #2DD0CF 12 | #2DD0CF 13 | @color/white 14 | #0B1816 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_multi_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_voice_or_text.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/send_message_failed.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/yxc/websocketclientdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.yxc.websocketclientdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.yxc.websocketclientdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebSocketClient 2 | Android端websocket即时通讯demo 3 | 4 | 5 | #### 主要功能 6 | 1、与websocket建立长连接
7 | 2、与websocket进行即时通讯
8 | 3、Service和Activity之间通讯和UI更新
9 | 4、弹出消息通知(包括锁屏通知)
10 | 5、心跳检测和重连(保证websocket连接稳定性)
11 | 6、服务(Service)保活
12 | 13 | #### 效果图 14 |
15 | 16 | 17 |

18 | 19 | 20 |
21 | 22 | *** 23 | 更多技术干货,欢迎关注我的公众号:ChaoYoung 24 |
25 | -------------------------------------------------------------------------------- /app/src/main/java/com/yxc/websocketclientdemo/modle/ChatMessage.java: -------------------------------------------------------------------------------- 1 | package com.yxc.websocketclientdemo.modle; 2 | 3 | public class ChatMessage { 4 | private String content; 5 | private String time; 6 | private int isMeSend;//0是对方发送 1是自己发送 7 | private int isRead;//是否已读(0未读 1已读) 8 | 9 | public String getContent() { 10 | return content; 11 | } 12 | 13 | public void setContent(String content) { 14 | this.content = content; 15 | } 16 | 17 | public String getTime() { 18 | return time; 19 | } 20 | 21 | public void setTime(String time) { 22 | this.time = time; 23 | } 24 | 25 | public int getIsMeSend() { 26 | return isMeSend; 27 | } 28 | 29 | public void setIsMeSend(int isMeSend) { 30 | this.isMeSend = isMeSend; 31 | } 32 | 33 | public int getIsRead() { 34 | return isRead; 35 | } 36 | 37 | public void setIsRead(int isRead) { 38 | this.isRead = isRead; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/yxc/websocketclientdemo/im/JWebSocketClient.java: -------------------------------------------------------------------------------- 1 | package com.yxc.websocketclientdemo.im; 2 | 3 | import android.util.Log; 4 | 5 | import org.java_websocket.client.WebSocketClient; 6 | import org.java_websocket.drafts.Draft_6455; 7 | import org.java_websocket.handshake.ServerHandshake; 8 | 9 | import java.net.URI; 10 | 11 | public class JWebSocketClient extends WebSocketClient { 12 | public JWebSocketClient(URI serverUri) { 13 | super(serverUri, new Draft_6455()); 14 | } 15 | 16 | @Override 17 | public void onOpen(ServerHandshake handshakedata) { 18 | Log.e("JWebSocketClient", "onOpen()"); 19 | } 20 | 21 | @Override 22 | public void onMessage(String message) { 23 | Log.e("JWebSocketClient", "onMessage()"); 24 | } 25 | 26 | @Override 27 | public void onClose(int code, String reason, boolean remote) { 28 | Log.e("JWebSocketClient", "onClose()"); 29 | } 30 | 31 | @Override 32 | public void onError(Exception ex) { 33 | Log.e("JWebSocketClient", "onError()"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.yxc.websocketclientdemo" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | 29 | implementation "org.java-websocket:Java-WebSocket:1.4.0" 30 | } 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 21 | 25 | 30 | 31 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 15 | 21 | 22 | 23 | 29 | 30 | 39 | 40 | 47 | 48 | 49 | 50 | 57 | 58 | 59 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 17 | 28 | 36 | 47 | 53 | 61 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/yxc/websocketclientdemo/adapter/Adapter_ChatMessage.java: -------------------------------------------------------------------------------- 1 | package com.yxc.websocketclientdemo.adapter; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.BaseAdapter; 9 | import android.widget.TextView; 10 | 11 | 12 | import com.yxc.websocketclientdemo.R; 13 | import com.yxc.websocketclientdemo.modle.ChatMessage; 14 | 15 | import java.text.SimpleDateFormat; 16 | import java.util.Date; 17 | import java.util.List; 18 | 19 | public class Adapter_ChatMessage extends BaseAdapter { 20 | List mChatMessageList; 21 | LayoutInflater inflater; 22 | Context context; 23 | 24 | public Adapter_ChatMessage(Context context, List list) { 25 | this.mChatMessageList = list; 26 | this.context = context; 27 | inflater = LayoutInflater.from(context); 28 | } 29 | 30 | @Override 31 | public int getViewTypeCount() { 32 | return 2; 33 | } 34 | 35 | @Override 36 | public int getItemViewType(int position) { 37 | if (mChatMessageList.get(position).getIsMeSend() == 1) 38 | return 0;// 返回的数据位角标 39 | else 40 | return 1; 41 | } 42 | 43 | @Override 44 | public int getCount() { 45 | return mChatMessageList.size(); 46 | } 47 | 48 | 49 | @Override 50 | public Object getItem(int i) { 51 | return mChatMessageList.get(i); 52 | } 53 | 54 | @Override 55 | public long getItemId(int i) { 56 | return i; 57 | } 58 | 59 | @Override 60 | public View getView(int i, View view, ViewGroup viewGroup) { 61 | ChatMessage mChatMessage = mChatMessageList.get(i); 62 | String content = mChatMessage.getContent(); 63 | String time = formatTime(mChatMessage.getTime()); 64 | int isMeSend = mChatMessage.getIsMeSend(); 65 | int isRead = mChatMessage.getIsRead();////是否已读(0未读 1已读) 66 | final ViewHolder holder; 67 | if (view == null) { 68 | holder = new ViewHolder(); 69 | if (isMeSend == 0) {//对方发送 70 | view = inflater.inflate(R.layout.item_chat_receive_text, viewGroup, false); 71 | holder.tv_content = view.findViewById(R.id.tv_content); 72 | holder.tv_sendtime = view.findViewById(R.id.tv_sendtime); 73 | holder.tv_display_name = view.findViewById(R.id.tv_display_name); 74 | } else { 75 | view = inflater.inflate(R.layout.item_chat_send_text, viewGroup, false); 76 | holder.tv_content = view.findViewById(R.id.tv_content); 77 | holder.tv_sendtime = view.findViewById(R.id.tv_sendtime); 78 | holder.tv_isRead = view.findViewById(R.id.tv_isRead); 79 | } 80 | 81 | view.setTag(holder); 82 | } else { 83 | holder = (ViewHolder) view.getTag(); 84 | } 85 | 86 | 87 | holder.tv_sendtime.setText(time); 88 | holder.tv_content.setVisibility(View.VISIBLE); 89 | holder.tv_content.setText(content); 90 | 91 | 92 | //如果是自己发送才显示未读已读 93 | if (isMeSend == 1) { 94 | if (isRead == 0) { 95 | holder.tv_isRead.setText("未读"); 96 | holder.tv_isRead.setTextColor(context.getResources().getColor(R.color.jmui_jpush_blue)); 97 | } else if (isRead == 1) { 98 | holder.tv_isRead.setText("已读"); 99 | holder.tv_isRead.setTextColor(Color.GRAY); 100 | } else { 101 | holder.tv_isRead.setText(""); 102 | } 103 | }else{ 104 | holder.tv_display_name.setVisibility(View.VISIBLE); 105 | holder.tv_display_name.setText("服务器"); 106 | } 107 | 108 | return view; 109 | } 110 | 111 | class ViewHolder { 112 | private TextView tv_content, tv_sendtime, tv_display_name, tv_isRead; 113 | } 114 | 115 | 116 | /** 117 | * 将毫秒数转为日期格式 118 | * 119 | * @param timeMillis 120 | * @return 121 | */ 122 | private String formatTime(String timeMillis) { 123 | long timeMillisl=Long.parseLong(timeMillis); 124 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 125 | Date date = new Date(timeMillisl); 126 | return simpleDateFormat.format(date); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_chat_receive_text.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 10 | 11 | 12 | 16 | 17 | 20 | 21 | 26 | 27 | 33 | 34 | 50 | 51 | 52 | 60 | 61 | 64 | 65 | 74 | 75 | 80 | 81 | 91 | 92 | 102 | 103 | 104 | 105 | 111 | 112 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_keyboard_xhs.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 16 | 17 | 18 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 71 | 72 | 73 | 74 | 75 | 83 | 84 | 85 | 92 | 93 | 94 | 101 | 102 | 103 |