Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix event
Browse files Browse the repository at this point in the history
  • Loading branch information
koh110 committed May 5, 2020
1 parent 604b02b commit db5aad5
Showing 1 changed file with 61 additions and 57 deletions.
118 changes: 61 additions & 57 deletions src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,75 @@ if (cluster.isMaster) {
cluster.fork()
})
} else {
redis.on('ready', async () => {
const wss = new WebSocket.Server(
{
port: SOCKET_LISTEN
},
() => {
logger.info('Listening on', SOCKET_LISTEN)
}
)
redis
.on('ready', async () => {
const wss = new WebSocket.Server(
{
port: SOCKET_LISTEN
},
() => {
logger.info('Listening on', SOCKET_LISTEN)
}
)

consume()
consume()

wss.on('connection', async function connection(ws: ExtWebSocket, req) {
const user: string = req.headers['x-user-id'] as string
if (!user) {
ws.close()
return
}
const id = uuid()
ws.id = id
saveSocket(id, user, ws)
const twitterUserName = req.headers['x-twitter-user-name'] as string
wss.on('connection', async function connection(ws: ExtWebSocket, req) {
const user: string = req.headers['x-user-id'] as string
if (!user) {
ws.close()
return
}
const id = uuid()
ws.id = id
saveSocket(id, user, ws)
const twitterUserName = req.headers['x-twitter-user-name'] as string

const data: PostData = {
cmd: 'socket:connection',
payload: { user, twitterUserName }
}
requestSocketAPI(data, user, id)
.then((data) => {
if (data) {
ws.send(data)
}
})
.catch((e) => {
logger.error('[post:error]', e)
})

const data: PostData = {
cmd: 'socket:connection',
payload: { user, twitterUserName }
}
requestSocketAPI(data, user, id)
.then((data) => {
if (data) {
ws.send(data)
ws.on('message', async function incoming(message) {
if (message === 'pong') {
return
}
try {
const data = await requestSocketAPI(message, user, id)
if (data) {
ws.send(data)
}
} catch (e) {
logger.error('[post:error]', e)
}
})
.catch((e) => {
logger.error('[post:error]', e)
})

ws.on('message', async function incoming(message) {
if (message === 'pong') {
return
}
try {
const data = await requestSocketAPI(message, user, id)
if (data) {
ws.send(data)
}
} catch (e) {
logger.error('[post:error]', e)
}
})
ws.on('close', function close() {
logger.info('closed:', user, ws.id)
removeSocket(ws.id, user)
})

ws.on('close', function close() {
logger.info('closed:', user, ws.id)
removeSocket(ws.id, user)
ws.on('error', function error(e) {
logger.error('error: ', e)
})
})

ws.on('error', function error(e) {
logger.error('error: ', e)
})
setInterval(() => {
wss.clients.forEach((ws) => {
ws.send('ping')
})
}, 50000)
})
.on('error', (e) => {
logger.error(e)
})

setInterval(() => {
wss.clients.forEach((ws) => {
ws.send('ping')
})
}, 50000)
})
}

0 comments on commit db5aad5

Please sign in to comment.