Skip to content

Commit

Permalink
#2 Support to receive chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyboer committed Oct 27, 2020
1 parent 3885906 commit a3e717a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
53 changes: 50 additions & 3 deletions lib/src/connect/meeting/main_websocket/chat/chat.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:async';

import 'package:bbb_app/src/connect/meeting/main_websocket/chat/message.dart';
import 'package:bbb_app/src/connect/meeting/main_websocket/module.dart';
import 'package:flutter/widgets.dart';

Expand All @@ -6,27 +9,71 @@ class ChatModule extends Module {
/// The default chat ID of the main public group chat.
static const String defaultChatID = "MAIN-PUBLIC-GROUP-CHAT";

/// Topic of chat messages to subscribe to.
static const String _groupChatMessageTopic = "group-chat-msg";

/// Controller publishing chat messages.
StreamController<ChatMessage> _chatMessageController =
StreamController.broadcast();

/// Messages already received.
List<ChatMessage> _messages = [];

ChatModule(messageSender) : super(messageSender);

/// Send a chat message.
Future<void> sendGroupChatMsg({
String chatID = defaultChatID,
@required String internUserID,
@required String message,
}) async {}
}) async {
// TODO
}

@override
void processMessage(Map<String, dynamic> msg) {
// TODO: implement processMessage
final String method = msg["msg"];

if (method == "added") {
String collectionName = msg["collection"];

if (collectionName == "group-chat-msg") {
Map<String, dynamic> fields = msg["fields"];

String chatID = fields["chatId"];
int timestamp = fields["timestamp"];
String senderID = fields["sender"];
String content = fields["message"];

ChatMessage message = ChatMessage(
chatID: chatID,
senderID: senderID,
content: content,
timestamp: new DateTime.fromMillisecondsSinceEpoch(timestamp),
);

print(
"Incoming message: ${message.content} (From ${message.timestamp.toString()}");

_messages.add(message);
_chatMessageController.add(message);
}
}
}

@override
void onConnected() {
// TODO: Subscribe to the chat topic
subscribe(_groupChatMessageTopic);
}

@override
Future<void> onDisconnect() async {
// Do nothing
}

/// Get a stream of incoming messages.
Stream<ChatMessage> get messageStream => _chatMessageController.stream;

/// Get already received messages.
List<ChatMessage> get messages => _messages;
}
32 changes: 32 additions & 0 deletions lib/src/connect/meeting/main_websocket/chat/message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// A chat message representation.
class ChatMessage {
/// ID of the chat.
String _chatID;

/// Intern user ID of the sender.
String _senderID;

/// Content of the message.
String _content;

/// Timestamp of the message.
DateTime _timestamp;

ChatMessage({
String chatID,
String senderID,
String content,
DateTime timestamp,
}) : this._chatID = chatID,
this._senderID = senderID,
this._content = content,
this._timestamp = timestamp;

String get content => _content;

String get senderID => _senderID;

String get chatID => _chatID;

DateTime get timestamp => _timestamp;
}

0 comments on commit a3e717a

Please sign in to comment.