-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (75 loc) · 2.81 KB
/
app.js
File metadata and controls
98 lines (75 loc) · 2.81 KB
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
93
94
95
96
97
98
const express = require('express');
const app = express();
const path = require('path');
const http = require('http');
const socketIo = require('socket.io');
const db = require('./config/keys').mongoURI;
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const users = require('./routes/api/users');
const games = require('./routes/api/games');
const boards = require('./routes/api/boards');
const pieces = require('./routes/api/pieces');
const boardController = require('./controllers/boards_controller');
const server = http.createServer(app);
const io = socketIo(server);
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log('Connected to MongoDB successfully'))
.catch((err) => console.log(err));
app.use(passport.initialize());
require('./config/passport')(passport);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api/users', users);
app.use('/api/games', games);
app.use('/api/boards', boards);
app.use('/api/pieces', pieces);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('frontend/build'));
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
// setting the socket up serverside
const nsp = io.of('/gamesNamespace');
nsp.on('connection', (socket) => {
socket.on('joinRoom', (room) => {
socket.join(room.roomId);
// console.log(`joined ${room.roomId}`);
});
// exppected input format: { id: "some_id_string_here"}
socket.on('deleteBoard', (board) => {
boardController.deleteBoard(board);
});
// expected input format: send board
socket.on('updateBoard', (board) => {
boardController.updateBoard(board);
});
// expected input format: { boardId: "some_id_string_here", pos: { x: num, y: num},
// size: num (optional), pieceId: "some_id string", imageUrl: "string", playerId:
// "playerId" }
socket.on('createToken', (token) => {
boardController.createToken(token);
});
// expected input format: { boardId: "string", tokenId: "string", pos: {x: num, y: "num" } }
socket.on('updateToken', (token) => {
boardController.updateToken(token);
});
// expected input format { boardId: "string", tokenId: "string"}
socket.on('deleteToken', (token) => {
boardController.deleteToken(token);
});
// receive a message and transmit it back to subscribers
socket.on('message', (message) => {
nsp.to(`${message.room}`).emit('message', message);
});
});
exports.transmitData = function (room, actionName, action) {
// console.log(room, actionName, action)
return nsp.to(room).emit(actionName, action);
// return nsp.to(room).emit(actionName, action)
};
server.listen(port, () => console.log(`Listening on port ${port}`));