-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·137 lines (115 loc) · 3.31 KB
/
app.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
var express = require('express');
var path = require('path');
var IO = require('socket.io');
var router = express.Router();
var app = express();
var server = require('http').Server(app);
app.use('/', express.static(path.join(__dirname, 'public')));
app.use('/ppt', express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// 创建socket服务
var io = IO(server, { path: '/pptSocket' });
var room = {};
// 房间用户名单
var defRoom = {
femaleCount: 0,
maleCount: 0,
female: {},
male: {},
list: []
}; //默认空值,用户清空赋值
io.on('connection', function (socket) {
var roomID = "hf";
if (!room[roomID]) {
room[roomID] = JSON.parse(JSON.stringify(defRoom));
}
var tmp = room[roomID];
var user = {};
socket.on('join', function (users) {
var sex = users.sex, id = users.id;
if (!sex) {
return;
}
if (sex && !id) {
tmp[sex + 'Count'] += 1;
var img = Math.floor(Math.random() * 10) + 1 + (sex == 'male' ? 0 : 10);
id = "000" + Math.floor(Math.random() * 1000);
id = id.slice(-5); id = id.replace('0', 'a');
user = {
sex: sex,
id: id,
img: img + '.jpg'
};
tmp.list.push(user);
tmp[sex][id] = Object.assign({}, user);
//给自己发消息
socket.emit('self', 'self', user);
// 广播向其他用户发消息
socket.broadcast.emit('sys', 'in', user);
console.log(user.id + '加入了' + roomID);
}
//数据在客户端有,但是在服务端没有的情况
if (!tmp[sex][id]) {
tmp[sex + 'Count'] += 1;
tmp.list.push(users);
tmp[sex][id] = Object.assign({}, users);
console.log(users.id + '回来了' + roomID);
}
socket.join(roomID); // 加入房间
// 通知房间内人员
// io.to(roomID).emit('sys', 'in', user);
});
//管理员各种操作,切换场景等
socket.on('option', function (data) {
if (data.option == 'clean') {
room[roomID] = JSON.parse(JSON.stringify(defRoom));
}
// 广播向其他用户发消息
socket.broadcast.emit('option', data);
});
//客户端获取所有数据
socket.on('getAll', function () {
console.log(tmp.list);
// 广播向其他用户发消息
socket.emit('getAll', tmp.list);
});
socket.on('leave', function () {
socket.emit('disconnect');
});
socket.on('disconnect', function () {
// 从房间名单中移除
if (user && tmp[user.sex] && tmp[user.sex][user.id]) {
delete tmp[user.sex][user.id];
tmp[user.sex + 'Count'] -= 1;
io.to(roomID).emit('sys', 'out', user);
console.log(user.id + '退出了' + roomID);
}
socket.leave(roomID); // 退出房间
});
// 接收用户消息,发送相应的房间
socket.on('message', function (users, msg) {
// 验证如果用户不在房间内则不给发送
if (!users || !users.id || !tmp[users.sex][users.id]) {
return false;
}
// 广播向其他用户发消息
socket.broadcast.emit('msg', users, msg);
socket.emit('msg', users, msg);
//向房间发消息
// io.to(roomID).emit('msg', users, msg);
});
});
// room page
router.get('/room/:roomID', function (req, res) {
var roomID = req.params.roomID;
// 渲染页面数据(见views/room.hbs)
res.render('room', {
roomID: roomID,
users: room[roomID]
});
});
app.use('/', router);
server.listen(3000, function () {
console.log('server listening on port 3000');
});