-
Notifications
You must be signed in to change notification settings - Fork 5
/
bj.js
94 lines (74 loc) · 2.02 KB
/
bj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// © Dan Tamas - http://dan-tamas.me
// More tutorials on http://cssgallery.info/category/titanium-appcelerator/
// This code is MIT licensed.
var bjnet = function(name,fn_receive) {
var services = null;
var bonjourSocket = Titanium.Network.createTCPSocket({
hostName:Titanium.Network.INADDR_ANY,
port:40401,
mode:Titanium.Network.READ_WRITE_MODE
});
// bonjourSocket.addEventListener('read', function(_ev) {
// DO WE NEED THIS ?
// )};
var localService = Titanium.Network.createBonjourService({
name:name,
type:'_utest._tcp',
domain:'local.'
});
// Searcher for finding other services
var serviceBrowser = Titanium.Network.createBonjourBrowser({
serviceType:'_utest._tcp',
domain:'local.'
});
var update_services = function(e) {
Titanium.API.info(JSON.stringify(e));
services = e['services'];
for (var i=0; i < services.length; i++) {
var service = services[i];
if ( (service.name != name) && (service.socket == null || !service.socket.isValid) ) {
service.resolve();
service.socket.addEventListener('read', function(x) {
fn_receive(x['data'].text);
});
service.socket.connect();
}
}
};
serviceBrowser.addEventListener('updatedServices', update_services);
return {
start: function(){
bonjourSocket.listen();
try
{
localService.publish(bonjourSocket);
serviceBrowser.search();
}
catch (e) {
Titanium.API.info( JSON.stringify(e) );
}
},
write: function(data) {
bonjourSocket.write(data);
},
stop: function(){
if (serviceBrowser.isSearching) {
serviceBrowser.stopSearch();
}
Titanium.API.info('Stopped search...');
localService.stop();
Titanium.API.info('Stopped service...');
if (bonjourSocket.isValid) {
bonjourSocket.close();
}
Titanium.API.info('Closed socket...');
for (var i=0; i < services.length; i++) {
var service = services[i];
if (service.socket.isValid) {
service.socket.close();
}
Titanium.API.info('Closed socket to service '+service.name+"...");
}
}
}
};