├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml ├── vcs.xml └── workspace.xml ├── .project ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── proguard-project.txt └── src └── main ├── AndroidManifest.xml └── java └── com └── codebutler └── android_websockets ├── HybiParser.java └── WebSocketClient.java /.gitignore: -------------------------------------------------------------------------------- 1 | gen/ 2 | build/ 3 | local.properties 4 | .gradle 5 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | android-websockets -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 28 | 29 | 30 | 48 | 55 | 56 | 57 | 68 | 69 | 82 | 83 | 84 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 106 | 107 | localhost 108 | 5050 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 46 | 47 | 48 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 72 | 73 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | 118 | 154 | 496 | 502 | 503 | 504 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 542 | 543 | 544 | 545 | 548 | 549 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 594 | 595 | 602 | 603 | 604 | 622 | 629 | 630 | 631 | 642 | 643 | 656 | 657 | 658 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 695 | 696 | 697 | 716 | 717 | 718 | 719 | 720 | 722 | 723 | localhost 724 | 5050 725 | 726 | 727 | 728 | 729 | 730 | 731 | 1368809717836 732 | 1368809717836 733 | 734 | 735 | 736 | 737 | 738 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 777 | 778 | 779 | 781 | 782 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 800 | 801 | 802 | 803 | 804 | 805 | Android 806 | 807 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 823 | 824 | 825 | 826 | 827 | 828 | 1.6 829 | 830 | 835 | 836 | 837 | 838 | 839 | 840 | irccloud-android 841 | 842 | 847 | 848 | 849 | 850 | 851 | 852 | Android 4.3 Platform 853 | 854 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | android-websockets 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebSocket client for Android 2 | 3 | A very simple bare-minimum WebSocket client for Android. 4 | 5 | ## Credits 6 | 7 | The hybi parser is based on code from the [faye project](https://github.com/faye/faye-websocket-node). Faye is Copyright (c) 2009-2012 James Coglan. Many thanks for the great open-source library! 8 | 9 | Ported from JavaScript to Java by [Eric Butler](https://twitter.com/codebutler) . 10 | 11 | ## Usage 12 | 13 | Here's the entire API: 14 | 15 | ```java 16 | List extraHeaders = Arrays.asList( 17 | new BasicNameValuePair("Cookie", "session=abcd"); 18 | ); 19 | 20 | WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Listener() { 21 | @Override 22 | public void onConnect() { 23 | Log.d(TAG, "Connected!"); 24 | } 25 | 26 | @Override 27 | public void onMessage(String message) { 28 | Log.d(TAG, String.format("Got string message! %s", message)); 29 | } 30 | 31 | @Override 32 | public void onMessage(byte[] data) { 33 | Log.d(TAG, String.format("Got binary message! %s", toHexString(data)); 34 | } 35 | 36 | @Override 37 | public void onDisconnect(int code, String reason) { 38 | Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason)); 39 | } 40 | 41 | @Override 42 | public void onError(Exception error) { 43 | Log.e(TAG, "Error!", error); 44 | } 45 | }, extraHeaders); 46 | 47 | client.connect(); 48 | 49 | // Later… 50 | client.send("hello!"); 51 | client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF }); 52 | client.disconnect(); 53 | ``` 54 | 55 | 56 | ## TODO 57 | 58 | * Run [autobahn tests](http://autobahn.ws/testsuite) 59 | * Investigate using [naga](http://code.google.com/p/naga/) instead of threads. 60 | 61 | ## License 62 | 63 | (The MIT License) 64 | 65 | Copyright (c) 2009-2012 James Coglan 66 | Copyright (c) 2012 Eric Butler 67 | 68 | Permission is hereby granted, free of charge, to any person obtaining a copy of 69 | this software and associated documentation files (the 'Software'), to deal in 70 | the Software without restriction, including without limitation the rights to use, 71 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 72 | Software, and to permit persons to whom the Software is furnished to do so, 73 | subject to the following conditions: 74 | 75 | The above copyright notice and this permission notice shall be included in all 76 | copies or substantial portions of the Software. 77 | 78 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 79 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 80 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 81 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 82 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 83 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 84 | 85 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | google() 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:4.0.0' 8 | } 9 | } 10 | apply plugin: 'com.android.library' 11 | 12 | repositories { 13 | google() 14 | jcenter() 15 | } 16 | 17 | android { 18 | namespace "com.codebutler.android_websockets" 19 | compileSdkVersion 31 20 | 21 | defaultConfig { 22 | minSdkVersion 14 23 | targetSdkVersion 31 24 | } 25 | sourceSets { 26 | main.java.srcDirs = ['src/main/java'] 27 | main.resources.srcDirs = ['src/main/java'] 28 | main.aidl.srcDirs = ['src/main/java'] 29 | main.renderscript.srcDirs = ['src/main/java'] 30 | main.res.srcDirs = ['res'] 31 | main.assets.srcDirs = ['assets'] 32 | } 33 | buildTypes { 34 | debugTest.initWith(debug) 35 | enterprisedebug.initWith(debug) 36 | enterpriserelease.initWith(release) 37 | mockdata.initWith(debug) 38 | } 39 | dependencies { 40 | implementation 'com.squareup.okhttp3:okhttp:3.10.0' 41 | } 42 | compileOptions { 43 | sourceCompatibility JavaVersion.VERSION_11 44 | targetCompatibility JavaVersion.VERSION_11 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irccloud/android-websockets/dcaae0c86e7755a4603d550d8f6091993b9fa885/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Feb 09 10:04:54 EST 2016 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-2.10-all.zip 7 | 8 | -------------------------------------------------------------------------------- /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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | 22 | -------------------------------------------------------------------------------- /src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/com/codebutler/android_websockets/HybiParser.java: -------------------------------------------------------------------------------- 1 | // 2 | // HybiParser.java: draft-ietf-hybi-thewebsocketprotocol-13 parser 3 | // 4 | // Based on code from the faye project. 5 | // https://github.com/faye/faye-websocket-node 6 | // Copyright (c) 2009-2012 James Coglan 7 | // 8 | // Ported from Javascript to Java by Eric Butler 9 | // 10 | // (The MIT License) 11 | // 12 | // Permission is hereby granted, free of charge, to any person obtaining 13 | // a copy of this software and associated documentation files (the 14 | // "Software"), to deal in the Software without restriction, including 15 | // without limitation the rights to use, copy, modify, merge, publish, 16 | // distribute, sublicense, and/or sell copies of the Software, and to 17 | // permit persons to whom the Software is furnished to do so, subject to 18 | // the following conditions: 19 | // 20 | // The above copyright notice and this permission notice shall be 21 | // included in all copies or substantial portions of the Software. 22 | // 23 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 27 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 28 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 29 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | 31 | package com.codebutler.android_websockets; 32 | 33 | import android.annotation.SuppressLint; 34 | import android.annotation.TargetApi; 35 | import android.net.TrafficStats; 36 | import android.os.Build; 37 | import android.util.Log; 38 | 39 | import java.io.*; 40 | import java.lang.reflect.Field; 41 | import java.util.Arrays; 42 | import java.util.List; 43 | import java.util.zip.DataFormatException; 44 | import java.util.zip.Deflater; 45 | import java.util.zip.Inflater; 46 | 47 | @TargetApi(9) 48 | public class HybiParser { 49 | private static final String TAG = "HybiParser"; 50 | 51 | private WebSocketClient mClient; 52 | 53 | private boolean mMasking = true; 54 | private boolean mDeflate = false; 55 | 56 | private int mStage; 57 | 58 | private boolean mFinal; 59 | private boolean mMasked; 60 | private boolean mDeflated; 61 | private int mOpcode; 62 | private int mLengthSize; 63 | private int mLength; 64 | private int mMode; 65 | 66 | private byte[] mMask = new byte[0]; 67 | private byte[] mPayload = new byte[0]; 68 | 69 | private boolean mClosed = false; 70 | 71 | private ByteArrayOutputStream mBuffer = new ByteArrayOutputStream(); 72 | private Inflater mInflater = new Inflater(true); 73 | private byte[] mInflateBuffer = new byte[4096]; 74 | private Deflater mDeflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); 75 | private byte[] mDeflateBuffer = new byte[4096]; 76 | 77 | private static final int BYTE = 255; 78 | private static final int FIN = 128; 79 | private static final int MASK = 128; 80 | private static final int RSV1 = 64; 81 | private static final int RSV2 = 32; 82 | private static final int RSV3 = 16; 83 | private static final int OPCODE = 15; 84 | private static final int LENGTH = 127; 85 | 86 | private static final int MODE_TEXT = 1; 87 | private static final int MODE_BINARY = 2; 88 | 89 | private static final int OP_CONTINUATION = 0; 90 | private static final int OP_TEXT = 1; 91 | private static final int OP_BINARY = 2; 92 | private static final int OP_CLOSE = 8; 93 | private static final int OP_PING = 9; 94 | private static final int OP_PONG = 10; 95 | 96 | private static final List OPCODES = Arrays.asList( 97 | OP_CONTINUATION, 98 | OP_TEXT, 99 | OP_BINARY, 100 | OP_CLOSE, 101 | OP_PING, 102 | OP_PONG 103 | ); 104 | 105 | private static final List FRAGMENTED_OPCODES = Arrays.asList( 106 | OP_CONTINUATION, OP_TEXT, OP_BINARY 107 | ); 108 | 109 | public HybiParser(WebSocketClient client) { 110 | mClient = client; 111 | 112 | if(Build.VERSION.SDK_INT < 19) { 113 | //Android's zlib wrapper doesn't expose Z_SYNC_FLUSH 114 | //So use a bit of reflection to enable it 115 | try { 116 | Field f = mDeflater.getClass().getDeclaredField("flushParm"); 117 | f.setAccessible(true); 118 | f.setInt(mDeflater, 2); // Z_SYNC_FLUSH 119 | } catch (Exception e) { 120 | e.printStackTrace(); 121 | } 122 | } 123 | } 124 | 125 | private static byte[] mask(byte[] payload, byte[] mask, int offset) { 126 | if (mask.length == 0) return payload; 127 | 128 | for (int i = 0; i < payload.length - offset; i++) { 129 | payload[offset + i] = (byte) (payload[offset + i] ^ mask[i % 4]); 130 | } 131 | return payload; 132 | } 133 | 134 | private byte[] deflate(byte[] payload) { 135 | ByteArrayOutputStream deflated = new ByteArrayOutputStream(); 136 | 137 | mDeflater.setInput(payload); 138 | while(!mDeflater.needsInput()) { 139 | int bytes; 140 | if(Build.VERSION.SDK_INT < 19) 141 | bytes = mDeflater.deflate(mDeflateBuffer); 142 | else 143 | bytes = mDeflater.deflate(mDeflateBuffer, 0, mDeflateBuffer.length, Deflater.SYNC_FLUSH); 144 | 145 | if(mDeflater.needsInput()) { 146 | //Strip the 0x00 0x00 0xFF 0xFF from the tail 147 | deflated.write(mDeflateBuffer, 0, bytes - 4); 148 | } else { 149 | deflated.write(mDeflateBuffer, 0, bytes); 150 | } 151 | } 152 | 153 | return deflated.toByteArray(); 154 | } 155 | 156 | private byte[] inflate(byte[] payload) throws DataFormatException { 157 | ByteArrayOutputStream inflated = new ByteArrayOutputStream(); 158 | 159 | mInflater.setInput(payload); 160 | while (!mInflater.needsInput()) { 161 | int chunkSize = mInflater.inflate(mInflateBuffer); 162 | inflated.write(mInflateBuffer, 0, chunkSize); 163 | } 164 | 165 | mInflater.setInput(new byte[] { 0, 0, -1, -1 }); 166 | while (!mInflater.needsInput()) { 167 | int chunkSize = mInflater.inflate(mInflateBuffer); 168 | inflated.write(mInflateBuffer, 0, chunkSize); 169 | } 170 | 171 | return inflated.toByteArray(); 172 | } 173 | 174 | public void setMasking(boolean masking) { 175 | mMasking = masking; 176 | } 177 | 178 | public void setDeflate(boolean deflate) { 179 | mDeflate = deflate; 180 | } 181 | 182 | public void start(HappyDataInputStream stream) throws IOException { 183 | while (true) { 184 | if (stream.available() == -1) break; 185 | switch (mStage) { 186 | case 0: 187 | parseOpcode(stream.readByte()); 188 | break; 189 | case 1: 190 | parseLength(stream.readByte()); 191 | break; 192 | case 2: 193 | parseExtendedLength(stream.readBytes(mLengthSize)); 194 | break; 195 | case 3: 196 | mMask = stream.readBytes(4); 197 | mStage = 4; 198 | break; 199 | case 4: 200 | mPayload = stream.readBytes(mLength); 201 | emitFrame(); 202 | mStage = 0; 203 | break; 204 | } 205 | } 206 | mClient.getListener().onDisconnect(0, "EOF"); 207 | } 208 | 209 | private void parseOpcode(byte data) throws ProtocolError { 210 | boolean rsv1 = (data & RSV1) == RSV1; 211 | boolean rsv2 = (data & RSV2) == RSV2; 212 | boolean rsv3 = (data & RSV3) == RSV3; 213 | 214 | if ((!mDeflate && rsv1) || rsv2 || rsv3) { 215 | throw new ProtocolError("RSV not zero"); 216 | } 217 | 218 | mFinal = (data & FIN) == FIN; 219 | mOpcode = (data & OPCODE); 220 | mDeflated = rsv1; 221 | mMask = new byte[0]; 222 | mPayload = new byte[0]; 223 | 224 | if (!OPCODES.contains(mOpcode)) { 225 | throw new ProtocolError("Bad opcode"); 226 | } 227 | 228 | if (!FRAGMENTED_OPCODES.contains(mOpcode) && !mFinal) { 229 | throw new ProtocolError("Expected non-final packet"); 230 | } 231 | 232 | mStage = 1; 233 | } 234 | 235 | private void parseLength(byte data) { 236 | mMasked = (data & MASK) == MASK; 237 | mLength = (data & LENGTH); 238 | 239 | if (mLength >= 0 && mLength <= 125) { 240 | mStage = mMasked ? 3 : 4; 241 | } else { 242 | mLengthSize = (mLength == 126) ? 2 : 8; 243 | mStage = 2; 244 | } 245 | } 246 | 247 | private void parseExtendedLength(byte[] buffer) throws ProtocolError { 248 | mLength = getInteger(buffer); 249 | mStage = mMasked ? 3 : 4; 250 | } 251 | 252 | public byte[] frame(String data) { 253 | return frame(data, OP_TEXT, -1); 254 | } 255 | 256 | public byte[] frame(byte[] data) { 257 | return frame(data, OP_BINARY, -1); 258 | } 259 | 260 | private byte[] frame(byte[] data, int opcode, int errorCode) { 261 | return frame((Object)data, opcode, errorCode); 262 | } 263 | 264 | private byte[] frame(String data, int opcode, int errorCode) { 265 | return frame((Object)data, opcode, errorCode); 266 | } 267 | 268 | private byte[] frame(Object data, int opcode, int errorCode) { 269 | if (mClosed) return null; 270 | 271 | byte[] buffer = (data instanceof String) ? decode((String) data) : (byte[]) data; 272 | if(mDeflate) { 273 | buffer = deflate(buffer); 274 | } 275 | int insert = (errorCode > 0) ? 2 : 0; 276 | int length = buffer.length + insert; 277 | int header = (length <= 125) ? 2 : (length <= 65535 ? 4 : 10); 278 | int offset = header + (mMasking ? 4 : 0); 279 | int masked = mMasking ? MASK : 0; 280 | byte[] frame = new byte[length + offset]; 281 | 282 | frame[0] = (byte) ((byte)FIN | (byte)opcode); 283 | if(mDeflate) 284 | frame[0] |= RSV1; 285 | 286 | if (length <= 125) { 287 | frame[1] = (byte) (masked | length); 288 | } else if (length <= 65535) { 289 | frame[1] = (byte) (masked | 126); 290 | frame[2] = (byte) Math.floor(length / 256); 291 | frame[3] = (byte) (length & BYTE); 292 | } else { 293 | frame[1] = (byte) (masked | 127); 294 | frame[2] = (byte) (((int) Math.floor(length / Math.pow(2, 56))) & BYTE); 295 | frame[3] = (byte) (((int) Math.floor(length / Math.pow(2, 48))) & BYTE); 296 | frame[4] = (byte) (((int) Math.floor(length / Math.pow(2, 40))) & BYTE); 297 | frame[5] = (byte) (((int) Math.floor(length / Math.pow(2, 32))) & BYTE); 298 | frame[6] = (byte) (((int) Math.floor(length / Math.pow(2, 24))) & BYTE); 299 | frame[7] = (byte) (((int) Math.floor(length / Math.pow(2, 16))) & BYTE); 300 | frame[8] = (byte) (((int) Math.floor(length / Math.pow(2, 8))) & BYTE); 301 | frame[9] = (byte) (length & BYTE); 302 | } 303 | 304 | if (errorCode > 0) { 305 | frame[offset] = (byte) (((int) Math.floor(errorCode / 256)) & BYTE); 306 | frame[offset+1] = (byte) (errorCode & BYTE); 307 | } 308 | System.arraycopy(buffer, 0, frame, offset + insert, buffer.length); 309 | 310 | if (mMasking) { 311 | byte[] mask = { 312 | (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256), 313 | (byte) Math.floor(Math.random() * 256), (byte) Math.floor(Math.random() * 256) 314 | }; 315 | System.arraycopy(mask, 0, frame, header, mask.length); 316 | mask(frame, mask, offset); 317 | } 318 | 319 | return frame; 320 | } 321 | 322 | public void ping(String message) { 323 | mClient.send(frame(message, OP_PING, -1)); 324 | } 325 | 326 | public void close(int code, String reason) { 327 | if (mClosed) return; 328 | mClient.send(frame(reason, OP_CLOSE, code)); 329 | mClosed = true; 330 | } 331 | 332 | @SuppressLint("NewApi") 333 | private void emitFrame() throws IOException { 334 | byte[] payload = mask(mPayload, mMask, 0); 335 | if (mDeflated) { 336 | try { 337 | payload = inflate(payload); 338 | } catch (DataFormatException e) { 339 | throw new IOException("Invalid deflated data"); 340 | } 341 | } 342 | int opcode = mOpcode; 343 | 344 | if (opcode == OP_CONTINUATION) { 345 | if (mMode == 0) { 346 | throw new ProtocolError("Mode was not set."); 347 | } 348 | mBuffer.write(payload); 349 | if (mFinal) { 350 | byte[] message = mBuffer.toByteArray(); 351 | if (mMode == MODE_TEXT) { 352 | mClient.getListener().onMessage(encode(message)); 353 | } else { 354 | mClient.getListener().onMessage(message); 355 | } 356 | reset(); 357 | if(Build.VERSION.SDK_INT >= 14) 358 | TrafficStats.incrementOperationCount(1); 359 | } 360 | 361 | } else if (opcode == OP_TEXT) { 362 | if (mFinal) { 363 | String messageText = encode(payload); 364 | mClient.getListener().onMessage(messageText); 365 | } else { 366 | mMode = MODE_TEXT; 367 | mBuffer.write(payload); 368 | } 369 | if(Build.VERSION.SDK_INT >= 14) 370 | TrafficStats.incrementOperationCount(1); 371 | 372 | } else if (opcode == OP_BINARY) { 373 | if (mFinal) { 374 | mClient.getListener().onMessage(payload); 375 | } else { 376 | mMode = MODE_BINARY; 377 | mBuffer.write(payload); 378 | } 379 | if(Build.VERSION.SDK_INT >= 14) 380 | TrafficStats.incrementOperationCount(1); 381 | 382 | } else if (opcode == OP_CLOSE) { 383 | int code = (payload.length >= 2) ? 256 * payload[0] + payload[1] : 0; 384 | String reason = (payload.length > 2) ? encode(slice(payload, 2)) : null; 385 | Log.d(TAG, "Got close op! " + code + " " + reason); 386 | mClient.getListener().onDisconnect(code, reason); 387 | 388 | } else if (opcode == OP_PING) { 389 | if (payload.length > 125) { throw new ProtocolError("Ping payload too large"); } 390 | Log.d(TAG, "Sending pong!!"); 391 | mClient.sendFrame(frame(payload, OP_PONG, -1)); 392 | 393 | } else if (opcode == OP_PONG) { 394 | String message = encode(payload); 395 | // FIXME: Fire callback... 396 | Log.d(TAG, "Got pong! " + message); 397 | } 398 | } 399 | 400 | private void reset() { 401 | mMode = 0; 402 | mBuffer.reset(); 403 | } 404 | 405 | private String encode(byte[] buffer) { 406 | try { 407 | return new String(buffer, "UTF-8"); 408 | } catch (UnsupportedEncodingException e) { 409 | throw new RuntimeException(e); 410 | } 411 | } 412 | 413 | private byte[] decode(String string) { 414 | try { 415 | return (string).getBytes("UTF-8"); 416 | } catch (UnsupportedEncodingException e) { 417 | throw new RuntimeException(e); 418 | } 419 | } 420 | 421 | private int getInteger(byte[] bytes) throws ProtocolError { 422 | long i = byteArrayToLong(bytes, 0, bytes.length); 423 | if (i < 0 || i > Integer.MAX_VALUE) { 424 | throw new ProtocolError("Bad integer: " + i); 425 | } 426 | return (int) i; 427 | } 428 | 429 | private byte[] slice(byte[] array, int start) { 430 | return Arrays.copyOfRange(array, start, array.length); 431 | } 432 | 433 | public static class ProtocolError extends IOException { 434 | public ProtocolError(String detailMessage) { 435 | super(detailMessage); 436 | } 437 | } 438 | 439 | private static long byteArrayToLong(byte[] b, int offset, int length) { 440 | if (b.length < length) 441 | throw new IllegalArgumentException("length must be less than or equal to b.length"); 442 | 443 | long value = 0; 444 | for (int i = 0; i < length; i++) { 445 | int shift = (length - 1 - i) * 8; 446 | value += (b[i + offset] & 0x000000FF) << shift; 447 | } 448 | return value; 449 | } 450 | 451 | public static class HappyDataInputStream extends DataInputStream { 452 | public HappyDataInputStream(InputStream in) { 453 | super(in); 454 | } 455 | 456 | public byte[] readBytes(int length) throws IOException { 457 | byte[] buffer = new byte[length]; 458 | readFully(buffer); 459 | return buffer; 460 | } 461 | } 462 | } -------------------------------------------------------------------------------- /src/main/java/com/codebutler/android_websockets/WebSocketClient.java: -------------------------------------------------------------------------------- 1 | package com.codebutler.android_websockets; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.annotation.TargetApi; 5 | import android.net.TrafficStats; 6 | import android.os.Build; 7 | import android.os.Handler; 8 | import android.os.HandlerThread; 9 | import android.text.TextUtils; 10 | import android.util.Base64; 11 | import android.util.Log; 12 | import org.apache.http.conn.ssl.StrictHostnameVerifier; 13 | 14 | import javax.net.SocketFactory; 15 | import javax.net.ssl.SSLContext; 16 | import javax.net.ssl.SSLException; 17 | import javax.net.ssl.SSLSocket; 18 | import javax.net.ssl.SSLSocketFactory; 19 | import javax.net.ssl.TrustManager; 20 | import java.io.EOFException; 21 | import java.io.IOException; 22 | import java.io.OutputStream; 23 | import java.io.PrintWriter; 24 | import java.net.HttpURLConnection; 25 | import java.net.InetAddress; 26 | import java.net.InetSocketAddress; 27 | import java.net.Socket; 28 | import java.net.SocketException; 29 | import java.net.URI; 30 | import java.security.KeyManagementException; 31 | import java.security.MessageDigest; 32 | import java.security.NoSuchAlgorithmException; 33 | import java.util.ArrayList; 34 | import java.util.Arrays; 35 | import java.util.List; 36 | import java.util.Map; 37 | 38 | import okhttp3.Headers; 39 | import okhttp3.internal.http.StatusLine; 40 | 41 | @TargetApi(8) 42 | public class WebSocketClient { 43 | private static final String TAG = "WebSocketClient"; 44 | private int mSocketTag = -1; 45 | 46 | private URI mURI; 47 | private Listener mListener; 48 | private DebugListener mDebugListener; 49 | private Socket mSocket; 50 | private Thread mThread; 51 | private static final HandlerThread mHandlerThread = new HandlerThread("websocket-thread"); 52 | private Handler mHandler; 53 | private Map mExtraHeaders; 54 | private HybiParser mParser; 55 | private String mProxyHost; 56 | private int mProxyPort; 57 | 58 | static final String[] ENABLED_CIPHERS = { 59 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", 60 | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", 61 | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", 62 | "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", 63 | "TLS_DHE_RSA_WITH_AES_128_CBC_SHA", 64 | "TLS_DHE_RSA_WITH_AES_256_CBC_SHA", 65 | "TLS_ECDHE_RSA_WITH_RC4_128_SHA", 66 | "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", 67 | "TLS_RSA_WITH_AES_128_CBC_SHA", 68 | "TLS_RSA_WITH_AES_256_CBC_SHA", 69 | "SSL_RSA_WITH_3DES_EDE_CBC_SHA", 70 | "SSL_RSA_WITH_RC4_128_SHA", 71 | "SSL_RSA_WITH_RC4_128_MD5", 72 | }; 73 | 74 | static final String[] ENABLED_PROTOCOLS = { 75 | "TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1" 76 | }; 77 | 78 | private final Object mSendLock = new Object(); 79 | 80 | private static TrustManager[] sTrustManagers; 81 | 82 | public static void setTrustManagers(TrustManager[] tm) { 83 | sTrustManagers = tm; 84 | } 85 | 86 | public WebSocketClient(URI uri, Listener listener, Map extraHeaders) { 87 | mURI = uri; 88 | mListener = listener; 89 | mExtraHeaders = extraHeaders; 90 | 91 | if(!mHandlerThread.isAlive()) 92 | mHandlerThread.start(); 93 | mHandler = new Handler(mHandlerThread.getLooper()); 94 | } 95 | 96 | public Listener getListener() { 97 | return mListener; 98 | } 99 | 100 | public void setListener(Listener listener) { 101 | mListener = listener; 102 | } 103 | 104 | public DebugListener getDebugListener() { 105 | return mDebugListener; 106 | } 107 | 108 | public void setDebugListener(DebugListener listener) { 109 | mDebugListener = listener; 110 | } 111 | 112 | private ArrayList mSocketThreads = new ArrayList<>(); 113 | 114 | private class ConnectRunnable implements Runnable { 115 | private SocketFactory mSocketFactory; 116 | private InetSocketAddress mAddress; 117 | 118 | ConnectRunnable(SocketFactory factory, InetSocketAddress address) { 119 | mSocketFactory = factory; 120 | mAddress = address; 121 | } 122 | 123 | @Override 124 | public void run() { 125 | try { 126 | if (mDebugListener != null) 127 | mDebugListener.onDebugMsg("Connecting to address: " + mAddress.getAddress() + " port: " + mAddress.getPort()); 128 | Socket socket = mSocketFactory.createSocket(); 129 | if(Build.VERSION.SDK_INT < 24) 130 | socket.getClass().getMethod("setHostname", String.class).invoke(socket, mURI.getHost()); 131 | socket.connect(mAddress, 30000); 132 | if(mSocket == null) { 133 | mSocket = socket; 134 | if (mDebugListener != null) 135 | mDebugListener.onDebugMsg("Connected to " + mAddress.getAddress()); 136 | if (mURI.getScheme().equals("wss")) { 137 | SSLSocket s = (SSLSocket) mSocket; 138 | try { 139 | ArrayList protocols = new ArrayList<>(Arrays.asList(ENABLED_PROTOCOLS)); 140 | protocols.retainAll(Arrays.asList(s.getSupportedProtocols())); 141 | Log.d(TAG, "Enabling protocols: " + protocols); 142 | s.setEnabledProtocols(protocols.toArray(new String[protocols.size()])); 143 | } catch (IllegalArgumentException e) { 144 | //Not supported on older Android versions 145 | } 146 | try { 147 | ArrayList ciphers = new ArrayList<>(Arrays.asList(ENABLED_CIPHERS)); 148 | ciphers.retainAll(Arrays.asList(s.getSupportedCipherSuites())); 149 | Log.d(TAG, "Enabling ciphers: " + ciphers); 150 | s.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()])); 151 | } catch (IllegalArgumentException e) { 152 | //Not supported on older Android versions 153 | } 154 | } 155 | start_socket_thread(); 156 | } else { 157 | socket.close(); 158 | } 159 | } catch (SSLException ex) { 160 | ex.printStackTrace(); 161 | if(mSocket == null && mSocketThreads.size() == 1) { 162 | Log.d(TAG, "Websocket SSL error!", ex); 163 | if (mListener != null) 164 | mListener.onDisconnect(0, "SSL"); 165 | } 166 | } catch (Exception ex) { 167 | ex.printStackTrace(); 168 | if(mSocket == null && mSocketThreads.size() == 1) { 169 | if (mListener != null) 170 | mListener.onError(ex); 171 | } 172 | } 173 | mSocketThreads.remove(Thread.currentThread()); 174 | } 175 | } 176 | 177 | public void connect() { 178 | if (mThread != null && mThread.isAlive()) { 179 | return; 180 | } 181 | 182 | mThread = new Thread(new Runnable() { 183 | @Override 184 | public void run() { 185 | try { 186 | int port = (mURI.getPort() != -1) ? mURI.getPort() : (mURI.getScheme().equals("wss") ? 443 : 80); 187 | SocketFactory factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory() : SocketFactory.getDefault(); 188 | if (mProxyHost != null && mProxyHost.length() > 0) { 189 | if (mDebugListener != null) 190 | mDebugListener.onDebugMsg("Connecting to proxy: " + mProxyHost + " port: " + mProxyPort); 191 | mSocket = SocketFactory.getDefault().createSocket(mProxyHost, mProxyPort); 192 | start_socket_thread(); 193 | } else { 194 | InetAddress[] addresses = InetAddress.getAllByName(mURI.getHost()); 195 | for (InetAddress address : addresses) { 196 | if(mSocket == null) { 197 | Thread t = new Thread(new ConnectRunnable(factory, new InetSocketAddress(address, port))); 198 | mSocketThreads.add(t); 199 | t.start(); 200 | Thread.sleep(300); 201 | } else { 202 | break; 203 | } 204 | } 205 | } 206 | } catch (Exception ex) { 207 | if (mListener != null) 208 | mListener.onError(ex); 209 | } 210 | } 211 | }); 212 | mThread.start(); 213 | } 214 | 215 | private void start_socket_thread() { 216 | mThread = new Thread(new Runnable() { 217 | @SuppressLint("NewApi") 218 | public void run() { 219 | try { 220 | String secret = createSecret(); 221 | 222 | int port = (mURI.getPort() != -1) ? mURI.getPort() : (mURI.getScheme().equals("wss") ? 443 : 80); 223 | 224 | String path = TextUtils.isEmpty(mURI.getPath()) ? "/" : mURI.getPath(); 225 | if (!TextUtils.isEmpty(mURI.getQuery())) { 226 | path += "?" + mURI.getQuery(); 227 | } 228 | 229 | String originScheme = mURI.getScheme().equals("wss") ? "https" : "http"; 230 | URI origin = new URI(originScheme, "//" + mURI.getHost(), null); 231 | 232 | if(Build.VERSION.SDK_INT >= 14 && mSocketTag > 0) { 233 | TrafficStats.setThreadStatsTag(mSocketTag); 234 | TrafficStats.tagSocket(mSocket); 235 | } 236 | 237 | PrintWriter out = new PrintWriter(mSocket.getOutputStream()); 238 | 239 | if(mProxyHost != null && mProxyHost.length() > 0 && mProxyPort > 0) { 240 | out.print("CONNECT " + mURI.getHost() + ":" + port + " HTTP/1.0\r\n"); 241 | out.print("\r\n"); 242 | out.flush(); 243 | HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream()); 244 | 245 | // Read HTTP response status line. 246 | String statusLineString = readLine(stream); 247 | if (statusLineString == null) { 248 | throw new Exception("Received no reply from server."); 249 | } else { 250 | StatusLine statusLine = StatusLine.parse(statusLineString); 251 | if (statusLine.code != HttpURLConnection.HTTP_OK) { 252 | throw new Exception(statusLine.toString()); 253 | } 254 | } 255 | 256 | // Read HTTP response headers. 257 | while (!TextUtils.isEmpty(readLine(stream))); 258 | if(mURI.getScheme().equals("wss")) { 259 | mSocket = getSSLSocketFactory().createSocket(mSocket, mURI.getHost(), port, false); 260 | SSLSocket s = (SSLSocket)mSocket; 261 | try { 262 | s.setEnabledProtocols(ENABLED_PROTOCOLS); 263 | } catch (IllegalArgumentException e) { 264 | //Not supported on older Android versions 265 | e.printStackTrace(); 266 | } 267 | try { 268 | s.setEnabledCipherSuites(ENABLED_CIPHERS); 269 | } catch (IllegalArgumentException e) { 270 | //Not supported on older Android versions 271 | e.printStackTrace(); 272 | } 273 | out = new PrintWriter(mSocket.getOutputStream()); 274 | } 275 | } 276 | 277 | if(mURI.getScheme().equals("wss")) { 278 | SSLSocket s = (SSLSocket) mSocket; 279 | StrictHostnameVerifier verifier = new StrictHostnameVerifier(); 280 | if (!verifier.verify(mURI.getHost(), s.getSession())) 281 | throw new SSLException("Hostname mismatch"); 282 | } 283 | 284 | out.print("GET " + path + " HTTP/1.1\r\n"); 285 | out.print("Upgrade: websocket\r\n"); 286 | out.print("Connection: Upgrade\r\n"); 287 | out.print("Host: " + mURI.getHost() + "\r\n"); 288 | out.print("Origin: " + origin.toString() + "\r\n"); 289 | out.print("Sec-WebSocket-Key: " + secret + "\r\n"); 290 | out.print("Sec-WebSocket-Version: 13\r\n"); 291 | out.print("Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n"); 292 | if (mExtraHeaders != null) { 293 | for (String key : mExtraHeaders.keySet()) { 294 | out.print(String.format("%s: %s\r\n", key, mExtraHeaders.get(key))); 295 | } 296 | } 297 | out.print("\r\n"); 298 | out.flush(); 299 | 300 | mParser = new HybiParser(WebSocketClient.this); 301 | HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream()); 302 | 303 | // Read HTTP response status line. 304 | String statusLineString = readLine(stream); 305 | if (statusLineString == null) { 306 | throw new Exception("Received no reply from server."); 307 | } else { 308 | StatusLine statusLine = StatusLine.parse(statusLineString); 309 | if (statusLine.code != 101) { 310 | throw new Exception(statusLine.toString()); 311 | } 312 | } 313 | 314 | // Read HTTP response headers. 315 | String line; 316 | boolean validated = false; 317 | 318 | while (!TextUtils.isEmpty(line = readLine(stream))) { 319 | int index = line.indexOf(":"); 320 | Headers header = new Headers.Builder().add(line.substring(0, index).trim(), line.substring(index + 1)).build(); 321 | if (header.name(0).equalsIgnoreCase("Sec-WebSocket-Accept")) { 322 | String expected = createSecretValidation(secret); 323 | String actual = header.value(0).trim(); 324 | 325 | if (!expected.equals(actual)) { 326 | throw new Exception("Bad Sec-WebSocket-Accept header value."); 327 | } 328 | 329 | validated = true; 330 | } else if(header.name(0).equalsIgnoreCase("Sec-WebSocket-Extensions")) { 331 | if(header.value(0).trim().equalsIgnoreCase("x-webkit-deflate-frame")) 332 | mParser.setDeflate(true); 333 | } 334 | } 335 | 336 | if (!validated) { 337 | throw new Exception("No Sec-WebSocket-Accept header."); 338 | } 339 | 340 | if(mListener != null) 341 | mListener.onConnect(); 342 | 343 | // Now decode websocket frames. 344 | mParser.start(stream); 345 | 346 | } catch (EOFException ex) { 347 | Log.d(TAG, "WebSocket EOF!", ex); 348 | try { 349 | if(mListener != null) 350 | mListener.onDisconnect(0, "EOF"); 351 | } catch (Exception e) { 352 | } 353 | } catch (SSLException ex) { 354 | // Connection reset by peer 355 | Log.d(TAG, "Websocket SSL error!", ex); 356 | try { 357 | if(mListener != null) 358 | mListener.onDisconnect(0, "SSL"); 359 | } catch (Exception e) { 360 | } 361 | } catch (Exception ex) { 362 | try { 363 | ex.printStackTrace(); 364 | if (mListener != null) 365 | mListener.onError(ex); 366 | } catch (Exception e) { 367 | } 368 | } 369 | } 370 | }); 371 | mThread.setName("websocket-parser-thread"); 372 | mThread.start(); 373 | } 374 | 375 | public void disconnect() { 376 | if (mSocket != null) { 377 | mHandler.post(new Runnable() { 378 | public void run() { 379 | try { 380 | if(mSocket != null) 381 | mSocket.close(); 382 | mSocket = null; 383 | } catch (IOException ex) { 384 | Log.d(TAG, "Error while disconnecting", ex); 385 | if(mListener != null) 386 | mListener.onError(ex); 387 | } 388 | } 389 | }); 390 | } 391 | } 392 | 393 | public void send(String data) { 394 | sendFrame(mParser.frame(data)); 395 | } 396 | 397 | public void send(byte[] data) { 398 | sendFrame(mParser.frame(data)); 399 | } 400 | 401 | // Can't use BufferedReader because it buffers past the HTTP data. 402 | private String readLine(HybiParser.HappyDataInputStream reader) throws IOException { 403 | int readChar = reader.read(); 404 | if (readChar == -1) { 405 | return null; 406 | } 407 | StringBuilder string = new StringBuilder(""); 408 | while (readChar != '\n') { 409 | if (readChar != '\r') { 410 | string.append((char) readChar); 411 | } 412 | 413 | readChar = reader.read(); 414 | if (readChar == -1) { 415 | return null; 416 | } 417 | } 418 | return string.toString(); 419 | } 420 | 421 | private String createSecret() { 422 | byte[] nonce = new byte[16]; 423 | for (int i = 0; i < 16; i++) { 424 | nonce[i] = (byte) (Math.random() * 256); 425 | } 426 | return Base64.encodeToString(nonce, Base64.DEFAULT).trim(); 427 | } 428 | 429 | private String createSecretValidation(String secret) { 430 | try { 431 | MessageDigest md = MessageDigest.getInstance("SHA-1"); 432 | md.update((secret + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes()); 433 | return Base64.encodeToString(md.digest(), Base64.DEFAULT).trim(); 434 | } catch (NoSuchAlgorithmException e) { 435 | throw new RuntimeException(e); 436 | } 437 | } 438 | 439 | void sendFrame(final byte[] frame) { 440 | mHandler.post(new Runnable() { 441 | @SuppressLint("NewApi") 442 | public void run() { 443 | try { 444 | synchronized (mSendLock) { 445 | if (mSocket == null) { 446 | if(mListener != null) 447 | mListener.onError(new IllegalStateException("Socket not connected")); 448 | return; 449 | } 450 | OutputStream outputStream = mSocket.getOutputStream(); 451 | outputStream.write(frame); 452 | outputStream.flush(); 453 | if(Build.VERSION.SDK_INT >= 14 && mSocketTag > 0) 454 | TrafficStats.incrementOperationCount(1); 455 | } 456 | } catch (IOException e) { 457 | if(mListener != null) 458 | mListener.onError(e); 459 | } 460 | } 461 | }); 462 | } 463 | 464 | public void setSocketTag(int tag) { 465 | mSocketTag = tag; 466 | if(Build.VERSION.SDK_INT >= 14 && mSocketTag > 0 && mSocket != null) { 467 | mHandler.post(new Runnable() { 468 | @TargetApi(14) 469 | public void run() { 470 | try { 471 | TrafficStats.setThreadStatsTag(mSocketTag); 472 | TrafficStats.tagSocket(mSocket); 473 | } catch (SocketException e) { 474 | if(mListener != null) 475 | mListener.onError(e); 476 | } 477 | } 478 | }); 479 | } 480 | } 481 | 482 | public void setProxy(String host, int port) { 483 | mProxyHost = host; 484 | mProxyPort = port; 485 | } 486 | 487 | public interface Listener { 488 | public void onConnect(); 489 | public void onMessage(String message); 490 | public void onMessage(byte[] data); 491 | public void onDisconnect(int code, String reason); 492 | public void onError(Exception error); 493 | } 494 | 495 | public interface DebugListener { 496 | public void onDebugMsg(String msg); 497 | } 498 | 499 | private SSLSocketFactory getSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { 500 | SSLContext context = SSLContext.getInstance("TLS"); 501 | context.init(null, sTrustManagers, null); 502 | return context.getSocketFactory(); 503 | } 504 | } 505 | --------------------------------------------------------------------------------