add http listener to websocket-server

This commit is contained in:
Kevin Jahns
2018-11-26 13:08:23 +01:00
parent 539fa8b21d
commit 12d43199d5
2 changed files with 18 additions and 2 deletions

View File

@@ -4,10 +4,17 @@
import * as Y from '../../index.mjs'
import WebSocket from 'ws'
import http from 'http'
const port = process.env.PORT || 1234
const wss = new WebSocket.Server({ port })
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('okay')
})
const wss = new WebSocket.Server({ noServer: true })
const docs = new Map()
const messageSync = 0
@@ -107,4 +114,13 @@ const setupConnection = (conn, req) => {
wss.on('connection', setupConnection)
server.on('upgrade', (request, socket, head) => {
// You may check auth of request here..
wss.handleUpgrade(request, socket, head, ws => {
wss.emit('connection', ws, request)
})
})
server.listen(port)
console.log('running on port', port)