Skip to content
Open

added #178

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
35 changes: 19 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
115 changes: 114 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,114 @@
'use strict';
import express from 'express';
import cors from 'cors';
import { WebSocketServer } from 'ws';

const app = express();
app.use(cors());
app.use(express.json());

const PORT = process.env.PORT || 3000;
const rooms = { general: [] }; // одразу створюємо general

function ensureRoom(room) {
if (!rooms[room]) rooms[room] = [];
}

function broadcast(room, data) {
wss.clients.forEach((client) => {
if (client.room === room && client.readyState === 1) {
client.send(JSON.stringify(data));
}
});
}

app.get('/messages', (req, res) => {
const room = req.query.room || 'general';
res.send(rooms[room] || []);
});

app.post('/messages', (req, res) => {
const { text, author, room = 'general' } = req.body;

ensureRoom(room);

const message = { text, author, time: new Date(), room };
rooms[room].push(message);

broadcast(room, { type: 'message', ...message });

res.status(201).send(message);
});

app.get('/rooms', (req, res) => {
res.send(Object.keys(rooms));
});

app.post('/rooms', (req, res) => {
const { name } = req.body;
ensureRoom(name);
res.status(201).send({ name });
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, you need to implement the ability to 'create / rename / join / delete' rooms. The functionality to rename a room is missing. You should add an endpoint here, for example, PUT /rooms/:name, to handle renaming.

app.delete('/rooms/:name', (req, res) => {
const { name } = req.params;

if (!rooms[name]) {
return res.status(404).send({ error: 'Room not found' });
}

delete rooms[name];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a room is deleted, what happens to the users currently in it? Their client.room property will refer to a room that no longer exists. Consider notifying these clients or moving them to a default room.


wss.clients.forEach((client) => {
if (client.room === name) {
client.room = 'general';
client.send(
JSON.stringify({
type: 'room_deleted',
redirectTo: 'general',
}),
);
}
});

res.send({ deleted: name });
});


const server = app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});

const wss = new WebSocketServer({ server });

wss.on('connection', (socket) => {
socket.room = 'general';

socket.on('message', (data) => {
const msg = JSON.parse(data);

if (msg.type === 'join') {

Check failure on line 89 in src/index.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected console statement
ensureRoom(msg.room);
socket.room = msg.room;

socket.send(
JSON.stringify({
type: 'history',
messages: rooms[msg.room],
}),
);
return;
}

if (msg.type === 'message') {
const message = {
text: msg.text,
author: msg.author,
time: new Date(),
room: socket.room,
};

rooms[socket.room].push(message);
broadcast(socket.room, { type: 'message', ...message });
}
});
});
Loading