-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (75 loc) · 1.92 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
// Setup basic express server
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = 4050
server.listen(port, function () {
console.log(`Server listening at port ${port}`);
});
// Routing
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// Chatroom
// 在线人数
var numUsers = 0;
// 监听客户端 socket 连接请求
io.on('connection', function (socket) {
console.log('a user connection');
// 判断当前用户是否已经登录过标志变量
var addedUser = false;
// 监听客户端发送信息请求
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
// 监听客户端的登录请求
socket.on('add user', function (username) {
console.log(`User: ${username} is login`);
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
// 登录回调
socket.emit('login', {
numUsers: numUsers
});
// 用户上线广播
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});
//
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
//
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
// 监听客户端断线
socket.on('disconnect', function () {
console.log(`User: ${socket.username} is logout`);
if (addedUser) {
--numUsers;
// 用户下线广播
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});
module.exports = app;