Skip to content

Commit 6f85454

Browse files
DeveloperDeveloper
authored andcommitted
Added ios basics
1 parent 32003fe commit 6f85454

845 files changed

Lines changed: 49641 additions & 13 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

0 Bytes
Binary file not shown.

demo/.DS_Store

0 Bytes
Binary file not shown.

demo/SocketDemoApp/.DS_Store

0 Bytes
Binary file not shown.
6 KB
Binary file not shown.

demo/SocketDemoApp/platforms/android/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
</activity>
1111
</application>
1212
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
13+
<uses-permission android:name="android.permission.INTERNET" />
1314
</manifest>
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
cordova.define('cordova/plugin_list', function(require, exports, module) {
2-
module.exports = [];
2+
module.exports = [
3+
{
4+
"file": "plugins/com.tlantic.plugins.socket/www/socket.js",
5+
"id": "com.tlantic.plugins.socket.Socket",
6+
"clobbers": [
7+
"window.tlantic.plugins.socket"
8+
]
9+
}
10+
];
311
module.exports.metadata =
412
// TOP OF METADATA
5-
{}
13+
{
14+
"com.tlantic.plugins.socket": "0.2.0"
15+
}
616
// BOTTOM OF METADATA
717
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
cordova.define("com.tlantic.plugins.socket.Socket", function(require, exports, module) { /* global module, require, document */
2+
var exec = require('cordova/exec');
3+
4+
//
5+
function Socket(){
6+
'use strict';
7+
8+
this.receiveHookName = 'SOCKET_RECEIVE_DATA_HOOK'; // *** Event name to act as "hook" for data receiving
9+
this.pluginRef = 'Socket'; // *** Plugin reference for Cordova.exec calls
10+
}
11+
12+
//
13+
Socket.prototype.connect = function (successCallback, errorCallback, host, port) {
14+
'use strict';
15+
exec(successCallback, errorCallback, this.pluginRef, 'connect', [host, port]);
16+
};
17+
18+
//
19+
Socket.prototype.disconnect = function (successCallback, errorCallback, host, port) {
20+
'use strict';
21+
exec(successCallback, errorCallback, this.pluginRef, 'disconnect', [host, port]);
22+
};
23+
24+
//
25+
Socket.prototype.disconnectAll = function (successCallback, errorCallback) {
26+
'use strict';
27+
exec(successCallback, errorCallback, this.pluginRef, 'disconnectAll', []);
28+
};
29+
30+
//
31+
Socket.prototype.send = function (successCallback, errorCallback, host, port, data) {
32+
'use strict';
33+
exec(successCallback, errorCallback, this.pluginRef, 'send', [host, port, data]);
34+
};
35+
36+
//
37+
Socket.prototype.receive = function (host, port, chunk) {
38+
'use strict';
39+
40+
var evReceive = document.createEvent('Events');
41+
42+
evReceive.initEvent(this.receiveHookName);
43+
evReceive.metadata = {
44+
host: host,
45+
port: port,
46+
data: chunk
47+
};
48+
49+
document.dispatchEvent(evReceive);
50+
};
51+
52+
module.exports = new Socket();
53+
});

demo/SocketDemoApp/platforms/android/res/xml/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
</author>
1414
<content src="index.html" />
1515
<access origin="*" />
16+
<feature name="Socket">
17+
<param name="android-package" value="com.tlantic.plugins.socket.SocketPlugin" />
18+
</feature>
1619
</widget>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
9+
10+
/**
11+
* @author viniciusl
12+
*
13+
* This class represents a socket connection, behaving like a thread to listen
14+
* a TCP port and receive data
15+
*/
16+
public class Connection extends Thread {
17+
private SocketPlugin hook;
18+
19+
private Socket callbackSocket;
20+
private PrintWriter writer;
21+
private BufferedReader reader;
22+
23+
private Boolean mustClose;
24+
private String host;
25+
private int port;
26+
27+
28+
/**
29+
* Creates a TCP socket connection object.
30+
*
31+
* @param pool Object containing "sendMessage" method to be called as a callback for data receive.
32+
* @param host Target host for socket connection.
33+
* @param port Target port for socket connection
34+
*/
35+
public Connection(SocketPlugin pool, String host, int port) {
36+
super();
37+
setDaemon(true);
38+
39+
this.mustClose = false;
40+
this.host = host;
41+
this.port = port;
42+
this.hook = pool;
43+
}
44+
45+
46+
/**
47+
* Returns socket connection state.
48+
*
49+
* @return true if socket connection is established or false case else.
50+
*/
51+
public boolean isConnected() {
52+
return this.callbackSocket.isConnected();
53+
}
54+
55+
56+
/**
57+
* Closes socket connection.
58+
*/
59+
public void close() {
60+
this.mustClose = true;
61+
}
62+
63+
64+
/**
65+
* Writes on socket output stream to send data to target host.
66+
*
67+
* @param data information to be sent
68+
*/
69+
public void write(String data) {
70+
this.writer.println(data);
71+
}
72+
73+
74+
75+
/* (non-Javadoc)
76+
* @see java.lang.Thread#run()
77+
*/
78+
public void run() {
79+
String chunk = null;
80+
81+
// creating connection
82+
try {
83+
84+
this.callbackSocket = new Socket(this.host, this.port);
85+
this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true);
86+
this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream()));
87+
88+
} catch (IOException e) {
89+
e.printStackTrace();
90+
this.close();
91+
}
92+
93+
// receiving data chunk
94+
while(!this.mustClose){
95+
96+
try {
97+
chunk = reader.readLine().replaceAll("\"\"", "null");
98+
System.out.print("## RECEIVED DATA: " + chunk);
99+
hook.sendMessage(this.host, this.port, chunk);
100+
} catch (IOException e) {
101+
e.printStackTrace();
102+
}
103+
}
104+
105+
106+
// closing connection
107+
try {
108+
callbackSocket.close();
109+
} catch (IOException e) {
110+
e.printStackTrace();
111+
}
112+
113+
}
114+
115+
}

0 commit comments

Comments
 (0)