質問編集履歴
1
サーバー側の処理を追加。
test
CHANGED
File without changes
|
test
CHANGED
@@ -107,3 +107,33 @@
|
|
107
107
|
</body>
|
108
108
|
</html>
|
109
109
|
|
110
|
+
|
111
|
+
サーバー側の処理です。
|
112
|
+
|
113
|
+
const express = require('express');
|
114
|
+
const http = require('http');
|
115
|
+
const socketIo = require('socket.io');
|
116
|
+
|
117
|
+
const app = express();
|
118
|
+
const server = http.Server(app);
|
119
|
+
const io = socketIo(server);
|
120
|
+
|
121
|
+
const PORT = 3000;
|
122
|
+
|
123
|
+
app.get('/', (req, res) => {
|
124
|
+
res.sendFile(__dirname + '/index.html');
|
125
|
+
});
|
126
|
+
|
127
|
+
server.listen(PORT, () => {
|
128
|
+
console.log(`listening on port ${PORT}`);
|
129
|
+
});
|
130
|
+
|
131
|
+
io.on('connection', (socket) => {
|
132
|
+
console.log('user connected');
|
133
|
+
socket.on('sendMessage', (message) => {
|
134
|
+
console.log('Message has been sent: ', message);
|
135
|
+
|
136
|
+
// 'receiveMessage' というイベントを発火、受信したメッセージを全てのクライアントに対して送信する
|
137
|
+
io.emit('receiveMessage', message);
|
138
|
+
});
|
139
|
+
});
|