Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
[INSTORE-540] close socket on destroy (#6)
Browse files Browse the repository at this point in the history
* Close the socket/thread using the CordovaPlugin onDestroy function

* Semi-colons my man

* Captial L in log
  • Loading branch information
bbil authored and darran-kelinske-fivestars committed Jan 9, 2020
1 parent d7c215b commit a373ef1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/android/Dgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ public class Dgram extends CordovaPlugin {
private DatagramSocket datagramSocket;
private DatagramSocketListener datagramSocketListener;

@Override
public void onDestroy() {
super.onDestroy();

Log.d(TAG, "onDestroy: cleanup thread and socket");
closeSocket();
}

@Override
public boolean execute(
final String action,
final String action,
final JSONArray data,
final CallbackContext callbackContext
) throws JSONException {
Expand All @@ -53,7 +61,7 @@ public boolean execute(
final String message = data.getString(0);
final String address = data.getString(1);
final int port = data.getInt(2);

cordova.getThreadPool().execute(new DatagramSocketSend(
this.datagramSocket,
callbackContext,
Expand All @@ -76,7 +84,7 @@ public boolean execute(
}

private void openSocket(
final JSONArray data,
final JSONArray data,
final CallbackContext callbackContext
) throws JSONException {
this.closeSocket();
Expand All @@ -92,7 +100,7 @@ private void openSocket(
}

private void open(
final int port,
final int port,
final boolean isBroadcast
) throws SocketException {
this.datagramSocket = new DatagramSocket(port);
Expand All @@ -118,7 +126,7 @@ private void startListening(
private void closeSocket() {
try {
this.closeListener();

if (this.datagramSocket != null) {
if (!this.datagramSocket.isClosed()) {
this.datagramSocket.close();
Expand Down

0 comments on commit a373ef1

Please sign in to comment.