-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
84 lines (72 loc) · 2.21 KB
/
server.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var db, db_name, io, nano;
io = require('socket.io').listen(7000);
nano = require('nano')('http://127.0.0.1:5984');
db_name = "signifier";
db = nano.use(db_name);
io.sockets.on('connection', function(socket) {
var Signifier, address;
address = socket.handshake.address;
console.log("here is the address!: ");
console.log(address);
Signifier = (function() {
function Signifier() {}
Signifier.socket = socket;
Signifier.scoutHood = function(imAt) {
console.log("querying db with [host,path] = ");
console.log([imAt.host, imAt.path]);
return db.view('signifier', 'neighborhood', {
key: [imAt.host, imAt.path]
}, function(err, signs) {
console.log(signs);
return Signifier.socket.emit("heresYourHood", signs);
});
};
Signifier.sendSigns = function(signs) {
return Signifier.socket.emit("heresYourHood", signs);
};
Signifier.deleteAllDocs = function() {
return db.view('signifier', 'neighborhood', function(err, body) {
var a, doc, _i, _len, _ref;
if (err) {
console.log(err);
return;
}
_ref = body.rows;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
a = _ref[_i];
doc = a.value;
db.destroy(doc._id, doc._rev, function(err, body) {
return console.log(err || body);
});
}
return console.log("database purged successfully!");
});
};
return Signifier;
})();
socket.emit('whereYat', {
header: null
});
console.log('whereYat just sent');
socket.on('chillinAt', function(imAt) {
return Signifier.scoutHood(imAt);
});
socket.on("heresASign", function(sign) {
sign.creator = {
ip: socket.handshake.address.address
};
return db.insert(sign, function(err, body, headers) {
if (err === false) return console.log(headers, body);
});
});
socket.on("delete", function(sig) {
return db.destroy(sig.id, sig.rev, function(err, body) {
console.log(err);
console.log(body);
if (!err) return console.log("sig successfully deleted");
});
});
return socket.on("deleteTheWholeShebang", function() {
return Signifier.deleteAllDocs();
});
});