-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (40 loc) · 1.14 KB
/
index.js
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
const WebSocket = require('ws')
const { createLogger, format, transports } = require('winston')
const logger = createLogger({
level: 'info',
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.errors({ stack: true }),
format.splat(),
format.json()
),
defaultMeta: { service: 'ws-instru' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`.
// - Write all logs error (and below) to `error.log`.
//
/* new transports.File({ filename: 'instru-error.log', level: 'error' }),
new transports.File({ filename: 'instru-combined.log' }) */
new transports.Console({
format: format.combine(
format.colorize(),
format.simple()
)
})
]
})
const wss = new WebSocket.Server({ port: 9000, path: '/instru' })
wss.on('connection', (ws) => {
ws.on('message', (msgString) => {
const message = JSON.parse(msgString)
if (message.type === 'error') {
logger.log('error', message.content)
} else {
logger.log('info', JSON.stringify(message.content))
}
})
ws.send('[WS] connection open')
})