Skip to content

Commit 032c6ee

Browse files
committed
App Crash Fix : #63
The app was crashing when a WebSocketNotConnectedException was thrown and not handled. To prevent this, the websocket.send() method is now enclosed in a try-catch block, which catches the exception and prevents the app from crashing.
1 parent bd3e43d commit 032c6ee

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

app/src/main/java/github/umer0586/sensorserver/websocketserver/SensorWebSocketServer.kt

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import android.view.MotionEvent
1818
import androidx.annotation.RequiresApi
1919
import github.umer0586.sensorserver.util.JsonUtil
2020
import org.java_websocket.WebSocket
21+
import org.java_websocket.exceptions.WebsocketNotConnectedException
2122
import org.java_websocket.handshake.ClientHandshake
2223
import org.java_websocket.server.WebSocketServer
2324
import java.net.InetSocketAddress
@@ -297,7 +298,11 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
297298
{
298299
if (websocket.getAttachment<Any>() is GPS)
299300
{
300-
websocket.send(location.toJson())
301+
try {
302+
websocket.send(location.toJson())
303+
}catch (e : WebsocketNotConnectedException){
304+
e.printStackTrace()
305+
}
301306
}
302307
}
303308
}
@@ -385,7 +390,11 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
385390
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
386391
{
387392
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.apply {
388-
websocket.send(this.toJson(lastKnownLocation = true))
393+
try {
394+
websocket.send(this.toJson(lastKnownLocation = true))
395+
} catch(e : WebsocketNotConnectedException){
396+
e.printStackTrace()
397+
}
389398
}
390399
}
391400
else
@@ -400,7 +409,11 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
400409
else {
401410

402411
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.apply {
403-
websocket.send(this.toJson(lastKnownLocation = true))
412+
try {
413+
websocket.send(this.toJson(lastKnownLocation = true))
414+
} catch(e : WebsocketNotConnectedException){
415+
e.printStackTrace()
416+
}
404417
}
405418
}
406419
}
@@ -514,23 +527,38 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
514527
message["x"] = motionEvent.x
515528
message["y"] = motionEvent.y
516529

517-
websocket.send(JsonUtil.toJSON(message))
530+
try {
531+
websocket.send(JsonUtil.toJSON(message))
532+
} catch (e : WebsocketNotConnectedException){
533+
e.printStackTrace()
534+
}
535+
518536
}
519537

520538
MotionEvent.ACTION_DOWN -> {
521539
message["action"] = "ACTION_DOWN"
522540
message["x"] = motionEvent.x
523541
message["y"] = motionEvent.y
524542

525-
websocket.send(JsonUtil.toJSON(message))
543+
try {
544+
websocket.send(JsonUtil.toJSON(message))
545+
} catch (e : WebsocketNotConnectedException){
546+
e.printStackTrace()
547+
}
548+
526549
}
527550

528551
MotionEvent.ACTION_MOVE -> {
529552
message["action"] = "ACTION_MOVE"
530553
message["x"] = motionEvent.x
531554
message["y"] = motionEvent.y
532555

533-
websocket.send(JsonUtil.toJSON(message))
556+
try {
557+
websocket.send(JsonUtil.toJSON(message))
558+
} catch (e : WebsocketNotConnectedException){
559+
e.printStackTrace()
560+
}
561+
534562
}
535563

536564

@@ -574,12 +602,17 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
574602
{
575603
val clientAssociatedSensor = webSocket.getAttachment<Sensor>()
576604

577-
if (clientAssociatedSensor != null) if (clientAssociatedSensor.type == sensorEvent.sensor.type && !webSocket.isClosing)
605+
if (clientAssociatedSensor != null) if (clientAssociatedSensor.type == sensorEvent.sensor.type && webSocket.isOpen)
578606
{
579607
message["values"] = sensorEvent.values
580608
message["timestamp"] = sensorEvent.timestamp
581609
message["accuracy"] = sensorEvent.accuracy
582-
webSocket.send(JsonUtil.toJSON(message))
610+
try{
611+
webSocket.send(JsonUtil.toJSON(message))
612+
} catch(e: WebsocketNotConnectedException){
613+
e.printStackTrace()
614+
}
615+
583616
}
584617
}
585618
else if (webSocket.getAttachment<Any>() is ArrayList<*>)
@@ -588,13 +621,18 @@ class SensorWebSocketServer(private val context: Context, address: InetSocketAdd
588621

589622
for (clientAssociatedSensor in clientAssociatedSensors)
590623
{
591-
if (clientAssociatedSensor.type == sensorEvent.sensor.type && !webSocket.isClosing)
624+
if (clientAssociatedSensor.type == sensorEvent.sensor.type && webSocket.isOpen)
592625
{
593626
message["values"] = sensorEvent.values
594627
message["timestamp"] = sensorEvent.timestamp
595628
message["accuracy"] = sensorEvent.accuracy
596629
message["type"] = sensorEvent.sensor.stringType
597-
webSocket.send(JsonUtil.toJSON(message))
630+
try{
631+
webSocket.send(JsonUtil.toJSON(message))
632+
} catch (e : WebsocketNotConnectedException){
633+
e.printStackTrace()
634+
}
635+
598636
}
599637
}
600638
}

0 commit comments

Comments
 (0)