-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (65 loc) · 2.21 KB
/
server.js
File metadata and controls
80 lines (65 loc) · 2.21 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
/**
* WebSocket server for Blockly collaboration
* Based on the y-websocket package
*/
const WebSocket = require('ws')
const http = require('http')
const setupWSConnection = require('y-websocket/bin/utils').setupWSConnection
const port = process.env.PORT || 1234
const host = process.env.HOST || '0.0.0.0' // Use 0.0.0.0 for production deployments
// CORS headers for HTTP responses
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
// Create a server
const server = http.createServer((request, response) => {
// Add CORS headers
Object.keys(corsHeaders).forEach(key => {
response.setHeader(key, corsHeaders[key])
})
// Handle OPTIONS preflight requests
if (request.method === 'OPTIONS') {
response.writeHead(204)
response.end()
return
}
response.writeHead(200, { 'Content-Type': 'text/plain' })
response.end('Blockly Collaboration WebSocket Server Running\n')
})
// Create a websocket server
const wss = new WebSocket.Server({ server })
// Custom connection handler that extracts room from URL
wss.on('connection', (conn, req) => {
// Extract the room name from the URL path
const pathname = req.url || '/'
let roomName = pathname.slice(1) // Remove leading slash
// No need to strip prefixes as client is sending the full room ID directly
console.log(`New WebSocket connection for room: ${roomName}`)
// Set up the WebSocket connection with the extracted room name
setupWSConnection(conn, req, { roomName })
})
// Error handling
wss.on('error', (error) => {
console.error('WebSocket server error:', error)
})
// Start the server
server.listen(port, host, () => {
console.log(`Blockly WebSocket server running at http://${host}:${port}`)
console.log('WebSocket connections are accepted at ws://<host>:<port>/:roomName')
})
// Handle termination gracefully
process.on('SIGINT', () => {
console.log('Shutting down WebSocket server...')
wss.close(() => {
console.log('WebSocket server closed')
process.exit(0)
})
})
// For health checks
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down')
process.exit(0)
})