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
| // サーバー
const io = require('socket.io')(server, {
cors: {
origin: 'https://myapp.com',
methods: ['GET', 'POST']
},
transports: ['websocket', 'polling']
});
io.on('connection', (socket) => {
console.log('Connected:', socket.id);
socket.on('disconnect', (reason) => {
console.log('Disconnected:', reason);
});
});
// クライアント
import { io } from 'socket.io-client';
const socket = io('wss://example.com', {
transports: ['websocket'],
reconnection: true,
reconnectionAttempts: 5,
reconnectionDelay: 1000
});
|