forked from telehash/telehash-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
connect.js
50 lines (42 loc) · 1.03 KB
/
connect.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
var telehash = require("../index.js").telehash;
var connector;
telehash.init(function initialised(err) {
if (err) {
console.log(err);
return;
}
console.log("initialised");
telehash.seed(function (status, info) {
if (status === 'offline' && info === 'snat-detected') {
console.log("SNAT detected. Exiting...");
process.exit();
}
if (status !== "online") {
console.log(status);
return;
}
console.log("seeded");
connect("echo.message.back");
});
});
function connect(name) {
if (!connector) connector = telehash.connect(name);
var gotResponse = false;
connector.send("TeleHash Rocks!", function (obj) {
if (obj) {
console.log("Reply #" + obj.count + " MESSAGE: ", obj.message, "from:", obj.from);
gotResponse = true;
} else {
if (!gotResponse) {
console.log("No Replies! :( Retrying..");
setTimeout(function () {
connect(name);
}, 100);
} else {
console.log("We got our replies.. yay!");
telehash.shutdown();
process.exit();
}
}
}, 5); //timeout after 5 seconds.
}