-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbridge.js
49 lines (38 loc) · 1.43 KB
/
bridge.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
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
/**
* To play in network with the emulator:
* telnet 127.0.0.1 5554 (the port may be different)
* auth PQ6AA3GMjjTMISP8 (read the key from %USERPROFILE%/.emulator_console_auth_token on windows or $home/.emulator_console_auth_token on linux)
* redir add udp:25600:25600
* exit
* node bridge.js
* join game to port 25601
*
* ip address of host is 10.0.2.2
*/
let clients = [];
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
let serverInfo = `${rinfo.address}:${rinfo.port}`;
let client = clients.find(it => it.serverInfo == serverInfo);
if (!client) {
console.log(`Got new client ${rinfo.address}:${rinfo.port}`)
client = {serverInfo: serverInfo, udp: dgram.createSocket('udp4')};
client.udp.on('message', (cmsg, crinfo) => {
//console.log(`client got: ${msg.length} bytes from ${crinfo.address}:${crinfo.port}`);
server.send(cmsg, rinfo.port, rinfo.address);
});
clients.push(client);
}
//console.log(`server got: ${msg.length} bytes from ${rinfo.address}:${rinfo.port}`);
client.udp.send(msg, 25600, 'localhost');
});
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
server.bind(25601);