├── README.md
├── app
├── .gitignore
├── app.iml
├── build.gradle
├── libs
│ └── socket.io-client.java-master
│ │ ├── .gitignore
│ │ ├── .travis.yml
│ │ ├── History.md
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── pom.xml
│ │ └── src
│ │ ├── main
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── nkzawa
│ │ │ ├── backo
│ │ │ └── Backoff.java
│ │ │ ├── hasbinary
│ │ │ └── HasBinary.java
│ │ │ └── socketio
│ │ │ ├── client
│ │ │ ├── Ack.java
│ │ │ ├── IO.java
│ │ │ ├── Manager.java
│ │ │ ├── On.java
│ │ │ ├── Socket.java
│ │ │ ├── SocketIOException.java
│ │ │ └── Url.java
│ │ │ └── parser
│ │ │ ├── Binary.java
│ │ │ ├── Packet.java
│ │ │ └── Parser.java
│ │ └── test
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── nkzawa
│ │ │ ├── backo
│ │ │ └── BackoffTest.java
│ │ │ ├── hasbinary
│ │ │ └── HasBinaryTest.java
│ │ │ ├── socketio
│ │ │ ├── client
│ │ │ │ ├── Connection.java
│ │ │ │ ├── ConnectionTest.java
│ │ │ │ ├── ExecutionTest.java
│ │ │ │ ├── SSLConnectionTest.java
│ │ │ │ ├── ServerConnectionNamespaceTest.java
│ │ │ │ ├── ServerConnectionTest.java
│ │ │ │ ├── SocketTest.java
│ │ │ │ ├── UrlTest.java
│ │ │ │ └── executions
│ │ │ │ │ ├── Connection.java
│ │ │ │ │ ├── ConnectionFailure.java
│ │ │ │ │ └── ImmediateClose.java
│ │ │ └── parser
│ │ │ │ ├── ByteArrayTest.java
│ │ │ │ ├── Helpers.java
│ │ │ │ └── ParserTest.java
│ │ │ └── util
│ │ │ └── Optional.java
│ │ └── resources
│ │ ├── cert.pem
│ │ ├── key.pem
│ │ ├── keystore.jks
│ │ ├── logging.properties
│ │ ├── package.json
│ │ └── server.js
├── proguard-rules.pro
├── src.zip
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── kriyatma
│ │ └── nodesocket
│ │ └── ApplicationTest.java
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── kriyatma
│ │ └── nodesocket
│ │ ├── ChatFragment.java
│ │ ├── Message.java
│ │ ├── MessageAdapter.java
│ │ └── SocketActivity.java
│ └── res
│ ├── drawable-hdpi
│ ├── ic_action_attachment.png
│ └── ic_action_camera.png
│ ├── drawable-mdpi
│ ├── ic_action_attachment.png
│ └── ic_action_camera.png
│ ├── drawable-xhdpi
│ ├── ic_action_attachment.png
│ └── ic_action_camera.png
│ ├── drawable-xxhdpi
│ ├── ic_action_attachment.png
│ └── ic_action_camera.png
│ ├── layout
│ ├── activity_socket.xml
│ ├── fragment_chat.xml
│ └── layout_message.xml
│ ├── menu
│ ├── menu_socket.xml
│ └── socket_activity_actions.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── build
└── intermediates
│ ├── dex-cache
│ └── cache.xml
│ └── gradle_project_sync_data.bin
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle
/README.md:
--------------------------------------------------------------------------------
1 | # android-socket.io-demo
2 | A demo realtime chat application with Node and socket.io on server side and native android as client. This demo includes sending text message as well as images files at realtime.
3 | server folder contains all the back end code i.e nodejs and socket.io.
4 | client folder contains all the android java client side code.
5 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
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 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
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 |
101 |
102 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 22
5 | buildToolsVersion "22.0.1"
6 |
7 | defaultConfig {
8 | applicationId "com.kriyatma.nodesocket"
9 | minSdkVersion 14
10 | targetSdkVersion 22
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:appcompat-v7:22.0.0'
25 | compile "com.android.support:recyclerview-v7:21.0.0"
26 | compile "com.github.nkzawa:socket.io-client:0.5.0"
27 | }
28 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Package Files #
4 | *.jar
5 | *.war
6 | *.ear
7 |
8 | # Intellij project files
9 | *.iml
10 | *.ipr
11 | *.iws
12 | .idea/
13 |
14 | .DS_Store
15 | target/
16 | node_modules/
17 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | install: mvn install -DskipTests=true -Dgpg.skip=true
3 | jdk:
4 | - openjdk7
5 | - oraclejdk8
6 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/History.md:
--------------------------------------------------------------------------------
1 | 0.5.0 / 2015-05-02
2 | ==================
3 |
4 | * bump `engine.io-client`.
5 | * enhance parser decode [niqo01]
6 | * add a wrong event name check
7 | * add setDefaultHostnameVerifier method
8 |
9 | 0.4.2 / 2015-03-07
10 | ==================
11 |
12 | * fix error on reconnection attemps
13 |
14 | 0.4.1 / 2015-02-08
15 | ==================
16 |
17 | * bump `engine.io-client`.
18 | * fix program doesn't terminate when closing socket before eatablishing connection.
19 |
20 | 0.4.0 / 2015-01-26
21 | ==================
22 |
23 | * compatible with socket.io 1.3.2
24 | * bump `engine.io-client`.
25 | * added `Socket#id()` pointing to session id
26 | * add exponential backoff with randomization
27 | * reset reconnection attempts state after a successul connection
28 | * fix binary arguments in emit with ack [AlfioEmanueleFresta]
29 |
30 | 0.3.0 / 2014-11-04
31 | ==================
32 |
33 | * compatible with socket.io 1.2.0
34 | * bump `engine.io-client`.
35 | * fix reconnection after reconnecting manually
36 | * enable to stop reconnecting
37 | * enable to reconnect manually
38 | * add `Socket#connected()`
39 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | Copyright (c) 2013 Naoyuki Kanezawa
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 |
6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 |
8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 |
10 |
11 | (The MIT License)
12 |
13 | Copyright (c) 2011 Guillermo Rauch
14 |
15 | Permission is hereby granted, free of charge, to any person obtaining
16 | a copy of this software and associated documentation files (the
17 | 'Software'), to deal in the Software without restriction, including
18 | without limitation the rights to use, copy, modify, merge, publish,
19 | distribute, sublicense, and/or sell copies of the Software, and to
20 | permit persons to whom the Software is furnished to do so, subject to
21 | the following conditions:
22 |
23 | The above copyright notice and this permission notice shall be
24 | included in all copies or substantial portions of the Software.
25 |
26 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
27 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
30 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/README.md:
--------------------------------------------------------------------------------
1 | # Socket.IO-client.java
2 | [](https://travis-ci.org/nkzawa/socket.io-client.java)
3 |
4 | This is the Socket.IO v1.x Client Library for Java, which is simply ported from the [JavaScript client](https://github.com/Automattic/socket.io-client).
5 |
6 | See also:
7 |
8 | - [Android chat demo](https://github.com/nkzawa/socket.io-android-chat)
9 | - [engine.io-client.java](https://github.com/nkzawa/engine.io-client.java)
10 |
11 | ## Installation
12 | The latest artifact is available on Maven Central. To install manually, please refer dependencies to [pom.xml](https://github.com/nkzawa/socket.io-client.java/blob/master/pom.xml).
13 |
14 | ### Maven
15 | Add the following dependency to your `pom.xml`.
16 |
17 | ```xml
18 |
19 |
20 | com.github.nkzawa
21 | socket.io-client
22 | 0.5.0
23 |
24 |
25 | ```
26 |
27 | ### Gradle
28 | Add it as a gradle dependency for Android Studio, in `build.gradle`:
29 |
30 | ```groovy
31 | compile 'com.github.nkzawa:socket.io-client:0.5.0'
32 | ```
33 |
34 | ## Usage
35 | Socket.IO-client.java has almost the same api and features with the original JS client. You use `IO#socket` to initialize `Socket`:
36 |
37 | ```java
38 | socket = IO.socket("http://localhost");
39 | socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
40 |
41 | @Override
42 | public void call(Object... args) {
43 | socket.emit("foo", "hi");
44 | socket.disconnect();
45 | }
46 |
47 | }).on("event", new Emitter.Listener() {
48 |
49 | @Override
50 | public void call(Object... args) {}
51 |
52 | }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
53 |
54 | @Override
55 | public void call(Object... args) {}
56 |
57 | });
58 | socket.connect();
59 | ```
60 |
61 | This Library uses [org.json](http://www.json.org/java/) to parse and compose JSON strings:
62 |
63 | ```java
64 | // Sending an object
65 | JSONObject obj = new JSONObject();
66 | obj.put("hello", "server");
67 | obj.put("binary", new byte[42]);
68 | socket.emit("foo", obj);
69 |
70 | // Receiving an object
71 | socket.on("foo", new Emitter.Listener() {
72 | @Override
73 | public void call(Object... args) {
74 | JSONObject obj = (JSONObject)args[0];
75 | }
76 | });
77 | ```
78 |
79 | Options are supplied as follows:
80 |
81 | ```java
82 | IO.Options opts = new IO.Options();
83 | opts.forceNew = true;
84 | opts.reconnection = false;
85 |
86 | socket = IO.socket("http://localhost", opts);
87 | ```
88 |
89 | You can supply query parameters with the `query` option. NB: if you don't want to reuse a cached socket instance when the query parameter changes, you should use the `forceNew` option, the use case might be if your app allows for a user to logout, and a new user to login again:
90 |
91 | ```java
92 | IO.Options opts = new IO.Options();
93 | opts.forceNew = true;
94 | opts.query = "auth_token=" + authToken;
95 | Socket socket = IO.socket("http://localhost", opts);
96 | ```
97 |
98 | You can get a callback with `Ack` when the server received a message:
99 |
100 | ```java
101 | socket.emit("foo", "woot", new Ack() {
102 | @Override
103 | public void call(Object... args) {}
104 | });
105 | ```
106 |
107 | And vice versa:
108 |
109 | ```java
110 | // ack from client to server
111 | socket.on("foo", new Emitter.Listener() {
112 | @Override
113 | public void call(Object... args) {
114 | Ack ack = (Ack) args[args.length - 1];
115 | ack.call();
116 | }
117 | });
118 | ```
119 |
120 | Use custom SSL settings:
121 |
122 | ```java
123 | // default SSLContext for all sockets
124 | IO.setDefaultSSLContext(mySSLContext);
125 |
126 | // set as an option
127 | opts = new IO.Options();
128 | opts.sslContext = mySSLContext;
129 | socket = IO.socket("https://localhost", opts);
130 | ```
131 |
132 | See the Javadoc for more details.
133 |
134 | http://nkzawa.github.io/socket.io-client.java/apidocs/
135 |
136 | ## Features
137 | This library supports all of the features the JS client does, including events, options and upgrading transport. Android is fully supported.
138 |
139 | ## License
140 |
141 | MIT
142 |
143 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | com.github.nkzawa
4 | socket.io-client
5 | 0.5.1-SNAPSHOT
6 | jar
7 | socket.io-client
8 | Socket.IO Client Library for Java
9 | https://github.com/nkzawa/socket.io-client.java
10 |
11 |
12 | org.sonatype.oss
13 | oss-parent
14 | 7
15 |
16 |
17 |
18 | github
19 |
20 |
21 |
22 |
23 | The MIT License (MIT)
24 | http://opensource.org/licenses/mit-license
25 | repo
26 |
27 |
28 |
29 |
30 | https://github.com/nkzawa/socket.io-client.java
31 | scm:git:git://github.com/nkzawa/socket.io-client.java.git
32 | scm:git:git@github.com:nkzawa/socket.io-client.java.git
33 | HEAD
34 |
35 |
36 |
37 |
38 | nkzawa
39 | Naoyuki Kanezawa
40 | naoyuki.kanezawa@gmail.com
41 |
42 |
43 |
44 |
45 |
46 | sonatype-oss-public
47 | https://oss.sonatype.org/content/groups/public/
48 |
49 | true
50 |
51 |
52 | true
53 |
54 |
55 |
56 |
57 |
58 |
59 | com.github.nkzawa
60 | engine.io-client
61 | 0.5.0
62 |
63 |
64 | org.json
65 | json
66 | 20090211
67 |
68 |
69 | junit
70 | junit
71 | 4.11
72 | test
73 |
74 |
75 | org.hamcrest
76 | hamcrest-library
77 | 1.3
78 | test
79 |
80 |
81 | org.skyscreamer
82 | jsonassert
83 | 1.2.3
84 | test
85 |
86 |
87 | com.github.nkzawa
88 | socket.io-client
89 | 0.5.0
90 |
91 |
92 |
93 |
94 |
95 | ossrh
96 | https://oss.sonatype.org/content/repositories/snapshots
97 |
98 |
99 | ossrh
100 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
101 |
102 |
103 |
104 |
105 |
106 |
107 | org.apache.maven.plugins
108 | maven-compiler-plugin
109 | 3.0
110 |
111 | 1.6
112 | 1.6
113 |
114 |
115 |
116 | org.apache.maven.plugins
117 | maven-surefire-plugin
118 | 2.14.1
119 |
120 | -Dfile.encoding=UTF-8
121 |
122 |
123 | java.util.logging.config.file
124 | ./src/test/resources/logging.properties
125 |
126 |
127 |
128 |
129 |
130 | org.apache.maven.plugins
131 | maven-gpg-plugin
132 | 1.5
133 |
134 |
135 | sign-artifacts
136 | verify
137 |
138 | sign
139 |
140 |
141 |
142 |
143 |
144 | org.apache.maven.plugins
145 | maven-source-plugin
146 | 2.2.1
147 |
148 |
149 | attach-sources
150 |
151 | jar-no-fork
152 |
153 |
154 |
155 |
156 |
157 | org.apache.maven.plugins
158 | maven-javadoc-plugin
159 | 2.9.1
160 |
161 |
162 | attach-javadocs
163 |
164 | jar
165 |
166 |
167 |
168 |
169 |
170 | org.apache.maven.plugins
171 | maven-release-plugin
172 | 2.5
173 |
174 | true
175 | false
176 | release
177 | deploy
178 |
179 |
180 |
181 | org.sonatype.plugins
182 | nexus-staging-maven-plugin
183 | 1.6.2
184 | true
185 |
186 | ossrh
187 | https://oss.sonatype.org/
188 | true
189 |
190 |
191 |
192 | org.codehaus.mojo
193 | exec-maven-plugin
194 | 1.2.1
195 |
196 |
197 | npm-install
198 | process-test-resources
199 |
200 | exec
201 |
202 |
203 | ./src/test/resources
204 | npm
205 |
206 | install
207 |
208 |
209 |
210 |
211 |
212 |
213 | com.github.github
214 | site-maven-plugin
215 | 0.10
216 |
217 | Creating site for ${project.version}
218 |
219 |
220 |
221 |
222 | site
223 |
224 | site
225 |
226 |
227 |
228 |
229 |
230 |
231 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/backo/Backoff.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.backo;
2 |
3 | public class Backoff {
4 |
5 | private long ms = 100;
6 | private long max = 10000;
7 | private int factor = 2;
8 | private double jitter = 0.0;
9 | private int attempts = 0;
10 |
11 | public Backoff() {}
12 |
13 | public long duration() {
14 | long ms = this.ms * (long) Math.pow(this.factor, this.attempts++);
15 | if (jitter != 0.0) {
16 | double rand = Math.random();
17 | int deviation = (int) Math.floor(rand * this.jitter * ms);
18 | ms = (((int) Math.floor(rand * 10)) & 1) == 0 ? ms - deviation : ms + deviation;
19 | }
20 | if (ms < this.ms) {
21 | // overflow happened
22 | ms = Long.MAX_VALUE;
23 | }
24 | return Math.min(ms, this.max);
25 | }
26 |
27 | public void reset() {
28 | this.attempts = 0;
29 | }
30 |
31 | public Backoff setMin(long min) {
32 | this.ms = min;
33 | return this;
34 | }
35 |
36 | public Backoff setMax(long max) {
37 | this.max = max;
38 | return this;
39 | }
40 |
41 | public Backoff setFactor(int factor) {
42 | this.factor = factor;
43 | return this;
44 | }
45 |
46 | public Backoff setJitter(double jitter) {
47 | this.jitter = jitter;
48 | return this;
49 | }
50 |
51 | public int getAttempts() {
52 | return this.attempts;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/hasbinary/HasBinary.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.hasbinary;
2 |
3 | import org.json.JSONArray;
4 | import org.json.JSONException;
5 | import org.json.JSONObject;
6 |
7 | import java.util.Iterator;
8 |
9 | public class HasBinary {
10 |
11 | private HasBinary() {}
12 |
13 | public static boolean hasBinary(Object data) {
14 | return _hasBinary(data);
15 | }
16 |
17 | private static boolean _hasBinary(Object obj) {
18 | if (obj == null) return false;
19 |
20 | if (obj instanceof byte[]) {
21 | return true;
22 | }
23 |
24 | if (obj instanceof JSONArray) {
25 | JSONArray _obj = (JSONArray)obj;
26 | int length = _obj.length();
27 | for (int i = 0; i < length; i++) {
28 | Object v;
29 | try {
30 | v = _obj.isNull(i) ? null : _obj.get(i);
31 | } catch (JSONException e) {
32 | return false;
33 | }
34 | if (_hasBinary(v)) {
35 | return true;
36 | }
37 | }
38 | } else if (obj instanceof JSONObject) {
39 | JSONObject _obj = (JSONObject)obj;
40 | Iterator keys = _obj.keys();
41 | while (keys.hasNext()) {
42 | String key = (String)keys.next();
43 | Object v;
44 | try {
45 | v = _obj.get(key);
46 | } catch (JSONException e) {
47 | return false;
48 | }
49 | if (_hasBinary(v)) {
50 | return true;
51 | }
52 | }
53 | }
54 |
55 | return false;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/socketio/client/Ack.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.socketio.client;
2 |
3 | /**
4 | * Acknowledgement.
5 | */
6 | public interface Ack {
7 |
8 | public void call(Object... args);
9 |
10 | }
11 |
12 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/socketio/client/IO.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.socketio.client;
2 |
3 |
4 | import com.github.nkzawa.socketio.parser.Parser;
5 |
6 | import javax.net.ssl.HostnameVerifier;
7 | import javax.net.ssl.SSLContext;
8 | import java.net.MalformedURLException;
9 | import java.net.URI;
10 | import java.net.URISyntaxException;
11 | import java.net.URL;
12 | import java.util.concurrent.ConcurrentHashMap;
13 | import java.util.logging.Logger;
14 |
15 |
16 | public class IO {
17 |
18 | private static final Logger logger = Logger.getLogger(IO.class.getName());
19 |
20 | private static final ConcurrentHashMap managers = new ConcurrentHashMap();
21 |
22 | /**
23 | * Protocol version.
24 | */
25 | public static int protocol = Parser.protocol;
26 |
27 | public static void setDefaultSSLContext(SSLContext sslContext) {
28 | Manager.defaultSSLContext = sslContext;
29 | }
30 |
31 | public static void setDefaultHostnameVerifier(HostnameVerifier hostnameVerifier) {
32 | Manager.defaultHostnameVerifier = hostnameVerifier;
33 | }
34 |
35 | private IO() {}
36 |
37 | public static Socket socket(String uri) throws URISyntaxException {
38 | return socket(uri, null);
39 | }
40 |
41 | public static Socket socket(String uri, Options opts) throws URISyntaxException {
42 | return socket(new URI(uri), opts);
43 | }
44 |
45 | public static Socket socket(URI uri) throws URISyntaxException {
46 | return socket(uri, null);
47 | }
48 |
49 | /**
50 | * Initializes a {@link Socket} from an existing {@link Manager} for multiplexing.
51 | *
52 | * @param uri uri to connect.
53 | * @param opts options for socket.
54 | * @return {@link Socket} instance.
55 | * @throws URISyntaxException
56 | */
57 | public static Socket socket(URI uri, Options opts) throws URISyntaxException {
58 | if (opts == null) {
59 | opts = new Options();
60 | }
61 |
62 | URL parsed;
63 | try {
64 | parsed = Url.parse(uri);
65 | } catch (MalformedURLException e) {
66 | throw new URISyntaxException(uri.toString(), e.getMessage());
67 | }
68 | URI source = parsed.toURI();
69 | Manager io;
70 |
71 | if (opts.forceNew || !opts.multiplex) {
72 | logger.fine(String.format("ignoring socket cache for %s", source));
73 | io = new Manager(source, opts);
74 | } else {
75 | String id = Url.extractId(parsed);
76 | if (!managers.containsKey(id)) {
77 | logger.fine(String.format("new io instance for %s", source));
78 | managers.putIfAbsent(id, new Manager(source, opts));
79 | }
80 | io = managers.get(id);
81 | }
82 |
83 | return io.socket(parsed.getPath());
84 | }
85 |
86 |
87 | public static class Options extends Manager.Options {
88 |
89 | public boolean forceNew;
90 |
91 | /**
92 | * Whether to enable multiplexing. Default is true.
93 | */
94 | public boolean multiplex = true;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/socketio/client/Manager.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.socketio.client;
2 |
3 | import com.github.nkzawa.backo.Backoff;
4 | import com.github.nkzawa.emitter.Emitter;
5 | import com.github.nkzawa.socketio.parser.Packet;
6 | import com.github.nkzawa.socketio.parser.Parser;
7 | import com.github.nkzawa.thread.EventThread;
8 |
9 | import javax.net.ssl.HostnameVerifier;
10 | import javax.net.ssl.SSLContext;
11 | import java.net.URI;
12 | import java.util.*;
13 | import java.util.concurrent.ConcurrentHashMap;
14 | import java.util.logging.Level;
15 | import java.util.logging.Logger;
16 |
17 |
18 | /**
19 | * Manager class represents a connection to a given Socket.IO server.
20 | */
21 | public class Manager extends Emitter {
22 |
23 | private static final Logger logger = Logger.getLogger(Manager.class.getName());
24 |
25 | /*package*/ enum ReadyState {
26 | CLOSED, OPENING, OPEN
27 | }
28 |
29 | /**
30 | * Called on a successful connection.
31 | */
32 | public static final String EVENT_OPEN = "open";
33 |
34 | /**
35 | * Called on a disconnection.
36 | */
37 | public static final String EVENT_CLOSE = "close";
38 |
39 | public static final String EVENT_PACKET = "packet";
40 | public static final String EVENT_ERROR = "error";
41 |
42 | /**
43 | * Called on a connection error.
44 | */
45 | public static final String EVENT_CONNECT_ERROR = "connect_error";
46 |
47 | /**
48 | * Called on a connection timeout.
49 | */
50 | public static final String EVENT_CONNECT_TIMEOUT = "connect_timeout";
51 |
52 | /**
53 | * Called on a successful reconnection.
54 | */
55 | public static final String EVENT_RECONNECT = "reconnect";
56 |
57 | /**
58 | * Called on a reconnection attempt error.
59 | */
60 | public static final String EVENT_RECONNECT_ERROR = "reconnect_error";
61 |
62 | public static final String EVENT_RECONNECT_FAILED = "reconnect_failed";
63 |
64 | public static final String EVENT_RECONNECT_ATTEMPT = "reconnect_attempt";
65 |
66 | public static final String EVENT_RECONNECTING = "reconnecting";
67 |
68 | /**
69 | * Called when a new transport is created. (experimental)
70 | */
71 | public static final String EVENT_TRANSPORT = Engine.EVENT_TRANSPORT;
72 |
73 | /*package*/ static SSLContext defaultSSLContext;
74 | /*package*/ static HostnameVerifier defaultHostnameVerifier;
75 |
76 | /*package*/ ReadyState readyState = null;
77 |
78 | private boolean _reconnection;
79 | private boolean skipReconnect;
80 | private boolean reconnecting;
81 | private boolean encoding;
82 | private int _reconnectionAttempts;
83 | private long _reconnectionDelay;
84 | private long _reconnectionDelayMax;
85 | private double _randomizationFactor;
86 | private Backoff backoff;
87 | private long _timeout;
88 | private Set connected;
89 | private URI uri;
90 | private List packetBuffer;
91 | private Queue subs;
92 | private Options opts;
93 | /*package*/ com.github.nkzawa.engineio.client.Socket engine;
94 | private Parser.Encoder encoder;
95 | private Parser.Decoder decoder;
96 |
97 | /**
98 | * This HashMap can be accessed from outside of EventThread.
99 | */
100 | private ConcurrentHashMap nsps;
101 |
102 |
103 | public Manager() {
104 | this(null, null);
105 | }
106 |
107 | public Manager(URI uri) {
108 | this(uri, null);
109 | }
110 |
111 | public Manager(Options opts) {
112 | this(null, opts);
113 | }
114 |
115 | public Manager(URI uri, Options opts) {
116 | if (opts == null) {
117 | opts = new Options();
118 | }
119 | if (opts.path == null) {
120 | opts.path = "/socket.io";
121 | }
122 | if (opts.sslContext == null) {
123 | opts.sslContext = defaultSSLContext;
124 | }
125 | if (opts.hostnameVerifier == null) {
126 | opts.hostnameVerifier = defaultHostnameVerifier;
127 | }
128 | this.opts = opts;
129 | this.nsps = new ConcurrentHashMap();
130 | this.subs = new LinkedList();
131 | this.reconnection(opts.reconnection);
132 | this.reconnectionAttempts(opts.reconnectionAttempts != 0 ? opts.reconnectionAttempts : Integer.MAX_VALUE);
133 | this.reconnectionDelay(opts.reconnectionDelay != 0 ? opts.reconnectionDelay : 1000);
134 | this.reconnectionDelayMax(opts.reconnectionDelayMax != 0 ? opts.reconnectionDelayMax : 5000);
135 | this.randomizationFactor(opts.randomizationFactor != 0.0 ? opts.randomizationFactor : 0.5);
136 | this.backoff = new Backoff()
137 | .setMin(this.reconnectionDelay())
138 | .setMax(this.reconnectionDelayMax())
139 | .setJitter(this.randomizationFactor());
140 | this.timeout(opts.timeout);
141 | this.readyState = ReadyState.CLOSED;
142 | this.uri = uri;
143 | this.connected = new HashSet();
144 | this.encoding = false;
145 | this.packetBuffer = new ArrayList();
146 | this.encoder = new Parser.Encoder();
147 | this.decoder = new Parser.Decoder();
148 | }
149 |
150 | private void emitAll(String event, Object... args) {
151 | this.emit(event, args);
152 | for (Socket socket : this.nsps.values()) {
153 | socket.emit(event, args);
154 | }
155 | }
156 |
157 | /**
158 | * Update `socket.id` of all sockets
159 | */
160 | private void updateSocketIds() {
161 | for (Socket socket : this.nsps.values()) {
162 | socket.id = this.engine.id();
163 | }
164 | }
165 |
166 | public boolean reconnection() {
167 | return this._reconnection;
168 | }
169 |
170 | public Manager reconnection(boolean v) {
171 | this._reconnection = v;
172 | return this;
173 | }
174 |
175 | public int reconnectionAttempts() {
176 | return this._reconnectionAttempts;
177 | }
178 |
179 | public Manager reconnectionAttempts(int v) {
180 | this._reconnectionAttempts = v;
181 | return this;
182 | }
183 |
184 | public long reconnectionDelay() {
185 | return this._reconnectionDelay;
186 | }
187 |
188 | public Manager reconnectionDelay(long v) {
189 | this._reconnectionDelay = v;
190 | if (this.backoff != null) {
191 | this.backoff.setMin(v);
192 | }
193 | return this;
194 | }
195 |
196 | public double randomizationFactor() {
197 | return this._randomizationFactor;
198 | }
199 |
200 | public Manager randomizationFactor(double v) {
201 | this._randomizationFactor = v;
202 | if (this.backoff != null) {
203 | this.backoff.setJitter(v);
204 | }
205 | return this;
206 | }
207 |
208 | public long reconnectionDelayMax() {
209 | return this._reconnectionDelayMax;
210 | }
211 |
212 | public Manager reconnectionDelayMax(long v) {
213 | this._reconnectionDelayMax = v;
214 | if (this.backoff != null) {
215 | this.backoff.setMax(v);
216 | }
217 | return this;
218 | }
219 |
220 | public long timeout() {
221 | return this._timeout;
222 | }
223 |
224 | public Manager timeout(long v) {
225 | this._timeout = v;
226 | return this;
227 | }
228 |
229 | private void maybeReconnectOnOpen() {
230 | // Only try to reconnect if it's the first time we're connecting
231 | if (!this.reconnecting && this._reconnection && this.backoff.getAttempts() == 0) {
232 | this.reconnect();
233 | }
234 | }
235 |
236 | public Manager open(){
237 | return open(null);
238 | }
239 |
240 | /**
241 | * Connects the client.
242 | *
243 | * @param fn callback.
244 | * @return a reference to this object.
245 | */
246 | public Manager open(final OpenCallback fn) {
247 | EventThread.exec(new Runnable() {
248 | @Override
249 | public void run() {
250 | logger.fine(String.format("readyState %s", Manager.this.readyState));
251 | if (Manager.this.readyState == ReadyState.OPEN) return;
252 |
253 | logger.fine(String.format("opening %s", Manager.this.uri));
254 | Manager.this.engine = new Engine(Manager.this.uri, Manager.this.opts);
255 | final com.github.nkzawa.engineio.client.Socket socket = Manager.this.engine;
256 | final Manager self = Manager.this;
257 | Manager.this.readyState = ReadyState.OPENING;
258 | Manager.this.skipReconnect = false;
259 |
260 | // propagate transport event.
261 | socket.on(Engine.EVENT_TRANSPORT, new Listener() {
262 | @Override
263 | public void call(Object... args) {
264 | self.emit(Manager.EVENT_TRANSPORT, args);
265 | }
266 | });
267 |
268 | final On.Handle openSub = On.on(socket, Engine.EVENT_OPEN, new Listener() {
269 | @Override
270 | public void call(Object... objects) {
271 | self.onopen();
272 | if (fn != null) fn.call(null);
273 | }
274 | });
275 |
276 | On.Handle errorSub = On.on(socket, Engine.EVENT_ERROR, new Listener() {
277 | @Override
278 | public void call(Object... objects) {
279 | Object data = objects.length > 0 ? objects[0] : null;
280 | logger.fine("connect_error");
281 | self.cleanup();
282 | self.readyState = ReadyState.CLOSED;
283 | self.emitAll(EVENT_CONNECT_ERROR, data);
284 | if (fn != null) {
285 | Exception err = new SocketIOException("Connection error",
286 | data instanceof Exception ? (Exception) data : null);
287 | fn.call(err);
288 | } else {
289 | // Only do this if there is no fn to handle the error
290 | self.maybeReconnectOnOpen();
291 | }
292 | }
293 | });
294 |
295 | if (Manager.this._timeout >= 0) {
296 | final long timeout = Manager.this._timeout;
297 | logger.fine(String.format("connection attempt will timeout after %d", timeout));
298 |
299 | final Timer timer = new Timer();
300 | timer.schedule(new TimerTask() {
301 | @Override
302 | public void run() {
303 | EventThread.exec(new Runnable() {
304 | @Override
305 | public void run() {
306 | logger.fine(String.format("connect attempt timed out after %d", timeout));
307 | openSub.destroy();
308 | socket.close();
309 | socket.emit(Engine.EVENT_ERROR, new SocketIOException("timeout"));
310 | self.emitAll(EVENT_CONNECT_TIMEOUT, timeout);
311 | }
312 | });
313 | }
314 | }, timeout);
315 |
316 | Manager.this.subs.add(new On.Handle() {
317 | @Override
318 | public void destroy() {
319 | timer.cancel();
320 | }
321 | });
322 | }
323 |
324 | Manager.this.subs.add(openSub);
325 | Manager.this.subs.add(errorSub);
326 |
327 | Manager.this.engine.open();
328 | }
329 | });
330 | return this;
331 | }
332 |
333 | private void onopen() {
334 | logger.fine("open");
335 |
336 | this.cleanup();
337 |
338 | this.readyState = ReadyState.OPEN;
339 | this.emit(EVENT_OPEN);
340 |
341 | final com.github.nkzawa.engineio.client.Socket socket = this.engine;
342 | this.subs.add(On.on(socket, Engine.EVENT_DATA, new Listener() {
343 | @Override
344 | public void call(Object... objects) {
345 | Object data = objects[0];
346 | if (data instanceof String) {
347 | Manager.this.ondata((String)data);
348 | } else if (data instanceof byte[]) {
349 | Manager.this.ondata((byte[])data);
350 | }
351 | }
352 | }));
353 | this.subs.add(On.on(this.decoder, Parser.Decoder.EVENT_DECODED, new Listener() {
354 | @Override
355 | public void call(Object... objects) {
356 | Manager.this.ondecoded((Packet) objects[0]);
357 | }
358 | }));
359 | this.subs.add(On.on(socket, Engine.EVENT_ERROR, new Listener() {
360 | @Override
361 | public void call(Object... objects) {
362 | Manager.this.onerror((Exception)objects[0]);
363 | }
364 | }));
365 | this.subs.add(On.on(socket, Engine.EVENT_CLOSE, new Listener() {
366 | @Override
367 | public void call(Object... objects) {
368 | Manager.this.onclose((String)objects[0]);
369 | }
370 | }));
371 | }
372 |
373 | private void ondata(String data) {
374 | this.decoder.add(data);
375 | }
376 |
377 | private void ondata(byte[] data) {
378 | this.decoder.add(data);
379 | }
380 |
381 | private void ondecoded(Packet packet) {
382 | this.emit(EVENT_PACKET, packet);
383 | }
384 |
385 | private void onerror(Exception err) {
386 | logger.log(Level.FINE, "error", err);
387 | this.emitAll(EVENT_ERROR, err);
388 | }
389 |
390 | /**
391 | * Initializes {@link Socket} instances for each namespaces.
392 | *
393 | * @param nsp namespace.
394 | * @return a socket instance for the namespace.
395 | */
396 | public Socket socket(String nsp) {
397 | Socket socket = this.nsps.get(nsp);
398 | if (socket == null) {
399 | socket = new Socket(this, nsp);
400 | Socket _socket = this.nsps.putIfAbsent(nsp, socket);
401 | if (_socket != null) {
402 | socket = _socket;
403 | } else {
404 | final Manager self = this;
405 | final Socket s = socket;
406 | socket.on(Socket.EVENT_CONNECT, new Listener() {
407 | @Override
408 | public void call(Object... objects) {
409 | s.id = self.engine.id();
410 | self.connected.add(s);
411 | }
412 | });
413 | }
414 | }
415 | return socket;
416 | }
417 |
418 | /*package*/ void destroy(Socket socket) {
419 | this.connected.remove(socket);
420 | if (this.connected.size() > 0) return;
421 |
422 | this.close();
423 | }
424 |
425 | /*package*/ void packet(Packet packet) {
426 | logger.fine(String.format("writing packet %s", packet));
427 | final Manager self = this;
428 |
429 | if (!self.encoding) {
430 | self.encoding = true;
431 | this.encoder.encode(packet, new Parser.Encoder.Callback() {
432 | @Override
433 | public void call(Object[] encodedPackets) {
434 | for (Object packet : encodedPackets) {
435 | if (packet instanceof String) {
436 | self.engine.write((String)packet);
437 | } else if (packet instanceof byte[]) {
438 | self.engine.write((byte[])packet);
439 | }
440 | }
441 | self.encoding = false;
442 | self.processPacketQueue();
443 | }
444 | });
445 | } else {
446 | self.packetBuffer.add(packet);
447 | }
448 | }
449 |
450 | private void processPacketQueue() {
451 | if (this.packetBuffer.size() > 0 && !this.encoding) {
452 | Packet pack = this.packetBuffer.remove(0);
453 | this.packet(pack);
454 | }
455 | }
456 |
457 | private void cleanup() {
458 | On.Handle sub;
459 | while ((sub = this.subs.poll()) != null) sub.destroy();
460 | }
461 |
462 | /*package*/ void close() {
463 | if (this.readyState != ReadyState.OPEN) {
464 | this.cleanup();
465 | }
466 | this.skipReconnect = true;
467 | this.backoff.reset();
468 | this.readyState = ReadyState.CLOSED;
469 | if (this.engine != null) {
470 | this.engine.close();
471 | }
472 | }
473 |
474 | private void onclose(String reason) {
475 | logger.fine("close");
476 | this.cleanup();
477 | this.backoff.reset();
478 | this.readyState = ReadyState.CLOSED;
479 | this.emit(EVENT_CLOSE, reason);
480 |
481 | if (this._reconnection && !this.skipReconnect) {
482 | this.reconnect();
483 | }
484 | }
485 |
486 | private void reconnect() {
487 | if (this.reconnecting || this.skipReconnect) return;
488 |
489 | final Manager self = this;
490 |
491 | if (this.backoff.getAttempts() >= this._reconnectionAttempts) {
492 | logger.fine("reconnect failed");
493 | this.backoff.reset();
494 | this.emitAll(EVENT_RECONNECT_FAILED);
495 | this.reconnecting = false;
496 | } else {
497 | long delay = this.backoff.duration();
498 | logger.fine(String.format("will wait %dms before reconnect attempt", delay));
499 |
500 | this.reconnecting = true;
501 | final Timer timer = new Timer();
502 | timer.schedule(new TimerTask() {
503 | @Override
504 | public void run() {
505 | EventThread.exec(new Runnable() {
506 | @Override
507 | public void run() {
508 | if (self.skipReconnect) return;
509 |
510 | logger.fine("attempting reconnect");
511 | int attempts = self.backoff.getAttempts();
512 | self.emitAll(EVENT_RECONNECT_ATTEMPT, attempts);
513 | self.emitAll(EVENT_RECONNECTING, attempts);
514 |
515 | // check again for the case socket closed in above events
516 | if (self.skipReconnect) return;
517 |
518 | self.open(new OpenCallback() {
519 | @Override
520 | public void call(Exception err) {
521 | if (err != null) {
522 | logger.fine("reconnect attempt error");
523 | self.reconnecting = false;
524 | self.reconnect();
525 | self.emitAll(EVENT_RECONNECT_ERROR, err);
526 | } else {
527 | logger.fine("reconnect success");
528 | self.onreconnect();
529 | }
530 | }
531 | });
532 | }
533 | });
534 | }
535 | }, delay);
536 |
537 | this.subs.add(new On.Handle() {
538 | @Override
539 | public void destroy() {
540 | timer.cancel();
541 | }
542 | });
543 | }
544 | }
545 |
546 | private void onreconnect() {
547 | int attempts = this.backoff.getAttempts();
548 | this.reconnecting = false;
549 | this.backoff.reset();
550 | this.updateSocketIds();
551 | this.emitAll(EVENT_RECONNECT, attempts);
552 | }
553 |
554 |
555 | public static interface OpenCallback {
556 |
557 | public void call(Exception err);
558 | }
559 |
560 |
561 | private static class Engine extends com.github.nkzawa.engineio.client.Socket {
562 |
563 | Engine(URI uri, Options opts) {
564 | super(uri, opts);
565 | }
566 | }
567 |
568 | public static class Options extends com.github.nkzawa.engineio.client.Socket.Options {
569 |
570 | public boolean reconnection = true;
571 | public int reconnectionAttempts;
572 | public long reconnectionDelay;
573 | public long reconnectionDelayMax;
574 | public double randomizationFactor;
575 |
576 | /**
577 | * Connection timeout (ms). Set -1 to disable.
578 | */
579 | public long timeout = 20000;
580 | }
581 | }
582 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/socketio/client/On.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.socketio.client;
2 |
3 | import com.github.nkzawa.emitter.Emitter;
4 |
5 | public class On {
6 |
7 | private On() {}
8 |
9 | public static Handle on(final Emitter obj, final String ev, final Emitter.Listener fn) {
10 | obj.on(ev, fn);
11 | return new Handle() {
12 | @Override
13 | public void destroy() {
14 | obj.off(ev, fn);
15 | }
16 | };
17 | }
18 |
19 | public static interface Handle {
20 |
21 | public void destroy();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/app/libs/socket.io-client.java-master/src/main/java/com/github/nkzawa/socketio/client/Socket.java:
--------------------------------------------------------------------------------
1 | package com.github.nkzawa.socketio.client;
2 |
3 | import com.github.nkzawa.emitter.Emitter;
4 | import com.github.nkzawa.hasbinary.HasBinary;
5 | import com.github.nkzawa.socketio.parser.Packet;
6 | import com.github.nkzawa.socketio.parser.Parser;
7 | import com.github.nkzawa.thread.EventThread;
8 | import org.json.JSONArray;
9 | import org.json.JSONException;
10 | import org.json.JSONObject;
11 |
12 | import java.util.*;
13 | import java.util.logging.Logger;
14 |
15 | /**
16 | * The socket class for Socket.IO Client.
17 | */
18 | public class Socket extends Emitter {
19 |
20 | private static final Logger logger = Logger.getLogger(Socket.class.getName());
21 |
22 | /**
23 | * Called on a connection.
24 | */
25 | public static final String EVENT_CONNECT = "connect";
26 |
27 | /**
28 | * Called on a disconnection.
29 | */
30 | public static final String EVENT_DISCONNECT = "disconnect";
31 |
32 | /**
33 | * Called on a connection error.
34 | *
35 | * Parameters:
36 | *
37 | * - (Exception) error data.
38 | *
39 | */
40 | public static final String EVENT_ERROR = "error";
41 |
42 | public static final String EVENT_MESSAGE = "message";
43 |
44 | public static final String EVENT_CONNECT_ERROR = Manager.EVENT_CONNECT_ERROR;
45 |
46 | public static final String EVENT_CONNECT_TIMEOUT = Manager.EVENT_CONNECT_TIMEOUT;
47 |
48 | public static final String EVENT_RECONNECT = Manager.EVENT_RECONNECT;
49 |
50 | public static final String EVENT_RECONNECT_ERROR = Manager.EVENT_RECONNECT_ERROR;
51 |
52 | public static final String EVENT_RECONNECT_FAILED = Manager.EVENT_RECONNECT_FAILED;
53 |
54 | public static final String EVENT_RECONNECT_ATTEMPT = Manager.EVENT_RECONNECT_ATTEMPT;
55 |
56 | public static final String EVENT_RECONNECTING = Manager.EVENT_RECONNECTING;
57 |
58 | private static Map events = new HashMap() {{
59 | put(EVENT_CONNECT, 1);
60 | put(EVENT_CONNECT_ERROR, 1);
61 | put(EVENT_CONNECT_TIMEOUT, 1);
62 | put(EVENT_DISCONNECT, 1);
63 | put(EVENT_ERROR, 1);
64 | put(EVENT_RECONNECT, 1);
65 | put(EVENT_RECONNECT_ATTEMPT, 1);
66 | put(EVENT_RECONNECT_FAILED, 1);
67 | put(EVENT_RECONNECT_ERROR, 1);
68 | put(EVENT_RECONNECTING, 1);
69 | }};
70 |
71 | /*package*/ String id;
72 |
73 | private volatile boolean connected;
74 | private int ids;
75 | private String nsp;
76 | private Manager io;
77 | private Map acks = new HashMap();
78 | private Queue subs;
79 | private final Queue> receiveBuffer = new LinkedList>();
80 | private final Queue> sendBuffer = new LinkedList>();
81 |
82 | public Socket(Manager io, String nsp) {
83 | this.io = io;
84 | this.nsp = nsp;
85 | }
86 |
87 | private void subEvents() {
88 | if (this.subs != null) return;
89 |
90 | final Manager io = Socket.this.io;
91 | Socket.this.subs = new LinkedList() {{
92 | add(On.on(io, Manager.EVENT_OPEN, new Listener() {
93 | @Override
94 | public void call(Object... args) {
95 | Socket.this.onopen();
96 | }
97 | }));
98 | add(On.on(io, Manager.EVENT_PACKET, new Listener() {
99 | @Override
100 | public void call(Object... args) {
101 | Socket.this.onpacket((Packet) args[0]);
102 | }
103 | }));
104 | add(On.on(io, Manager.EVENT_CLOSE, new Listener() {
105 | @Override
106 | public void call(Object... args) {
107 | Socket.this.onclose(args.length > 0 ? (String) args[0] : null);
108 | }
109 | }));
110 | }};
111 | }
112 |
113 | /**
114 | * Connects the socket.
115 | */
116 | public Socket open() {
117 | EventThread.exec(new Runnable() {
118 | @Override
119 | public void run() {
120 | if (Socket.this.connected) return;
121 |
122 | Socket.this.subEvents();
123 | Socket.this.io.open(); // ensure open
124 | if (Manager.ReadyState.OPEN == Socket.this.io.readyState) Socket.this.onopen();
125 | }
126 | });
127 | return this;
128 | }
129 |
130 | /**
131 | * Connects the socket.
132 | */
133 | public Socket connect() {
134 | return this.open();
135 | }
136 |
137 | /**
138 | * Send messages.
139 | *
140 | * @param args data to send.
141 | * @return a reference to this object.
142 | */
143 | public Socket send(final Object... args) {
144 | EventThread.exec(new Runnable() {
145 | @Override
146 | public void run() {
147 | Socket.this.emit(EVENT_MESSAGE, args);
148 | }
149 | });
150 | return this;
151 | }
152 |
153 | /**
154 | * Emits an event. When you pass {@link Ack} at the last argument, then the acknowledge is done.
155 | *
156 | * @param event an event name.
157 | * @param args data to send.
158 | * @return a reference to this object.
159 | */
160 | @Override
161 | public Emitter emit(final String event, final Object... args) {
162 | EventThread.exec(new Runnable() {
163 | @Override
164 | public void run() {
165 | if (events.containsKey(event)) {
166 | Socket.super.emit(event, args);
167 | return;
168 | }
169 |
170 | List