-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchatServer.js
72 lines (60 loc) · 1.41 KB
/
chatServer.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
//var file
var fs = require('fs');
var http = require('http');
var mongo = require('mongodb');
var db = new mongo.Db('mydb', new mongo.Server('localhost', 27017, {}), {});
function passToClient(data)
{
var string;
var body;
var i;
var test = console.log(data);
for (i = 0; i < data.length; i++) {
string = data[i].name;
body = data[i].body;
everyone.now.printData(string, body);
}
//everyone.now.printData(test);
}
db.open(function() {
});
server = http.createServer(function(req, res){
fs.readFile('index.html', function(err, data){
res.writeHead(200, {'Content-Type':'text/html'});
res.write(data);
res.end();
});
});
server.listen(8080);
//Now.JS
var everyone = require("now").initialize(server);
everyone.now.distributeMessage = function(name, msg){
everyone.now.receiveMessage(name, msg);
};
everyone.now.distributeAlert = function(name){
everyone.now.peopleOnline(name);
};
everyone.now.leaving = function(name){
everyone.now.removeUsers(name);
};
everyone.now.saveDB = function(name, msg){
db.collection('testCollection', function(err, collection) {
doc = {
"name": name,
"body": msg
};
collection.insert(doc, function(){
});
});
};
everyone.now.printDatabase = function(){
var data = {};
db.collection('testCollection', function(err, collection) {
collection.find(function(err, cursor) {
cursor.toArray(function(err, docs){
data = docs;
passToClient(data);
});
});
});
};