-
Notifications
You must be signed in to change notification settings - Fork 589
/
Copy pathbackground.js
42 lines (39 loc) · 1.19 KB
/
background.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
var udpSocket = new udp();
udpSocket.localPort = 8943;
udpSocket.receive = receiveMsg;
udpSocket.init(function(){
udpSocket.joinGroup('224.0.1.100', function(){
chrome.runtime.onMessage.addListener(function(message, sender, callback){
if(message.action == 'send'){
var buf = str2ab(message.msg);
udpSocket.send('224.0.1.100', udpSocket.localPort, buf, function(){
//message is sent
});
}
});
chrome.app.runtime.onLaunched.addListener(function(){
chrome.app.window.create('main.html', {
'id': 'main',
'bounds': {
'width': 400,
'height': 600
}
});
});
});
});
function receiveMsg(info){
var msg = ab2str(info.data);
chrome.runtime.sendMessage({action:'receive', msg:msg});
}
function str2ab(str){
var buf = new ArrayBuffer(str.length*2);
bufView = new Uint16Array(buf);
for(var i=0; i<str.length; i++){
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function ab2str(buf){
return String.fromCharCode.apply(null, new Uint16Array(buf));
}