Skip to content

Commit

Permalink
Add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsandow committed Jul 25, 2022
1 parent e2c27f6 commit 7fe5bef
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ module.exports = {
'no-shadow': 'off',
'@typescript-eslint/no-shadow': ['error'],
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-explicit-any': [1, { ignoreRestArgs: true }],
},
}
2 changes: 1 addition & 1 deletion client/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const uri = process.env?.SERVER_URI || 'http://localhost:3000'
export const username = process.env?.RPG_FOR_BOTS_USERNAME || 'foobar'
export const password = process.env?.RPG_FOR_BOTS_PASSWORD || 'foobar'
export const password = process.env?.RPG_FOR_BOTS_PASSWORD || 'foobar2'
1 change: 1 addition & 0 deletions client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io(uri, {
const start = Date.now()

socket.emit('ping', () => {
// eslint-disable-next-line no-console
console.log(`Ping: ${Date.now() - start}ms`)

socket.close()
Expand Down
16 changes: 8 additions & 8 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

import { port } from './env'
import { parseToken, validateCredentials } from './aurh'
import logger from './logger'

const server = new Server<
ClientToServerEvents,
Expand All @@ -21,8 +22,9 @@ server.use((socket, next) => {
const credentials = parseToken(socket.handshake.auth?.token || '')

if (!validateCredentials(credentials)) {
console.debug(
`[${socket.id}] Invalid credentials for user "${credentials.username}" from ${socket.handshake.address}`,
logger.debug(
socket,
`Invalid credentials for user "${credentials.username}" from ${socket.handshake.address}`,
)
next(new Error('Invalid credentials'))
return
Expand All @@ -33,20 +35,18 @@ server.use((socket, next) => {
})

server.on('connection', (socket) => {
console.debug(
`[${socket.id}] New connection from ${socket.handshake.address}`,
)
logger.debug(socket, `New connection from ${socket.handshake.address}`)

if (socket.data.username) {
console.log(`[${socket.id}] User "${socket.data.username}" came online`)
logger.log(socket, `User came online`)
}

socket.on('disconnect', () => {
if (socket.data.username) {
console.log(`[${socket.id}] User "${socket.data.username}" went offline`)
logger.log(socket, `User went offline`)
}

console.debug(`[${socket.id}] Connection closed`)
logger.debug(socket, `Connection closed`)
})

socket.on('ping', (callback) => {
Expand Down
70 changes: 70 additions & 0 deletions server/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Socket } from 'socket.io'

import {
ClientToServerEvents,
InterServerEvents,
ServerToClientEvents,
SocketData,
} from '../shared/socket'

function combineWithSocketData(
socket: Socket<
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>,
...data: any[]
): // eslint-disable-next-line @typescript-eslint/no-explicit-any
any[] {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output: any[] = Array.from(data)

if (socket.data.username) {
output.unshift(`[${socket.data.username}]`)
}

output.unshift(`[${socket.id}]`)
// output.unshift(`[${socket.handshake.address}]`)

return output
}

export default {
log: (
socket: Socket<
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>,
...data: any[]
): void => {
// eslint-disable-next-line no-console
console.log(...combineWithSocketData(socket, ...data))
},
debug: (
socket: Socket<
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>,
...data: any[]
): void => {
// eslint-disable-next-line no-console
console.debug(...combineWithSocketData(socket, ...data))
},
error: (
socket: Socket<
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>,
...data: any[]
): void => {
// eslint-disable-next-line no-console
console.error(...combineWithSocketData(socket, ...data))
},
}

0 comments on commit 7fe5bef

Please sign in to comment.