make this a proper esm module
This commit is contained in:
36
bin/server.cjs
Executable file
36
bin/server.cjs
Executable file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* @type {any}
|
||||
*/
|
||||
const WebSocket = require('ws')
|
||||
const http = require('http')
|
||||
const number = require('lib0/number')
|
||||
const wss = new WebSocket.Server({ noServer: true })
|
||||
const setupWSConnection = require('./utils.cjs').setupWSConnection
|
||||
|
||||
const host = process.env.HOST || 'localhost'
|
||||
const port = number.parseInt(process.env.PORT || '1234')
|
||||
|
||||
const server = http.createServer((_request, response) => {
|
||||
response.writeHead(200, { 'Content-Type': 'text/plain' })
|
||||
response.end('okay')
|
||||
})
|
||||
|
||||
wss.on('connection', setupWSConnection)
|
||||
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
// You may check auth of request here..
|
||||
// See https://github.com/websockets/ws#client-authentication
|
||||
/**
|
||||
* @param {any} ws
|
||||
*/
|
||||
const handleAuth = ws => {
|
||||
wss.emit('connection', ws, request)
|
||||
}
|
||||
wss.handleUpgrade(request, socket, head, handleAuth)
|
||||
})
|
||||
|
||||
server.listen(port, host, () => {
|
||||
console.log(`running at '${host}' on port ${port}`)
|
||||
})
|
||||
Reference in New Issue
Block a user