-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (84 loc) · 2.59 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
var express = require("express");
var hbs = require("hbs");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var qrcode = require("qrcode-npm");
var uuid = require('node-uuid');
var port = 2218;
http.listen(port, function () {
console.log("http://localhost:" + port);
});
app.use("/static", express.static(__dirname + "/public"));
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.set('views', __dirname + '/views');
app.get("/", function (req, res) {
res.render("a");
});
app.get("/playb/:id", function (req, res) {
if (req.params.id) {
console.log(req.params.id);
res.render("b", {
data: req.params.id
});
}
});
var onlineRooms = {};
var roomid = null;
var count = 0;
io.sockets.on("connection", function (socket) {
console.log("已连接");
socket.on("registerRoom", function (data) {
console.log(data);
roomid = data;
if (!onlineRooms.hasOwnProperty(data)) {
var room = {
id: data,
user: []
};
socket.emit("registerSuccess", room.id);
onlineRooms[data] = room;
console.log(onlineRooms);
socket.join(data);
} else {
socket.emit("roomExist");
}
});
socket.on("playbJoin", function (data) {
if (roomid) {
if (onlineRooms[roomid].user.length < 1) {
if (data == roomid) {
count++;
var user = {
uid: roomid + count
};
onlineRooms[roomid].user.push(user);
socket.join(data);
io.sockets.to(roomid).emit("playbJoin");
socket.on("playbClickJoin", function () {
console.log("用户B点击了join按钮");
io.sockets.to(data).emit("playbClickJoin");
});
}
} else {
// io.sockets.to(roomid).emit('phoneEnough');
}
} else {
console.log("请先注册房间");
}
});
socket.on("playaClickStart", function () {
console.log("用户A点击了start按钮");
io.sockets.to(roomid).emit("playaClickStart");
});
socket.on("playaBarEnd",function(){
io.sockets.to(roomid).emit("playbBarStart");
});
socket.on("playbBarEnd",function(){
io.sockets.to(roomid).emit("play");
});
socket.on("disconnect", function () {
console.log("断开连接");
});
});