-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.js
64 lines (49 loc) · 1.33 KB
/
udp.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
import base64 from 'base64-js';
import dgram from 'dgram';
// only works for 8-bit chars
function toByteArray(obj) {
var uint = new Uint8Array(obj.length);
for (var i = 0, l = obj.length; i < l; i++){
uint[i] = obj.charCodeAt(i);
}
return new Uint8Array(uint);
};
export class Client {
constructor() {
let self = this;
this.socket = dgram.createSocket('udp4');
this.socket.bind(10000, function() {
self.socket.setBroadcast(true);
self.findUdpServer('whois;');
});
this.CLIENT_PORT = 10000;
};
setServerAddress(address) {
this.ADDR = address;
}
findUdpServer(text, callback) {
let self = this;
var msg=toByteArray(text);
self.socket.send(msg, 0, msg.length, this.CLIENT_PORT, '255.255.255.255', callback)
}
sendUdpPacket(text, callback) {
let self = this;
var msg=toByteArray(text);
self.socket.send(msg, 0, msg.length, this.CLIENT_PORT, this.ADDR, callback)
};
}
export class Server {
constructor() {
this.socket = dgram.createSocket('udp4');
this.SERVER_PORT = 10001;
this.socket.bind(this.SERVER_PORT, function(err) {
if (err) throw err;
})
};
listen(callback) {
this.socket.on('message', function(data, rinfo) {
var str = String.fromCharCode.apply(null, new Uint8Array(data));
callback(str, rinfo);
})
}
}