-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (40 loc) · 1.19 KB
/
index.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
const { createServer } = require("http");
const express = require('express');
const { Server } = require("socket.io");
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
// options
});
// msgs
const AskJoin = 'ASK_JOIN';
const AcceptJoin = 'ACCEPT_JOIN';
const ChangePaddleDirection = 'CHANGE_PADDLE_DIRECTION';
const ChangeBallDirection = 'CHANGE_BALL_DIRECTION';
io.on("connection", (socket) => {
console.log('we are connected')
socket.on(ChangeBallDirection, (data) => {
console.log('ballMove message: ' + data);
io.emit(ChangeBallDirection, data);
});
socket.on(AskJoin, (data) => {
console.log('waiting message: ' + data);
io.emit(AskJoin, data);
});
socket.on(AcceptJoin, (data) => {
console.log('join message: ' + data);
io.emit(AcceptJoin, data);
});
socket.on(ChangePaddleDirection, (data) => {
console.log('change paddle message: ' + data);
io.emit(ChangePaddleDirection, data);
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
});
app.use(express.static('public'))
httpServer.listen(3000);