-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (33 loc) · 1.12 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const PORT = process.env.PORT || 3000;
// index.htmlから読み込まれている静的ファイルを送れるようにしておく
app.use(express.static('src/'));
// GETされたらindex.htmlを送信する
app.get('/', function(req, res){
res.sendfile('src/index.html');
});
// クライアントからの接続を待つ
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
// クライアントからメッセージを受け取ったら投げ返す
socket.on('bingo', function(msg){
// 同じクライアントに送信する場合は socket.emit を io.emit に変える
console.log(msg);
io.emit('bingo', msg);
});
socket.on('reset', function(msg){
io.emit('reset', msg);
});
socket.on('null', function(msg){
io.emit('null', msg);
});
});
http.listen(PORT, function(){
console.log('listening on *:3000');
});