Skip to content

Commit 39a0c52

Browse files
committed
Added java version of library
1 parent 8e31604 commit 39a0c52

File tree

15 files changed

+394
-16
lines changed

15 files changed

+394
-16
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ dependencies {
2626
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
2727
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
2828
implementation 'com.google.code.gson:gson:2.8.1'
29-
implementation project(path: ':dotnetcoresignalrclient')
29+
implementation project(path: ':dotnetcoresignalrclientjava')
3030
}

app/src/main/java/com/smartarmenia/websocketclient/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.widget.Toast
66
import com.google.gson.Gson
7-
import com.smartarmenia.dotnetcoresignalrclient.*
7+
import com.smartarmenia.dotnetcoresignalrclientjava.*
88
import kotlinx.android.synthetic.main.activity_main.*
99

1010

@@ -29,7 +29,7 @@ class MainActivity : AppCompatActivity(), HubConnectionListener, HubEventListene
2929

3030
}
3131

32-
private val connection: HubConnection = WebSocketHubConnection("http://192.168.0.109:5002/signalr/hubs/auth")
32+
private val connection: HubConnection = WebSocketHubConnection("http://192.168.0.104:5002/signalr/hubs/auth")
3333

3434
override fun onCreate(savedInstanceState: Bundle?) {
3535
super.onCreate(savedInstanceState)

dotnetcoresignalrclient/src/main/java/com/smartarmenia/dotnetcoresignalrclient/WebSocketHubConnection.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import java.net.HttpURLConnection
1515
import java.net.URI
1616
import java.net.URL
1717
import java.util.*
18-
import javax.net.ssl.HttpsURLConnection
1918

2019
class WebSocketHubConnection(private val hubUrl: String) : HubConnection {
2120
companion object {
@@ -27,25 +26,25 @@ class WebSocketHubConnection(private val hubUrl: String) : HubConnection {
2726
private val listeners = mutableListOf<HubConnectionListener>()
2827
private val eventListeners = mutableMapOf<String, MutableList<HubEventListener>>()
2928
private val parsedUri = Uri.parse(hubUrl)
29+
private val gson = Gson()
3030

3131
override fun connect(authHeader: String?) {
3232
Log.i(TAG, "Requesting connection id...")
33-
val connection: HttpURLConnection = when {
34-
parsedUri.scheme == "http" -> URL(hubUrl).openConnection() as HttpURLConnection
35-
parsedUri.scheme == "https" -> URL(hubUrl).openConnection() as HttpsURLConnection
36-
else -> throw RuntimeException("URL must start with http or https")
37-
}
33+
if (!(parsedUri.scheme == "http" || parsedUri.scheme == "https"))
34+
throw RuntimeException("URL must start with http or https")
35+
36+
val connection: HttpURLConnection = URL(hubUrl).openConnection() as HttpURLConnection
3837
if (authHeader != null) {
3938
connection.addRequestProperty("Authorization", authHeader)
4039
}
4140
connection.connectTimeout = 15000
42-
connection.connectTimeout = 15000
41+
connection.readTimeout = 15000
4342
connection.requestMethod = "OPTIONS"
4443
val responseCode = connection.responseCode
4544
when (responseCode) {
4645
200 -> {
4746
val result = InputStreamConverter.convert(connection.inputStream)
48-
val jsonElement = Gson().fromJson<JsonElement>(result, JsonElement::class.java)
47+
val jsonElement = gson.fromJson<JsonElement>(result, JsonElement::class.java)
4948
val connectionId = jsonElement.asJsonObject.get("connectionId").asString
5049
if (!jsonElement.asJsonObject.get("availableTransports").asJsonArray.toList().map { it.asString }.contains("WebSockets")) {
5150
throw RuntimeException("The server does not support WebSockets transport")
@@ -60,7 +59,7 @@ class WebSocketHubConnection(private val hubUrl: String) : HubConnection {
6059
private fun connectClient(connectionId: String?, authHeader: String?) {
6160
val uriBuilder = parsedUri.buildUpon()
6261
uriBuilder.appendQueryParameter("id", connectionId)
63-
uriBuilder.scheme(if (parsedUri.scheme == "http") "ws" else "wss")
62+
uriBuilder.scheme(parsedUri.scheme.replace("http", "ws"))
6463
val uri = uriBuilder.build()
6564
val headers = if (authHeader == null) null else mapOf("Authorization" to authHeader)
6665
client = object : WebSocketClient(URI(uri.toString()), Draft_6455(), headers, 15000) {
@@ -72,7 +71,7 @@ class WebSocketHubConnection(private val hubUrl: String) : HubConnection {
7271

7372
override fun onMessage(s: String) {
7473
Log.i(TAG, s)
75-
val element = Gson().fromJson<SignalRMessage>(s.replace(SPECIAL_SYMBOL, ""), SignalRMessage::class.java)
74+
val element = gson.fromJson<SignalRMessage>(s.replace(SPECIAL_SYMBOL, ""), SignalRMessage::class.java)
7675
if (element.type == 1) {
7776
val message = HubMessage(element.invocationId!!, element.target!!, element.arguments!!)
7877
listeners.forEach { it.onMessage(message) }
@@ -81,7 +80,7 @@ class WebSocketHubConnection(private val hubUrl: String) : HubConnection {
8180
}
8281

8382
override fun onClose(i: Int, s: String, b: Boolean) {
84-
Log.i(TAG, "Closed. Code: $i, $s, $b")
83+
Log.i(TAG, "Closed. Code: $i, Reason: $s, Remote: $b")
8584
listeners.forEach { it.onDisconnected() }
8685
}
8786

@@ -121,7 +120,7 @@ class WebSocketHubConnection(private val hubUrl: String) : HubConnection {
121120

122121
override fun invoke(event: String, vararg parameters: Any) {
123122
val map = mapOf("type" to 1, "invocationId" to UUID.randomUUID().toString(), "target" to event, "arguments" to parameters, "nonblocking" to false)
124-
client.send(Gson().toJson(map) + SPECIAL_SYMBOL)
123+
client.send(gson.toJson(map) + SPECIAL_SYMBOL)
125124
}
126125

127126
override fun addListener(listener: HubConnectionListener) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.1"
6+
7+
8+
defaultConfig {
9+
minSdkVersion 16
10+
targetSdkVersion 26
11+
versionCode 1
12+
versionName "1.0"
13+
14+
}
15+
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
23+
}
24+
25+
dependencies {
26+
implementation fileTree(dir: 'libs', include: ['*.jar'])
27+
implementation 'org.java-websocket:Java-WebSocket:1.3.4'
28+
implementation 'com.google.code.gson:gson:2.8.1'
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.smartarmenia.dotnetcoresignalrclientjava" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.smartarmenia.dotnetcoresignalrclientjava;
2+
3+
public interface HubConnection {
4+
void connect(String authHeader);
5+
6+
void disconnect();
7+
8+
void addListener(HubConnectionListener listener);
9+
10+
void removeListener(HubConnectionListener listener);
11+
12+
void subscribeToEvent(String eventName, HubEventListener eventListener);
13+
14+
void unSubscribeFromEvent(String eventName, HubEventListener eventListener);
15+
16+
void invoke(String event, Object... parameters);
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.smartarmenia.dotnetcoresignalrclientjava;
2+
3+
public interface HubConnectionListener {
4+
void onConnected();
5+
6+
void onDisconnected();
7+
8+
void onMessage(HubMessage message);
9+
10+
void onError(Exception exception);
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.smartarmenia.dotnetcoresignalrclientjava;
2+
3+
public interface HubEventListener {
4+
void onEventMessage(HubMessage message);
5+
}

0 commit comments

Comments
 (0)