-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (45 loc) · 1.29 KB
/
server.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
var express=require('express');
var connection=require('socket.io');
var app=express();
var user=[];//用于存储用户名列表
// app.use("/",express.static(__dirname+"www"));
// app.use(express.static('public'));
app.use(express.static('assets'));
app.get('/',function(req,res){
res.sendFile(__dirname+"/"+"index.html");
})
var server =app.listen(8081,function(){
var host=server.address().address
var port=server.address().port
// console.log("应用实例,访问地址为http://%s:%s",host,port);
})
var io=connection.listen(server);
io.on('connection',function(socket){
// socket.on('foo',function(data){
// console.log(data);
// });
//新用户进入
socket.on('enterRoom',function(username){
if(user.indexOf(username)>-1){
socket.emit("userExisted");
}else if(username=="me"){
socket.emit("unvalidusername");
}else{
socket.userIndex=user.length;
socket.username=username;
user.push(username);
socket.emit("enter");
io.sockets.emit('system',username,user,'enter');
}
});
socket.on('sendMsg',function(content){
socket.broadcast.emit("newMsg",socket.username,content);
});
//用户离开
socket.on('disconnect',function(){
if(socket.username){
user.splice(socket.userIndex,1);
socket.broadcast.emit("system",socket.username,user,'left');
}
})
})