Skip to content

Commit e36f037

Browse files
RxJava implemenation with example
Change-Id: I078c5ba41e4cca71ce90044f1d129a46ad5bc6ae
1 parent 857a5c5 commit e36f037

86 files changed

Lines changed: 5016 additions & 84 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#Python
2+
*.pyc
3+
websockets-server/server/build/**
4+
websockets-server/venv/**
5+
websockets-server/server/websockets_server.egg-info/**
6+
websockets-server/server/dist/**
7+
18
#Android generated
29
bin
310
gen

README.md

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,119 @@
1-
# AndroidAUSocketIO
2-
AndroidAUSocketIO is library providing websocket connection form Android.
1+
# JavaWebsocketClient also for Android
2+
JavaWebsocketClient is library is simple library for Websocket connection for java and Android.
33
It is designed to be fast and fault tolerant.
44

5-
# Run example from gradle
5+
[![Build Status](https://travis-ci.org/jacek-marchwicki/JavaWebsocketClient.svg?branch=master)](https://travis-ci.org/jacek-marchwicki/JavaWebsocketClient)
66

7-
./gradlew installDebug
7+
## Content of the package
8+
9+
* Example websockets server [python twisted server](websockets-server/README.md)
10+
* Imperative websocket client library `websockets/`
11+
* Imperative websocket android example `websockets-example/`
12+
* Rx-java websocket client library `websockets-rxjava/`
13+
* Rx-java websocket android example `websockets-rxjava-example/`
14+
15+
## Imperative example
16+
17+
Connect to server and send message on connected:
18+
19+
```java
20+
final NewWebSocket newWebSocket = new NewWebSocket(new SecureRandomProviderImpl(), new SocketProviderImpl());
21+
final WebSocketConnection connection = newWebSocket.create(SERVER_URI, new WebSocketListener() {
22+
@Override
23+
public void onConnected() throws IOException, InterruptedException, NotConnectedException {
24+
connection.sendStringMessage("register");
25+
}
26+
});
27+
connection.connect();
28+
```
29+
30+
For more examples look:
31+
* [Android example](websockets-example/src/main/java/com/appunite/socket/MainActivity.java)
32+
* [Sample test](websockets/src/test/java/com/appunite/websocket/WebsocketTest.java)
33+
34+
35+
## Reactive example
36+
37+
How to connect to server:
38+
39+
```java
40+
final Subscription subscribe = new RxWebSockets(new NewWebSocket(), SERVER_URI)
41+
.webSocketObservable()
42+
.subscribe(new Action1<RxEvent>() {
43+
@Override
44+
public void call(RxEvent rxEvent) {
45+
System.out.println("Event: " + rxEvent.toString());
46+
}
47+
});
48+
Thread.sleep(10000);
49+
subscribe.unsubscribe();
50+
```
51+
52+
Send message on connected:
53+
54+
```java
55+
final Subscription subscribe = new RxWebSockets(newWebSocket, SERVER_URI)
56+
.webSocketObservable()
57+
.subscribe(new Action1<RxEvent>() {
58+
@Override
59+
public void call(RxEvent rxEvent) {
60+
if (rxEvent instanceof RxEventConnected) {
61+
Observable.just("response")
62+
.compose(RxMoreObservables.sendMessage((RxEventConnected) rxEvent))
63+
.subscribe();
64+
}
65+
}
66+
});
67+
Thread.sleep(1000);
68+
subscribe.unsubscribe();
69+
```
70+
71+
For examples look:
72+
* Android example: [Activity](websockets-rxjava-example/src/main/java/com/appunite/socket/MainActivity.java) [Presenter](websockets-rxjava-example/src/main/java/com/appunite/socket/MainPresenter.java)
73+
* Example Real tests: [RxJsonWebSocketsRealTest](websockets-rxjava-example/src/test/java/com/example/RxJsonWebSocketsRealTest.java), [RxWebSocketsRealTest](websockets-rxjava-example/src/test/java/com/example/RxWebSocketsRealTest.java), [SocketRealTest](websockets-rxjava-example/src/test/java/com/example/SocketRealTest.java)
74+
* [Unit test](websockets-rxjava-example/src/test/java/com/example/SocketTest.java)
75+
76+
## Rx-java with json parser
77+
78+
```java
79+
class YourMessage {
80+
public String response;
81+
public String error;
82+
}
83+
84+
final RxJsonWebSockets rxJsonWebSockets = new RxJsonWebSockets(new RxWebSockets(new NewWebSocket(), SERVER_URI), new GsonBuilder().create(), Message.class);
85+
rxJsonWebSockets.webSocketObservable()
86+
.compose(MoreObservables.filterAndMap(RxJsonEventMessage.class))
87+
.compose(RxJsonEventMessage.filterAndMap(YourMessage.class))
88+
.subscribe(new Action1<YourMessage>() {
89+
@Override
90+
public void call(YourMessage yourMessage) {
91+
System.out.println("your message: " + yourMessage.response);
92+
}
93+
});
94+
```
95+
96+
## Run examples from gradle
97+
98+
To run example first run [websocket server](websockets-server/README.md), than update url to your host in:
99+
* [Rx-java Activity](websockets-rxjava-example/src/main/java/com/appunite/socket/MainActivity.java)
100+
* [Imperative Activity](websockets-example/src/main/java/com/appunite/socket/MainActivity.java)
101+
102+
Reactive (rx-java) example:
103+
104+
```bash
105+
./gradlew :websockets-rxjava-example:installDebug
106+
```
107+
108+
Imperative example:
109+
110+
```bash
111+
./gradlew :websockets-example:installDebug
112+
```
8113
9-
# License
114+
## License
10115

11-
Copyright [2012] [Jacek Marchwicki <[email protected]>]
116+
Copyright [2015] [Jacek Marchwicki <[email protected]>]
12117

13118
Licensed under the Apache License, Version 2.0 (the "License");
14119
you may not use this file except in compliance with the License.

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
include ":websockets-example"
22
include ":websockets"
3+
include ":websockets-rxjava"
4+
include ":websockets-rxjava-example"

websockets-example/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
android:icon="@drawable/ic_launcher"
1616
android:label="@string/app_name" >
1717
<activity
18-
android:name=".Main"
18+
android:name=".MainActivity"
1919
android:label="@string/app_name" >
2020
<intent-filter>
2121
<action android:name="android.intent.action.MAIN" />

websockets-example/src/main/java/com/appunite/socket/Main.java renamed to websockets-example/src/main/java/com/appunite/socket/MainActivity.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2012 Jacek Marchwicki <[email protected]>
2+
* Copyright (C) 2015 Jacek Marchwicki <[email protected]>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
3636

3737
import javax.annotation.Nonnull;
3838

39-
public class Main extends Activity implements OnClickListener {
39+
public class MainActivity extends Activity implements OnClickListener {
4040

4141

4242
private static final URI ADDRESS;
@@ -141,12 +141,12 @@ public void run() {
141141
@Override
142142
public void onCreate(Bundle savedInstanceState) {
143143
super.onCreate(savedInstanceState);
144-
setContentView(R.layout.main);
144+
setContentView(R.layout.main_activity);
145145

146-
connectButton = findViewById(R.id.connect_button);
147-
disconnectButton = findViewById(R.id.disconnect_button);
146+
connectButton = findViewById(R.id.main_acitivity_connect_button);
147+
disconnectButton = findViewById(R.id.main_activity_disconnect_button);
148148
sendButton = findViewById(R.id.send_button);
149-
final ListView listView = (ListView) findViewById(android.R.id.list);
149+
final ListView listView = (ListView) findViewById(R.id.main_activity_list);
150150

151151
messages = new ArrayList<>();
152152
adapter = new ArrayAdapter<>(this,
@@ -229,10 +229,10 @@ private void addMessageOnList(String msg) {
229229
public void onClick(View v) {
230230
int id = v.getId();
231231
switch (id) {
232-
case R.id.connect_button:
232+
case R.id.main_acitivity_connect_button:
233233
connect();
234234
break;
235-
case R.id.disconnect_button:
235+
case R.id.main_activity_disconnect_button:
236236
disconnect();
237237
break;
238238
case R.id.send_button:

websockets-example/src/main/res/layout/main.xml renamed to websockets-example/src/main/res/layout/main_activity.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
android:orientation="horizontal" >
1515

1616
<Button
17-
android:id="@+id/connect_button"
17+
android:id="@+id/main_acitivity_connect_button"
1818
android:layout_width="0dp"
1919
android:layout_height="wrap_content"
2020
android:layout_weight="1"
2121
android:text="connect" />
2222

2323
<Button
24-
android:id="@+id/disconnect_button"
24+
android:id="@+id/main_activity_disconnect_button"
2525
android:layout_width="0dp"
2626
android:layout_height="wrap_content"
2727
android:layout_weight="1"
@@ -36,7 +36,7 @@
3636
</LinearLayout>
3737

3838
<ListView
39-
android:id="@android:id/list"
39+
android:id="@+id/main_activity_list"
4040
android:layout_width="fill_parent"
4141
android:layout_height="fill_parent"
4242
android:layout_alignParentBottom="true"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath 'com.android.tools.build:gradle:1.1.0'
7+
}
8+
}
9+
10+
apply plugin: 'com.android.application'
11+
12+
repositories {
13+
mavenCentral()
14+
maven { url 'https://commondatastorage.googleapis.com/maven-repository/' }
15+
}
16+
17+
dependencies {
18+
compile project(":websockets-rxjava")
19+
compile 'io.reactivex:rxandroid:0.24.0'
20+
compile 'com.android.support:recyclerview-v7:21.0.0'
21+
compile 'com.google.guava:guava:18.0'
22+
//compile "com.appunite:websockets:1.0"
23+
24+
testCompile "org.hamcrest:hamcrest-all:1.3"
25+
testCompile 'junit:junit:4.11'
26+
testCompile 'org.mockito:mockito-all:1.9.5'
27+
testCompile 'com.google.truth:truth:0.25'
28+
testCompile 'com.google.guava:guava:18.0'
29+
}
30+
31+
android {
32+
compileSdkVersion 22
33+
buildToolsVersion "22.0.1"
34+
35+
defaultConfig {
36+
minSdkVersion 8
37+
targetSdkVersion 22
38+
}
39+
buildTypes {
40+
release {
41+
proguardFiles = [getDefaultProguardFile('proguard-android.txt'), 'src/main/proguard-project.txt']
42+
}
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.appunite.socket"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="8"
9+
android:targetSdkVersion="16" />
10+
11+
<uses-permission android:name="android.permission.INTERNET" />
12+
13+
<application
14+
android:allowBackup="true"
15+
android:icon="@drawable/ic_launcher"
16+
android:label="@string/app_name" >
17+
<activity
18+
android:name=".MainActivity"
19+
android:label="@string/app_name" >
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>

0 commit comments

Comments
 (0)