-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
46 lines (42 loc) · 1.56 KB
/
socket.js
File metadata and controls
46 lines (42 loc) · 1.56 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
const SocketIO = require('socket.io')
module.exports = (server) =>{
const io = SocketIO(server, {path: '/socket.io'})
io.on('connection', (socket) => {
const req = socket.request;
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log('새로운 클라이언트 접속!', ip, socket.id, req.ip)
socket.on('disconnect', ()=>{
console.log('클라이언트 접속 해제', ip, socket.id)
clearInterval(socket.interval)
})
socket.on('error', (error) =>{
console.error(error)
})
socket.on('reply',(data)=>{
console.log(data)
})
socket.interval = setInterval(()=>{
socket.emit('news','Hello Socket.IO')
})
})
// const wss = new webSocket.Server({server})
// wss.on('connection', (ws, req) => {
// const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
// console.log('새로운 클라이언트 접속', ip);
// ws.on('message', (message)=>{
// console.log(message)
// })
// ws.on('errpr', (error)=>{
// console.log(error)
// })
// ws.on('close', ()=>{
// console.log('클라이언트 접속 해제', ip)
// clearInterval(ws.interval)
// })
// ws.interval = setInterval(() =>{
// if(ws.readyState === ws.OPEN){
// ws.send('서버에서 클라이언트로 메시지를 보냅니다.')
// }
// },3000)
// })
}