-
Notifications
You must be signed in to change notification settings - Fork 253
added #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
added #178
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 }); | ||
| }); | ||
|
|
||
| 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]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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') { | ||
| 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 }); | ||
| } | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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.