-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (71 loc) · 2.08 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
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
const express = require('express');
const app = express();
const server = app.listen(8080);
var io = require('socket.io').listen(server);
const bodyParser = require('body-parser');
const path = require('path');
app.use(express.static(path.join(__dirname, 'build')));
app.use(
bodyParser.urlencoded({
type: "image/*",
extended: false,
limit: "50mb"
})
);
app.use(
bodyParser.json({
type: "application/*",
limit: "50mb"
})
);
app.use(
bodyParser.text({
type: "text/plain"
})
);
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
/***** Socket ******/
io.on('connection', function (socket) {
console.log("連線建立,SOCKET ID: " + socket.id);
//連線到指定房間後,加入
socket.on('joinRoom', function (data) {
console.log("有人要加入" + data.roomName + "房間,SocketID是:" + socket.id);
socket.join(data.roomName);
socket.to(data.roomName).emit('newPlayerComing', { socketID: socket.id });
});
/** Send & Received StickyNotes **/
// Basic Data : RoomName, NoteID ,Content
//0. 房間j名稱設定
socket.on('roomOwnerSetTopic', function (data) {
console.log("遊戲開始,設定主題囉:" + data.topic);
let room = Object.keys(socket.rooms)[1];
socket.to(room).emit('showTopic', data.topic);
})
//0. Add Event (from Frond-End)
// socket.on('AddStickyNote', function (data) {
// //之後要有檢查機制
// let NoteID = '001';
// socket.emit('GiveYouStickyNoteID', NoteID);
// //存入資料庫
// });
//1. Send Event (from Frond-End)
//接受某人便條紙內容->廣播給該房間其他人->存入資料庫
/*
data= {
noteList : []
}
*/
socket.on('sendStickyNote', function (data) {
//之後要有檢查機制
console.log("接受到便條紙資料,傳給其他人囉:" + data);
let room = Object.keys(socket.rooms)[1];
let NoteDetail = {
socketID: socket.id,
noteList: data.noteList
}
socket.to(room).emit('receivedOtherStickyNote', NoteDetail);
//存入資料庫
});
});