-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (125 loc) · 3.66 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongodb = require('mongodb');
var mongodbServer = new mongodb.Server('localhost', 27017, { auto_reconnect: true});
var db = new mongodb.Db('mydb', mongodbServer);
var storeMessage = function(name,data){
console.log('message stored');
db.open(function() {
db.collection('cappedMessageCollection', function(err, collection) {
/* Insert a data */
collection.insert({
"name":name,
"data":data,
});
});
});
};
io.on('connection', function(socket){
console.log('a user connected');
var client_name=null;
var time_start=Date.parse(new Date());
db.open(function() {
db.collection('usersCollection', function(err, collection) {
collection.find({}).toArray(function(err,users){
users.forEach(function(user){
socket.emit('user_list',user.name);
});
});
});
db.collection('cappedMessageCollection', function(err, collection) {
collection.find({}).toArray(function(err,messages){
messages.forEach(function(message){
if (message.name!=client_name)
{
socket.emit('chat_message',message.name,message.data);
}
});
});
});
});
socket.on('user_name', function(user_name){
console.log('name received');
if (client_name!=null)
{
db.collection('usersCollection', function(err, collection) {
/* Delete a data */
collection.remove({
"name":client_name,
});
});
}
db.open(function() {
db.collection('usersCollection', function(err, collection) {
/* Insert a data */
collection.insert({
"name":user_name
}, function(err, data) {
if (data) {
client_name=user_name;
io.emit('user_list',user_name);
console.log('CN received');
} else {
socket.emit('chat_message',"System:","the name has been taken");
}
});
});
});
});
socket.on('chat_message', function(msg){
io.emit('chat_message',client_name,msg);
storeMessage(client_name,msg);
db.open(function() {
db.collection('mycoll', function(err, collection) {
/* Insert a data */
collection.insert({
"message": msg,
"username": client_name,
"time": Date.parse(new Date()),
}, function(err, data) {
if (data) {
console.log('Successfully Insert');
} else {
console.log('Failed to Insert');
}
});
});
});
});
socket.on('disconnect',function(){
console.log('user disconnect');
io.emit("remove_user",client_name);
db.collection('usersCollection', function(err, collection) {
/* Delete a data */
collection.remove({
"name":client_name,
});
});
db.collection('mycoll', function(err, collection) {
collection.insert({
"username": client_name,
"timestart": time_start,
"timeend": Date.parse(new Date()),
}, function(err, data) {
if (data) {
console.log('Successfully Insert');
} else {
console.log('Failed to Insert');
}
});
});
});
socket.on('get_user',function(){
db.collection('usersCollection', function(err, collection) {
collection.find({}).toArray(function(err,users){
users.forEach(function(user){
socket.emit('user_list',user.name);
});
});
});
});
});
http.listen(8080, function(){
console.log('listening on *:8080');
});