forked from robertsambuena/chat_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
164 lines (145 loc) · 4.68 KB
/
Copy pathapp.js
File metadata and controls
164 lines (145 loc) · 4.68 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//dependencies
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoose = require('mongoose');
//connect to database
mongoose.connect('mongodb://localhost/chat_db');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log('callback');
var users_schema = mongoose.Schema({
id: String,
nickname: String,
created_at: Date,
sockets: []
});
var user_model = mongoose.model('users', users_schema);
var new_user = new user_model({
id: 12,
nickname: 'testboys',
created_at: new Date(),
sockets: ['h929svnert0fsdfo', 'urbvpn0378djqpbso3']
});
console.log('new_user',new_user);
new_user.save(function (err, res) {
if (err) return console.error(err);
console.log('new_user res',res);
});
user_model.find(function (err, new_user) {
if (err) return console.error(err);
console.log('find_all', new_user);
});
user_model.find({ name: /^testdata/ }, function (err, res) {
if (err) return console.error(err);
console.log('find', res);
});
});
//custom variables
var __current_users = {};
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/js', function(req, res){
res.sendFile(__dirname + '/index.js');
});
app.get('/css', function(req, res){
res.sendFile(__dirname + '/index.css');
});
//convert object to array
function obj_to_array (obj) {
if ( obj!== null && typeof obj === 'object')
return Object.keys(obj).map(function (key) {return obj[key]});
return false;
}
// SOCKET DECLARATION
var User = io.of('/default_user');
User.on('connection', function(socket) {
//join default_room
socket.join('default_room');
/* Catch events from client */
//connect
socket.on('connect', function(user){
console.log('user connected');
});
//disconnect
socket.on('disconnect', function() {
console.log('user disconnected', __current_users);
});
//someone connected
socket.on('user_connect', function(user) {
//send to everyone except the sender
if (!user.sockets) user.sockets = []; //initialize if not declared user.sockets
if (user && user.id) {
var specific_user = chatuser.get(user.id);
user.sockets.push(socket.id); //save new socket to user
if (specific_user) user = specific_user;
else chatuser.edit(user.id, user);
}
socket.in('default_room').emit('user_connect', user); //send event
});
//someone sent a message
socket.on('chat_message', function(chat) {
//send to everyone except the sender
socket.in('default_room').emit('chat_message', chat);
});
socket.on('get_current_users', function(chat) {
//send to everyone except the sender
check_all_user_status();
});
});
//user resource
//__current_users will be replaced with a real database resource
function ChatUser () {
this.get = function (id) {
if (id) return __current_users;
else return __current_users[id] || false
};
this.add = function (user) {
return __current_users[user.id] = user;
};
this.edit = function (id, user) {
if (__current_users[id]) return __current_users[id].user = user;
return false;
};
this.get_by_socket = function (sid) {
//get current online users
var __current_users_arr = obj_to_array(__current_users);
return __current_users_arr.filter(function (arr) {
return !!~arr.sockets.indexOf(sid);
});
};
this.remove_socket = function (sid) {
//get current online users
var __current_users_arr = obj_to_array(__current_users);
return __current_users_arr.filter(function (arr) {
var result = arr.sockets.indexOf(sid)
if (!!~result){
__current_users[__current_users_arr.id].sockets[result].splice(result, 1);
}
return true;
});
};
}
var chatuser = new ChatUser();
//get current online users
function check_all_user_status() {
console.log('User.adapter.rooms[]',User.adapter.rooms['default_room']);
var socket_ids = Object.keys(User.adapter.rooms['default_room'] || {}),
online_users = [],
__current_users_arr = obj_to_array(chatuser.get());
console.log('__current_users',__current_users);
//check the socket_ids from users
socket_ids.forEach(function(sid) {
online_users.push(chatuser.get_by_socket(sid));
console.log('online_users', online_users.length);
});
console.log('send event \'current online\': ' + __current_users_arr.length);
//send to all sockets
io.of('/default_user').in('default_room').emit('get_current_users', online_users);
}
setInterval(check_all_user_status, 5000);
http.listen(3000, function(){
console.log('listening on *:3000');
});