|
@@ -2,6 +2,8 @@ const http = require('http')
|
|
|
const express = require('express')
|
|
const express = require('express')
|
|
|
const morgan = require('morgan')
|
|
const morgan = require('morgan')
|
|
|
const bodyParser = require('body-parser')
|
|
const bodyParser = require('body-parser')
|
|
|
|
|
+const WebSocket = require('ws')
|
|
|
|
|
+const EventEmitter = require('events')
|
|
|
const chalk = require('chalk')
|
|
const chalk = require('chalk')
|
|
|
const config = require('../config')
|
|
const config = require('../config')
|
|
|
|
|
|
|
@@ -11,6 +13,7 @@ app.use(morgan('combined'))
|
|
|
app.use(bodyParser.json())
|
|
app.use(bodyParser.json())
|
|
|
app.use(express.static('dist'))
|
|
app.use(express.static('dist'))
|
|
|
|
|
|
|
|
|
|
+app.ws = new EventEmitter()
|
|
|
app.listen = (port = config.port || (app.settings.env === 'production' ? 80 : 3000)) => new Promise((resolve, reject) => {
|
|
app.listen = (port = config.port || (app.settings.env === 'production' ? 80 : 3000)) => new Promise((resolve, reject) => {
|
|
|
app.server = http.createServer(app)
|
|
app.server = http.createServer(app)
|
|
|
app.server.once('error', (err) => {
|
|
app.server.once('error', (err) => {
|
|
@@ -22,7 +25,14 @@ app.listen = (port = config.port || (app.settings.env === 'production' ? 80 : 30
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
app.server.listen(port, () => {
|
|
app.server.listen(port, () => {
|
|
|
- console.log(`Server running at ${chalk.underline(chalk.blueBright(`http://localhost:${app.server.address().port}`))}`)
|
|
|
|
|
|
|
+ app.wss = new WebSocket.Server({ server: app.server })
|
|
|
|
|
+ app.wss.on('connection', (...args) => app.ws.emit('connection', ...args))
|
|
|
|
|
+ app.wss.on('message', (...args) => app.ws.emit('message', ...args))
|
|
|
|
|
+ app.wss.on('close', (...args) => app.ws.emit('close', ...args))
|
|
|
|
|
+ app.wss.on('error', (...args) => app.ws.emit('error', ...args))
|
|
|
|
|
+ app.server.port = app.server.address().port
|
|
|
|
|
+ console.log(`Server running at ${chalk.underline(chalk.blueBright(`http://localhost:${app.server.port}`))}`)
|
|
|
|
|
+ resolve()
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|