-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketHelper.js
172 lines (141 loc) · 4.3 KB
/
socketHelper.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
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
169
170
171
172
var Record = require('./models/record');
var User = require('./models/user');
var persons = {};
var log = function() {
console.log.apply(console, arguments);
};
log.color = function() {
var args = [].slice.call(arguments);
args.unshift('\033[35m');
args.push('\033[39m');
console.log.apply(console, args);
};
var joinRoom = function(socket, messageId) {
var rooms = socket.rooms;
messageId = String(messageId);
if(!rooms[messageId]) {
socket.join(messageId);
}
};
var searchFriend = function(userId) {
return persons[userId];
};
var onMessage = function(data, messageId, io) {
var socket = this;
var promise;
if(true) {
promise = Record.activateRecord(messageId);
promise = promise.then(function() {
return Record.createMessage(messageId, data);
});
}else {
promise = Record.createMessage(messageId, data);
}
// var promise = Record.createMessage(messageId, data);
promise.then(function(messageId) {
var resp = {
code: 0,
data: {
messageId: messageId,
attr: data
}
};
var friSocket = searchFriend(data.receiver);
if(friSocket) {
friSocket.emit('message', resp);
}else {
Record.setMsgUnRead(data.receiver, data.sender);
}
socket.emit('message', resp);
}).catch(function(err) {
log.color(err);
socket.emit('error', {
code: -1,
message: err.message
});
});
};
var onRecord = function(data, io) {
var socket = this;
var author = data.sender,
contacter = data.receiver;
var promise = new User(author).getRecords(contacter);
promise.then(function(records) {
if(records.length>0) {
var record = records[0];
var messageId = record.messageId;
var p = Record.createMessage(messageId, data);
return p;
}else {
var p = Record.createRecord(author, contacter);
p = p.then(function(doc) {
var messageId = doc.messageId;
var p = Record.createMessage(messageId, data);
return p;
});
return p;
}
}).then(function(messageId) {
var resp = {
code: 0,
data: {
messageId: messageId,
attr: data
}
};
var friSocket = searchFriend(data.receiver);
if(friSocket) {
friSocket.emit('record', resp);
if(false) {
// 没有进入特定的房间
Record.setMsgUnRead(data.receiver, data.sender);
}
}else {
Record.setMsgUnRead(data.receiver, data.sender); // 不需要依赖或等待
}
socket.emit('record', resp);
}).catch(function(err) {
socket.emit('error', {
code: -1,
message: err.message
});
});
};
var onEnterRoom = function(data, io) {
Record.setMsgRead(data.author, data.relate);
};
var onLeaveRoom = function(data, io) {
// Record.setMsgUnRead(data.author, data.relate);
};
// 使用devtool运行程序会有问题,有很多套接字链接。好像是devtool也使用了socket一样!!!
exports.onConnection = function(socket) {
var io = this;
console.log('a user connected!');
socket.on('login', function(data) {
console.log('a user login!');
socket.userId = data.userId;
persons[data.userId] = socket;
});
socket.on('enter', function(data) {
console.log('user enter: ', data.author, data.relate);
onEnterRoom.call(socket, data, io);
});
socket.on('leave', function(data) {
console.log('user leave: ', data.author, data.relate);
onLeaveRoom.call(socket, data, io);
});
socket.on('message', function(data, messageId) {
onMessage.call(socket, data, messageId, io);
});
socket.on('record', function(data) {
onRecord.call(socket, data, io);
});
socket.on('logout', function() {
console.log('a user logout!');
persons[socket.userId] = null;
});
socket.on('disconnect', function() {
console.log('a user disconnect!');
persons[socket.userId] = null;
});
};