10
10
import java .io .*;
11
11
import java .net .Socket ;
12
12
13
+ /**
14
+ * Capacitor plugin that provides raw TCP socket functionality.
15
+ * Implements methods to connect, send, receive, and disconnect.
16
+ */
13
17
@ CapacitorPlugin (name = "SocketPlugin" )
14
18
public class SocketPlugin extends Plugin {
15
19
private Socket socket ;
16
20
private BufferedReader reader ;
17
21
private BufferedWriter writer ;
22
+ private boolean isConnected = false ;
18
23
19
24
@ PluginMethod
20
25
public void connect (PluginCall call ) {
21
26
String ip = call .getString ("ip" );
22
27
int port = call .getInt ("port" );
28
+
29
+ // Validate inputs
30
+ if (ip == null || ip .isEmpty ()) {
31
+ call .reject ("IP address is required" );
32
+ return ;
33
+ }
34
+
35
+ if (port <= 0 || port > 65535 ) {
36
+ call .reject ("Invalid port number" );
37
+ return ;
38
+ }
39
+
40
+ // Prevent duplicate connections
41
+ if (socket != null && !socket .isClosed ()) {
42
+ call .reject ("Already connected; please disconnect first" );
43
+ return ;
44
+ }
45
+
23
46
try {
24
47
socket = new Socket (ip , port );
48
+ socket .setSoTimeout (30_000 ); // 30s timeout
25
49
reader = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
26
50
writer = new BufferedWriter (new OutputStreamWriter (socket .getOutputStream ()));
51
+ isConnected = true ;
27
52
JSObject ret = new JSObject ();
28
53
ret .put ("success" , true );
29
54
call .resolve (ret );
30
55
} catch (Exception e ) {
56
+ closeResources ();
31
57
call .reject ("Connection failed: " + e .getMessage ());
32
58
}
33
59
}
34
60
35
- @ PluginMethod
61
+ @ PluginMethod
36
62
public void send (PluginCall call ) {
37
63
String data = call .getString ("data" );
64
+
65
+ // Validate input
66
+ if (data == null ) {
67
+ call .reject ("Data is required" );
68
+ return ;
69
+ }
70
+
71
+ // Check connection state
72
+ if (socket == null || socket .isClosed () || !isConnected ) {
73
+ call .reject ("Not connected to any server" );
74
+ return ;
75
+ }
76
+
38
77
try {
39
78
writer .write (data );
40
79
writer .flush ();
41
80
JSObject ret = new JSObject ();
42
81
ret .put ("success" , true );
43
82
call .resolve (ret );
44
83
} catch (Exception e ) {
84
+ closeResources ();
85
+ isConnected = false ;
45
86
call .reject ("Send failed: " + e .getMessage ());
46
87
}
47
88
}
@@ -61,12 +102,36 @@ public void receive(PluginCall call) {
61
102
@ PluginMethod
62
103
public void disconnect (PluginCall call ) {
63
104
try {
64
- if (socket != null ) socket .close ();
105
+ closeResources ();
106
+ isConnected = false ;
65
107
JSObject ret = new JSObject ();
66
108
ret .put ("success" , true );
67
109
call .resolve (ret );
68
110
} catch (Exception e ) {
69
111
call .reject ("Disconnect failed: " + e .getMessage ());
70
112
}
71
113
}
114
+
115
+ /**
116
+ * Helper method to close all resources and clean up state
117
+ */
118
+ private void closeResources () {
119
+ try {
120
+ if (reader != null ) {
121
+ reader .close ();
122
+ reader = null ;
123
+ }
124
+ if (writer != null ) {
125
+ writer .close ();
126
+ writer = null ;
127
+ }
128
+ if (socket != null ) {
129
+ socket .close ();
130
+ socket = null ;
131
+ }
132
+ } catch (IOException e ) {
133
+ // Log but continue cleanup
134
+ getContext ().getActivity ().runOnUiThread (() ->
135
+ Log .e ("SocketPlugin" , "Error closing resources" , e ));
136
+ }
72
137
}
0 commit comments