-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
49 lines (39 loc) · 1.17 KB
/
app.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const session = require('express-session');
const db = require('./db/setup');
const routes = require('./routes/index');
const WebSocket = require('ws');
const eventBus = require('./eventBus');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(routes);
const PORT = 3333;
const server = app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// Set up WebSocket server
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
console.log('New client connected');
ws.on('close', () => {
console.log('Client disconnected');
});
});
eventBus.on('newMessage', (message) => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(message));
}
});
});