Skip to content

Commit

Permalink
use event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhao committed Jan 15, 2024
1 parent e2138ad commit 6d3e718
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions livekit-rtc/livekit/rtc/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,30 @@
from datetime import datetime
import json
import logging
from typing import Any, Callable, Dict, Optional
from typing import Any, Callable, Dict, Literal, Optional

from .room import Room, Participant, DataPacket
from ._event_emitter import EventEmitter
from ._proto.room_pb2 import DataPacketKind
from ._utils import generate_random_base62

_CHAT_TOPIC = "lk-chat-topic"
_CHAT_UPDATE_TOPIC = "lk-chat-update-topic"

EventTypes = Literal["message_received",]

class ChatManager:

class ChatManager(EventEmitter[EventTypes]):
"""A utility class that sends and receives chat messages in the active session.
It implements LiveKit Chat Protocol, and serializes data to/from JSON data packets.
"""

def __init__(
self, room: Room, *, on_message: Callable[["ChatMessage"], None] = None
):
def __init__(self, room: Room):
self._lp = room.local_participant
self._room = room
self._callback: Callable[["ChatMessage"], None] = None

room.on("data_received", self._on_data_received)
if on_message:
self.on_message(on_message)

def close(self):
self._room.off("data_received", self._on_data_received)
Expand Down Expand Up @@ -92,8 +90,7 @@ def _on_data_received(self, dp: DataPacket):
msg = ChatMessage.from_jsondict(parsed)
if dp.participant:
msg.participant = dp.participant
if self._callback:
self._callback(msg)
self.emit("message_received", msg)
except Exception as e:
logging.warning("failed to parse chat message: %s", e, exc_info=e)

Expand Down

0 comments on commit 6d3e718

Please sign in to comment.