-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
92 lines (81 loc) · 2.35 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
/** CONFIGURATION **/
HOST = "http://127.0.0.1/"
PORT = "8080"
/** CONFIGURATION **/
var express = require('express')
, app = express.createServer()
, io = require('socket.io').listen(app);
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.listen(PORT);
var DB = new Array();
app.get('/games/online/:nickname', function (req, res) {
var nick = req.params.nickname,roomid,
pageData = {
PORT: PORT,
HOST: HOST,
title: "联机对战",
nickname: nick
}
//查找是否有等待房间
if (!DB['waitingRoom'] || DB['waitingRoom'].length == 0) {
roomid = Math.random().toString(36).substring(12);
//添加等待房间
DB['waitingRoom'] = [roomid];
//添加玩家
DB[roomid] = { players: [nick], sockets: [] };
pageData.players = [nick];
}
else {
//获取等待房间
roomid = DB['waitingRoom'][DB['waitingRoom'].length-1];
//判断是否页面刷新
if (DB[roomid]) {
DB['waitingRoom'].pop();
//添加玩家
DB[roomid].players.push(nick);
pageData.players = DB[roomid].players;
}
else {
DB[roomid] = { players: [nick], sockets: [] };
pageData.players = [nick];
}
}
pageData.roomId = roomid;
//console.log(DB);
res.render('games/online', pageData);
});
io.sockets.on('connection', function(socket) {
//监听用户加入
socket.on('login', function(data) {
console.log(data.username + ' connected');
socket.name = data.username;
socket.roomid = data.roomid;
DB[data.roomid].sockets.push(this);
//console.log(DB[data.roomid].sockets);
});
//监听用户退出
socket.on('disconnect', function(){
//将信息发送给用户
if (DB[socket.roomid]) {
for (var i = 0; i < DB[socket.roomid].sockets.length; i++) {
DB[socket.roomid].sockets[i].emit('message', { type: 'disconnect'});
}
//删除DB
delete DB[socket.roomid];
}
});
//监听用户匹配
socket.on('match', function(data) {
//console.log(data);
for (var i = 0; i < DB[data.roomid].sockets.length; i++) {
DB[data.roomid].sockets[i].emit('match', data.players);
}
});
socket.on('message', function(data) {
console.log(data)
var s_socket = DB[data.roomid].sockets[0].name == data.player ? DB[data.roomid].sockets[1] : DB[data.roomid].sockets[0];
s_socket.emit('message', { type: data.type, player: data.player });
});
});