Skip to content

Commit 64eeebf

Browse files
committed
Merge pull request #27 from Tlantic/v0.3.0
V0.3.0 - Merge
2 parents 82e73ae + 1f62df7 commit 64eeebf

11 files changed

Lines changed: 1181 additions & 792 deletions

File tree

plugin.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="com.tlantic.plugins.socket"
5-
version="0.2.0">
5+
version="0.3.0">
66
<name>Socket</name>
77
<description>Tlantic TCP socket plugin</description>
88
<license>GPL</license>
@@ -41,4 +41,19 @@
4141
<source-file src="src/ios/Connection.m" />
4242
</platform>
4343

44+
<!-- windows8 -->
45+
<platform name="windows8">
46+
<js-module src="src/windows8/Connection.js" name="Connection">
47+
<merges target="" />
48+
</js-module>
49+
<js-module src="src/windows8/SocketProxy.js" name="SocketProxy">
50+
<merges target="" />
51+
</js-module>
52+
<config-file target="package.appxmanifest" parent="/Package/Capabilities">
53+
<Capability Name="internetClientServer" />
54+
<Capability Name="privateNetworkClientServer" />
55+
<Capability Name="internetClient" />
56+
</config-file>
57+
</platform>
58+
4459
</plugin>

specs/v03.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Cordova TCP Socket Plugin v0.3.x release notes
2+
==============================================
3+
4+
## v0.3.0
5+
6+
This is the third version of the plugin, with few support to attend basic needs regarding Windows8 platform.
7+
Some issues were closed and a great improvement was made on socket calls. After opening a TCP socket connection,
8+
you can use the connection id to perform operations like send or disconnect.
9+
10+
### Cordova/Phonegap Compatibility
11+
12+
The following versions of Cordova/Phonegap were used to test this plugin:
13+
14+
* Cordova 3.4
15+
* Phonegap 3.4
16+
17+
18+
### Platforms support
19+
20+
Platforms supported by this plugin:
21+
22+
* Android 4.1.1 or later
23+
* iOS 7.x
24+
* Windows 8 (desktop) or later - probably works with a WinRT version
25+
26+
### Limitations
27+
28+
The pause/resume limitation may exists. I didn't take a look on it yet to check if there is any issue.

src/android/Connection.java

