@@ -15,7 +15,6 @@ import java.net.HttpURLConnection
1515import java.net.URI
1616import java.net.URL
1717import java.util.*
18- import javax.net.ssl.HttpsURLConnection
1918
2019class 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 ) {
0 commit comments