forked from yjs/yjs-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-server.js
26 lines (19 loc) · 836 Bytes
/
demo-server.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
/**
* @type {any}
*/
const WebSocket = require('ws')
const http = require('http')
const StaticServer = require('node-static').Server
const setupWSConnection = require('y-websocket/bin/utils.js').setupWSConnection
const production = process.env.PRODUCTION != null
const port = process.env.PORT || 8080
const staticServer = new StaticServer('.', { cache: production ? 3600 : false, gzip: production })
const server = http.createServer((request, response) => {
request.addListener('end', () => {
staticServer.serve(request, response)
}).resume()
})
const wss = new WebSocket.Server({ server })
wss.on('connection', (conn, req) => setupWSConnection(conn, req, { gc: req.url.slice(1) !== 'prosemirror-versions' }))
server.listen(port)
console.log(`Listening to http://localhost:${port} ${production ? '(production)' : ''}`)