Lines changed: 145 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,145 @@
1-
package com.tlantic.plugins.socket;
2-
3-
import java.io.BufferedReader;
4-
import java.io.IOException;
5-
import java.io.InputStreamReader;
6-
import java.io.PrintWriter;
7-
import java.net.Socket;
8-
import java.net.UnknownHostException;
9-
10-
11-
/**
12-
* @author viniciusl
13-
*
14-
* This class represents a socket connection, behaving like a thread to listen
15-
* a TCP port and receive data
16-
*/
17-
public class Connection extends Thread {
18-
private SocketPlugin hook;
19-
20-
private Socket callbackSocket;
21-
private PrintWriter writer;
22-
private BufferedReader reader;
23-
24-
private Boolean mustClose;
25-
private String host;
26-
private int port;
27-
28-
29-
/**
30-
* Creates a TCP socket connection object.
31-
*
32-
* @param pool Object containing "sendMessage" method to be called as a callback for data receive.
33-
* @param host Target host for socket connection.
34-
* @param port Target port for socket connection
35-
*/
36-
public Connection(SocketPlugin pool, String host, int port) {
37-
super();
38-
setDaemon(true);
39-
40-
this.mustClose = false;
41-
this.host = host;
42-
this.port = port;
43-
this.hook = pool;
44-
}
45-
46-
47-
/**
48-
* Returns socket connection state.
49-
*
50-
* @return true if socket connection is established or false case else.
51-
*/
52-
public boolean isConnected() {
53-
return this.callbackSocket.isConnected();
54-
}
55-
56-
/**
57-
* Closes socket connection.
58-
*/
59-
public void close() {
60-
// closing connection
61-
try {
62-
this.mustClose = true;
63-
this.writer.close();
64-
this.reader.close();
65-
callbackSocket.close();
66-
} catch (IOException e) {
67-
e.printStackTrace();
68-
}
69-
}
70-
71-
72-
/**
73-
* Writes on socket output stream to send data to target host.
74-
*
75-
* @param data information to be sent
76-
*/
77-
public void write(String data) {
78-
this.writer.println(data);
79-
}
80-
81-
82-
83-
/* (non-Javadoc)
84-
* @see java.lang.Thread#run()
85-
*/
86-
public void run() {
87-
String chunk = null;
88-
89-
// creating connection
90-
try {
91-
this.callbackSocket = new Socket(this.host, this.port);
92-
this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true);
93-
this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream()));
94-
95-
// receiving data chunk
96-
while(!this.mustClose){
97-
98-
try {
99-
chunk = reader.readLine().replaceAll("\"\"", "null");
100-
System.out.print("## RECEIVED DATA: " + chunk);
101-
hook.sendMessage(this.host, this.port, chunk);
102-
} catch (IOException e) {
103-
e.printStackTrace();
104-
}
105-
}
106-
107-
} catch (UnknownHostException e1) {
108-
// TODO Auto-generated catch block
109-
e1.printStackTrace();
110-
} catch (IOException e1) {
111-
// TODO Auto-generated catch block
112-
e1.printStackTrace();
113-
}
114-
115-
}
116-
117-
}
1+
package com.tlantic.plugins.socket;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintWriter;
7+
import java.net.Socket;
8+
import java.net.UnknownHostException;
9+
10+
11+
/**
12+
* @author viniciusl
13+
*
14+
* This class represents a socket connection, behaving like a thread to listen
15+
* a TCP port and receive data
16+
*/
17+
public class Connection extends Thread {
18+
private SocketPlugin hook;
19+
20+
private Socket callbackSocket;
21+
private PrintWriter writer;
22+
private BufferedReader reader;
23+
24+
private Boolean mustClose;
25+
private String host;
26+
private int port;
27+
28+
29+
/**
30+
* Creates a TCP socket connection object.
31+
*
32+
* @param pool Object containing "sendMessage" method to be called as a callback for data receive.
33+
* @param host Target host for socket connection.
34+
* @param port Target port for socket connection
35+
*/
36+
public Connection(SocketPlugin pool, String host, int port) {
37+
super();
38+
setDaemon(true);
39+
40+
this.mustClose = false;
41+
this.host = host;
42+
this.port = port;
43+
this.hook = pool;
44+
}
45+
46+
47+
/**
48+
* Returns socket connection state.
49+
*
50+
* @return true if socket connection is established or false case else.
51+
*/
52+
public boolean isConnected() {
53+
54+
boolean result = (
55+
this.callbackSocket == null ? false :
56+
this.callbackSocket.isConnected() &&
57+
this.callbackSocket.isBound() &&
58+
!this.callbackSocket.isClosed() &&
59+
!this.callbackSocket.isInputShutdown() &&
60+
!this.callbackSocket.isOutputShutdown());
61+
62+
// if everything apparently is fine, time to test the streams
63+
if (result) {
64+
try {
65+
this.callbackSocket.getInputStream().available();
66+
} catch (IOException e) {
67+
// connection lost
68+
result = false;
69+
}
70+
}
71+
72+
return result;
73+
}
74+
75+
/**
76+
* Closes socket connection.
77+
*/
78+
public void close() {
79+
// closing connection
80+
try {
81+
//this.writer.close();
82+
//this.reader.close();
83+
callbackSocket.shutdownInput();
84+
callbackSocket.shutdownOutput();
85+
callbackSocket.close();
86+
this.mustClose = true;
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
92+
93+
/**
94+
* Writes on socket output stream to send data to target host.
95+
*
96+
* @param data information to be sent
97+
*/
98+
public void write(String data) {
99+
this.writer.println(data);
100+
}
101+
102+
103+
104+
/* (non-Javadoc)
105+
* @see java.lang.Thread#run()
106+
*/
107+
public void run() {
108+
String chunk = null;
109+
110+
// creating connection
111+
try {
112+
this.callbackSocket = new Socket(this.host, this.port);
113+
this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true);
114+
this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream()));
115+
116+
// receiving data chunk
117+
while(!this.mustClose){
118+
119+
try {
120+
121+
if (this.isConnected()) {
122+
chunk = reader.readLine();
123+
124+
if (chunk != null) {
125+
chunk = chunk.replaceAll("\"\"", "null");
126+
System.out.print("## RECEIVED DATA: " + chunk);
127+
hook.sendMessage(this.host, this.port, chunk);
128+
}
129+
}
130+
} catch (Exception e) {
131+
e.printStackTrace();
132+
}
133+
}
134+
135+
} catch (UnknownHostException e1) {
136+
// TODO Auto-generated catch block
137+
e1.printStackTrace();
138+
} catch (IOException e1) {
139+
// TODO Auto-generated catch block
140+
e1.printStackTrace();
141+
}
142+
143+
}
144+
145+
}

0 commit comments

Comments
 (0)