Skip to content
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

5 live chat implementation #7

Merged
merged 7 commits into from
Nov 20, 2024
Merged
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
12 changes: 12 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ Before running, make sure you have `ffmpeg` installed on your system.
node index.js
```
Be aware, that this application is designed for Linux systems!

## Architecture

### API

- Endpoints to add a channel, get all channels, get selected channel and set selected channel

### WebSockets

- `channel-added` and `channel-selected` events will be send to all connected clients
- chat messages: `send-chat-message` and `chat-message`
- users: `user-connected` and `user-disconnected`
73 changes: 9 additions & 64 deletions backend/controllers/ChannelController.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,11 @@
const ffmpegService = require('../services/FFmpegService');
const storageService = require('../services/StorageService');
const Channel = require('../models/Channel');

let channels = [new Channel('DEFAULT_CHANNEL', process.env.DEFAULT_CHANNEL_URL)];
let currentChannel = channels[0];

function setCurrent(req, res) {
const { channelName } = req.body || {};

if (!channelName) {
return res.status(400).json({ status: 'error', message: 'channelName is required' });
}

const nextChannel = channels.find(channel => channel.name === channelName);
if (!nextChannel) {
res.status(400).json({ status: 'error', message: 'Channel does not exist' });
}

if (currentChannel !== nextChannel) {
const segmentNumber = storageService.getNextSegmentNumber();
storageService.clearStorage();
currentChannel = nextChannel;
ffmpegService.startFFmpeg(nextChannel.url, segmentNumber);
}
res.status(200).json({ status: 'success', channelUrl: currentChannel.url });
}


function getCurrent(_, res) {
if (currentChannel) {
res.status(200).json({ channelName: currentChannel.name, channelUrl: currentChannel.url });
} else {
res.status(404).json({ status: 'error', message: 'No channel active' });
}
}


function getChannels(_, res) {
res.status(200).json({ channels });
}

function addChannel(req, res) {
const { name, url } = req.body;

if (!name || !url) {
return res.status(400).json({ status: 'error', message: 'Channel name and URL are required' });
}

const channelExists = channels.some(channel => channel.url === url);
if (channelExists) {
return res.status(409).json({ status: 'error', message: 'Channel already exists' });
}

const newChannel = new Channel(name, url);
channels.push(newChannel);
res.status(201).json({ status: 'success', message: 'Channel added', channel: newChannel });
}

const ChannelService = require('../services/ChannelService');

module.exports = {
setCurrent,
getCurrent,
getChannels,
addChannel
};
getChannels(req, res) {
res.json(ChannelService.getChannels());
},

getCurrentChannel(req, res) {
res.json(ChannelService.getCurrentChannel());
},
};
4 changes: 2 additions & 2 deletions backend/controllers/StreamController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ffmpegService = require('../services/FFmpegService');
const storageService = require('../services/StorageService');
const ffmpegService = require('../services/streaming/FFmpegService');
const storageService = require('../services/streaming/StorageService');

function start() {
if (!ffmpegService.isFFmpegRunning()) {
Expand Down
27 changes: 0 additions & 27 deletions backend/index.js

This file was deleted.

5 changes: 4 additions & 1 deletion backend/models/Channel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
class Channel {
constructor(name, url) {
static nextId = 0;
constructor(name, url, avatar) {
this.id = Channel.nextId++;
this.name = name;
this.url = url;
this.avatar = avatar;
}
}

Expand Down
9 changes: 9 additions & 0 deletions backend/models/ChatMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ChatMessage {
constructor(userId, message, timestamp) {
this.userId = userId;
this.message = message;
this.timestamp = timestamp;
}
}

module.exports = ChatMessage;
Loading