-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (89 loc) · 3.01 KB
/
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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const path = require('path');
const fs = require('fs');
const express = require('express');
var messages = {};
const cpen322 = require('./cpen322-tester.js');
const WebSocket = require('ws');
var broker = new WebSocket.Server({port: 8000});
broker.on('connection', (client) => {
console.log('New client connected.');
client.on('message', (message) => {
const parsedMessage = JSON.parse(message);
console.log("NOW PRINTING PARSED MESSAGE: ", parsedMessage);
const roomId = parsedMessage.roomId;
messages[roomId].push(parsedMessage);
// Iterate through the broker.clients set and forward the message
broker.clients.forEach((otherClient) => {
if (otherClient !== client && otherClient.readyState === WebSocket.OPEN) {
otherClient.send(JSON.stringify(parsedMessage));
}
});
// Assuming `messages` is an object storing arrays of messages for each room
});
client.on('close', () => {
console.log('Client disconnected.');
});
});
function logRequest(req, res, next){
console.log(`${new Date()} ${req.ip} : ${req.method} ${req.path}`);
next();
}
const host = 'localhost';
const port = 3000;
const clientApp = path.join(__dirname, 'client');
// express app
let app = express();
app.use(express.json()) // to parse application/json
app.use(express.urlencoded({ extended: true })) // to parse application/x-www-form-urlencoded
app.use(logRequest); // logging for debug
// serve static files (client-side)
app.use('/', express.static(clientApp, { extensions: ['html'] }));
app.listen(port, () => {
console.log(`${new Date()} App Started. Listening on ${host}:${port}, serving ${clientApp}`);
});
let chatrooms = [
{id: "101", name: "Jack", image: "img1.jpg"}, //arbitrary
{id: "102", name: "Daniel", image: "img2.jpg"} //arbitrary
];
// var roomCounter = 0;
function generateUniqueId() {
return 'room-' + Date.now() + '-' + Math.floor(Math.random() * 1000);
}
chatrooms.forEach(room => {
console.log("ROOM ID IS: ", room.id);
messages[room.id] = [];
});
app.route('/chat')
.get((req, res, next) => {
console.log("GET RECEIVED");
let chatData = chatrooms.map(room => {
return {
id: room.id,
name: room.name,
image: room.image,
messages: messages[room.id]
};
});
res.json(chatData);
})
.post((req, res) => {
const data = req.body;
if(!('name' in data)){
// res.
return res.status(400).json({ error: 'Name is Missing' });
}
else{
// var id = (++roomCounter);
var id = generateUniqueId();
var name = data['name'];
var image = data['image'];
var messagesToAdd = [];
var room = {id: id, name: name, image: image, messages: messagesToAdd};
messages[id] = messagesToAdd;
chatrooms.push(room);
return res.status(200).json(room);
}
});
cpen322.connect('http://3.98.223.41/cpen322/test-a3-server.js');
cpen322.export(__filename, { app });
cpen322.export(__filename, { chatrooms, messages, broker});