-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.js
More file actions
168 lines (136 loc) · 4.24 KB
/
user.js
File metadata and controls
168 lines (136 loc) · 4.24 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
165
166
167
168
var _ = require('lodash');
var usernamesOccupied = [];
function User(socket) {
this._socket = socket;
this.id = socket.id;
this.name = '';
this.session = socket.handshake.session;
this.sessionInitiated = false;
this.inNamespace = socket.id.split('#')[0];
for (var t in socket) {
this[t] = socket[t];
}
console.log('connected ' + socket.id);
Janta[this.inNamespace].push(this.id);
console.log(this.id + " added to namespace " + this.inNamespace);
this.__init__();
};
User.prototype.emit = function(event, data) {
this._socket.emit(event, data);
}
User.prototype._broadcast = function(event, data) {
this._socket.broadcast.emit(event, data);
}
User.prototype._onMessageRecieved = function(data) {
this._broadcast('message', {
'username': data.username,
'message': data.message
});
this.emit('recieved', {
'username': data.username,
'_hash': data._hash
});
}
User.prototype.checkSession = function(data) {
// check if session is present
if (!Sessions[this.session.id]) {
// console.log("Request login");
this.emit('request login', {
'id': this.id
});
return;
}
//Session is runnning.
this.sessionInit({
restore: true
});
this.emit('welcome back', {
'username': this.name
});
this._broadcast('back', {
'username': this.name
});
console.log("Session restored for " + this.name);
};
User.prototype.setUsername = function(data) {
// if name exit again ask for a new name
// console.log("nameExists(data.name) " + nameExists(data.username));
if (nameExists(data.username)) {
this.emit('request login', { exists: true });
return;
}
// username is confirmed
this.name = data.username;
usernamesOccupied.push(data.username);
// create a new session for this user
this.sessionInit({
restore: false
});
// this.currentroom = new Room(this);
};
User.prototype.sessionInit = function(param) {
// if !restore create new
if (!param.restore) {
// add to global
Sessions[this.session.id] = this.name;
this.session.sockets = [];
this.session.username = this.name;
this._broadcast('joined', {
username: this.name
});
} else {
this.name = this.session.username;
}
// add the new socket to session sockets
this.session.sockets.push(this.id);
this.emit('username', {
'username': this.name,
'users_online': usernamesOccupied
});
this.sessionInitiated = true;
// update
this.session.save();
};
User.prototype.__init__ = function() {
this._socket.on('setUsername', (function(data) {
this.setUsername(data);
console.log(data);
}).bind(this));
this._socket.on('message', (function(data) {
console.log(this.name + " : " + data.message);
this._onMessageRecieved(data);
}).bind(this));
this._socket.on('disconnect', (function(data) {
this.udisconnect(data);
}).bind(this));
};
User.prototype.udisconnect = function(data) {
if(!this.sessionInitiated)
return;
for(var i = this.session.sockets.length-1; i > -1; i--){
if (this.session.sockets[i] === this.id){
this.session.sockets.splice(i, 1);
}
}
if(this.inNamespace != '/'){
// console.log("this.inNamespace " + this.inNamespace);
// console.log("142 :" + _.contains(Janta[this.inNamespace], this.id));
// console.log(Janta[this.inNamespace]);
if(_.contains(Janta[this.inNamespace], this.id)){
var pos = Janta[this.inNamespace].indexOf(this.id);
Janta[this.inNamespace].splice(pos, 1);
}
if(Janta[this.inNamespace].length == 0){
// no one is in this namespace
RoomNames = _.remove(RoomNames, this.inNamespace.split('/')[1]);
delete Janta[this.inNamespace];
}
}
if(this.session){this.session.save()};
console.log("disconnect " + this.id);
};
function nameExists(name) {
// _.some(connectionList, function(item) {return item.username == username})
return _.contains(usernamesOccupied, name);
};
module.exports = User;