Skip to content

Commit

Permalink
[#187] feat - socket.ts 수정
Browse files Browse the repository at this point in the history
- Socket 로직 리팩토링
  • Loading branch information
hschoi1104 committed May 4, 2021
1 parent 2bacbe1 commit aee850e
Showing 1 changed file with 122 additions and 37 deletions.
159 changes: 122 additions & 37 deletions backend/src/utils/socket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import express from 'express'
import http from 'http'
import sio from 'socket.io'
import { AttendanceDao } from './../dao/attendance.dao'
import moment from 'moment'
import User from './../models/User'

//socket io to Attendance
export const io = undefined
Expand All @@ -9,53 +12,135 @@ export async function initSocket(
app: express.Express,
SOCKET_PORT: string
): Promise<void> {
const server = http.createServer(app)
const io = sio(server, { origins: '*:*' })

//Attendance State
const curState = {
const att = {
flag: false,
time: 180000,
starter: '',
time: 0,
code: -404,
list: new Set(),
}
//connect event
const server = http.createServer(app)
const io = sio(server, { origins: '*:*' })
let timerID
io.on('connection', function(socket) {
socket.on('join', function(data) {
socket.on('join', async function(data) {
socket.join(data.roomName)
io.to(socket.id).emit('attendance', curState)

io.to(socket.id).emit('init', {
flag: att.flag,
starter: att.starter,
time: att.time,
result: att.list.has(data.user),
})
io.to(socket.id).emit('health', true)
if (att.flag == true && att.starter == data.user) {
io.to(socket.id).emit('admin', {
code: att.code,
})
}
})
//attendance event lisner
socket.on('attendance', function(data) {
curState.flag = data.flag
if (!data.flag) curState.time = 18000
const msg = {
flag: data.flag,
time: curState.time,
socket.on('check', async function(data) {
if (att.code == data.code) {
if (att.list.has(data.user) == true)
io.to(socket.id).emit('result', 'already')
else {
att.list.add(data.user)
const user = await User.findOne({ username: data.user })
AttendanceDao.createAttendance(
data.user,
user!.info.realname,
'attendance',
moment().format('YYYY-MM-DD')
)
io.to(socket.id).emit('result', 'success')
}
} else {
if (data.code == -1) {
if (att.list.has(data.user) == true)
io.to(socket.id).emit('result', 'already')
else io.to(socket.id).emit('result', 'prepare')
} else io.to(socket.id).emit('result', 'fail')
}
//broadcast changed state
// socket.broadcast.to('attendance').emit('attendance', msg)
io.to('attendance').emit('attendance', msg)
})
//setTimeout 3m when attendance start

socket.on('start', function() {
curState.time = 180000
const timerID = setInterval(function() {
if (curState.flag == false) {
clearInterval(timerID)
curState.time = 180000
}
curState.time -= 1000
if (curState.time == 0) {
clearInterval(timerID)
if (curState.flag == true) {
const msg = {
socket.on('start', async function(data) {
try {
const userlist = await AttendanceDao.getAttendanceByDate(
moment().format('YYYY-MM-DD')
)

const { code, starter } = data
att.time = 180000
att.code = code
att.flag = true
att.starter = starter
att.list = new Set()

userlist.forEach(user => {
if (user.state == 'attendance') att.list.add(user.username)
})

const user = await User.findOne({ username: starter })
AttendanceDao.createAttendance(
starter,
user!.info.realname,
'attendance',
moment().format('YYYY-MM-DD')
)

io.to('attendance').emit('state', {
flag: true,
starter,
})

timerID = setInterval(async function() {
att.time -= 1000
if (att.time <= 0) {
att.time = 0
att.code = -404
att.flag = false
att.starter = ''
att.list = new Set()

io.to('attendance').emit('state', {
flag: false,
}
curState.flag = false
io.to('attendance').emit('attendance', msg)
starter: null,
})
clearInterval(timerID)
}
}
}, 1000)
}, 1000)
} catch (err) {
console.log(err)
}
})

socket.on('stop', async function() {
try {
clearInterval(timerID)
io.to('attendance').emit('state', {
flag: false,
starter: null,
})

const userlist = await User.find({ attable: true })
userlist.forEach(user => {
if (att.list.has(user.username) == false) {
AttendanceDao.createAttendance(
user.username,
user.info.realname,
'absence',
moment().format('YYYY-MM-DD')
)
}
})
att.time = 0
att.code = -404
att.flag = false
att.starter = ''
att.list = new Set()
} catch (err) {
console.log(err)
}
})
})
//start socket.io server
Expand Down

0 comments on commit aee850e

Please sign in to comment.