forked from cs4241-20a/final-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (25 loc) · 836 Bytes
/
server.js
File metadata and controls
34 lines (25 loc) · 836 Bytes
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
const WebSocket = require('ws')
const http = require('http')
const express = require( 'express' ),
app = express()
const port = process.env.PORT || 3000
const server = http.createServer(app)
app.use( express.static( 'build' ) )
app.get( '/', function (req, res) {
res.sendFile(__dirname + '/build/index.html')
})
const wss = new WebSocket.Server({ noServer:true })
const setupWSConnection = require('./node_modules/y-websocket/bin/utils.js').setupWSConnection
wss.on('connection', setupWSConnection)
server.on('upgrade', (request, socket, head) => {
// You may check auth of request here..
/**
* @param {any} ws
*/
const handleAuth = ws => {
wss.emit('connection', ws, request)
}
wss.handleUpgrade(request, socket, head, handleAuth)
})
server.listen(port)
console.log('running on port', port)