Skip to content

Commit 9b8608a

Browse files
committed
2014041001
1 parent af8531d commit 9b8608a

File tree

6 files changed

+248
-0
lines changed

6 files changed

+248
-0
lines changed

local_messager/background.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var udpSocket = new udp();
2+
udpSocket.localPort = 8943;
3+
udpSocket.receive = receiveMsg;
4+
udpSocket.init(function(){
5+
udpSocket.joinGroup('224.0.1.100', function(){
6+
chrome.runtime.onMessage.addListener(function(message, sender, callback){
7+
if(message.action == 'send'){
8+
var buf = str2ab(message.msg);
9+
udpSocket.send('224.0.1.100', udpSocket.localPort, buf, function(){
10+
//message is sent
11+
});
12+
}
13+
});
14+
chrome.app.runtime.onLaunched.addListener(function(){
15+
chrome.app.window.create('main.html', {
16+
'id': 'main',
17+
'bounds': {
18+
'width': 400,
19+
'height': 600
20+
}
21+
});
22+
});
23+
});
24+
});
25+
26+
function receiveMsg(info){
27+
var msg = ab2str(info.data);
28+
chrome.runtime.sendMessage({action:'receive', msg:msg});
29+
}
30+
31+
function str2ab(str){
32+
var buf = new ArrayBuffer(str.length*2);
33+
bufView = new Uint16Array(buf);
34+
for(var i=0; i<str.length; i++){
35+
bufView[i] = str.charCodeAt(i);
36+
}
37+
return buf;
38+
}
39+
40+
function ab2str(buf){
41+
return String.fromCharCode.apply(null, new Uint16Array(buf));
42+
}

local_messager/lm.png

6.05 KB
Loading

local_messager/main.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<html>
2+
<head>
3+
<title>Local Messager</title>
4+
<style>
5+
* {
6+
margin: 0;
7+
padding: 0;
8+
}
9+
10+
body {
11+
border: #EEE 1px solid;
12+
}
13+
14+
#history {
15+
margin: 10px;
16+
border: gray 1px solid;
17+
height: 480px;
18+
overflow-y: auto;
19+
overflow-x: hidden;
20+
}
21+
22+
#history div {
23+
padding: 10px;
24+
border-bottom: #EEE 1px solid;
25+
}
26+
27+
#msg {
28+
margin: 10px;
29+
width: 380px;
30+
height: 80px;
31+
box-sizing: border-box;
32+
border: gray 1px solid;
33+
font-size: 24px;
34+
}
35+
</style>
36+
</head>
37+
<body>
38+
<div id="history"></div>
39+
<input type="text" id="msg" />
40+
<script src="main.js"></script>
41+
</body>
42+
</html>

local_messager/main.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
document.getElementById('msg').onkeyup = function(e){
2+
if(e.keyCode==13){
3+
chrome.runtime.sendMessage({
4+
action:'send',
5+
msg:encodeURIComponent(this.value)
6+
});
7+
this.value = '';
8+
}
9+
}
10+
11+
chrome.runtime.onMessage.addListener(function(message, sender, callback){
12+
if(message.action == 'receive'){
13+
var el = document.createElement('div');
14+
el.innerText = decodeURIComponent(message.msg);
15+
document.getElementById('history').appendChild(el);
16+
}
17+
});

local_messager/manifest.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"app": {
3+
"background": {
4+
"scripts": ["udp.js", "background.js"]
5+
}
6+
},
7+
"manifest_version": 2,
8+
"name": "Local Messager",
9+
"version": "1.0",
10+
"description": "A local network chating application.",
11+
"icons": {
12+
"128": "lm.png"
13+
},
14+
"sockets": {
15+
"udp": {
16+
"send": "224.0.1.100",
17+
"bind": ":*",
18+
"multicastMembership": ""
19+
}
20+
}
21+
}

local_messager/udp.js

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
function udp(){
2+
var _udp = chrome.sockets.udp;
3+
this.option = {},
4+
this.socketId = 0,
5+
this.localAddress = '0.0.0.0',
6+
this.localPort = 0,
7+
8+
this.create = function(callback){
9+
_udp.create(this.option, function(socketInfo){
10+
this.socketId = socketInfo.socketId;
11+
callback();
12+
}.bind(this));
13+
}.bind(this),
14+
15+
this.update = function(){
16+
_udp.update(this.socketId, newSocketOption, callback);
17+
}.bind(this),
18+
19+
this.pause = function(isPaused, callback){
20+
_udp.setPaused(this.socketId, isPaused, callback);
21+
}.bind(this),
22+
23+
this.bind = function(callback){
24+
_udp.bind(this.socketId, this.localAddress, this.localPort, callback);
25+
}.bind(this),
26+
27+
this.close = function(callback){
28+
_udp.close(this.socketId, callback);
29+
}.bind(this),
30+
31+
this.send = function(address, port, data, callback){
32+
_udp.send(this.socketId, data, address, port, callback);
33+
}.bind(this),
34+
35+
this.receive = function(info){
36+
console.log('Received data from '+info.removeAddress+':'+info.removePort);
37+
},
38+
39+
this.error = function(code){
40+
console.log('An error occurred with code '+code);
41+
},
42+
43+
this.joinGroup = function(address, callback){
44+
_udp.joinGroup(this.socketId, address, function(code){
45+
if(code<0){
46+
this.error(code);
47+
return false;
48+
}
49+
else{
50+
callback();
51+
}
52+
}.bind(this));
53+
}.bind(this),
54+
55+
this.leaveGroup = function(address, callback){
56+
_udp.leaveGroup(this.socketId, address, function(code){
57+
if(code<0){
58+
this.error(code);
59+
return false;
60+
}
61+
else{
62+
callback();
63+
}
64+
}.bind(this));
65+
}.bind(this),
66+
67+
this.setMilticastTTL = function(ttl, callback){
68+
_udp.setMulticastTimeToLive(this.socketId, ttl, function(code){
69+
if(code<0){
70+
this.error(code);
71+
return false;
72+
}
73+
else{
74+
callback();
75+
}
76+
}.bind(this));
77+
}.bind(this),
78+
79+
this.setMilticastLoopback = function(enabled, callback){
80+
_udp.setMulticastLoopbackMode(this.sockedId, enabled, function(code){
81+
if(code<0){
82+
this.error(code);
83+
return false;
84+
}
85+
else{
86+
callback();
87+
}
88+
}.bind(this));
89+
}.bind(this),
90+
91+
this.getInfo = function(callback){
92+
_udp.getInfo(this.socketId, callback);
93+
}.bind(this),
94+
95+
this.getSockets = function(callback){
96+
_udp.getSockets (callback);
97+
}.bind(this),
98+
99+
this.getGroups = function(callback){
100+
_udp.getJoinedGroups(this.socketId, callback);
101+
}.bind(this),
102+
103+
this.init = function(callback){
104+
this.create(function(){
105+
this.bind(function(code){
106+
if(code<0){
107+
this.error(code);
108+
return false;
109+
}
110+
else{
111+
callback();
112+
}
113+
}.bind(this));
114+
_udp.onReceive.addListener(function(info){
115+
if(info.socketId==this.socketId){
116+
this.receive(info);
117+
}
118+
}.bind(this));
119+
_udp.onReceiveError.addListener(function(info){
120+
if(info.socketId==this.socketId){
121+
this.error(info.resultCode);
122+
}
123+
});
124+
}.bind(this));
125+
}.bind(this)
126+
}

0 commit comments

Comments
 (0)