Skip to content

connection

framewise_meet_client.connection

WebSocketConnection

Manages WebSocket connection to the server.

Source code in framewise_meet_client/connection.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class WebSocketConnection:
    """Manages WebSocket connection to the server."""

    def __init__(self, host: str, port: int, meeting_id: str, api_key: Optional[str] = None):
        """Initialize the connection.

        Args:
            host: Server hostname
            port: Server port
            meeting_id: ID of the meeting to join
            api_key: Optional API key for authentication
        """
        self.host = host
        self.port = port
        self.meeting_id = meeting_id
        self.api_key = api_key
        self.websocket = None
        self.connected = False

    async def connect(self) -> None:
        """Connect to the WebSocket server."""
        url = f"ws://{self.host}:{self.port}/listen/{self.meeting_id}"

        # Add API key to headers if provided
        headers = {}
        if self.api_key:
            headers["Authorization"] = f"Bearer {self.api_key}"
            logger.debug("Added API key to connection headers")

        try:
            self.websocket = await websockets.connect(url)
            self.connected = True
            logger.info(f"Connected to server at {url}")

            # Wait for auth confirmation if API key was provided
            if self.api_key:
                try:
                    # Wait for auth confirmation message with timeout
                    auth_message = await asyncio.wait_for(self.websocket.recv(), timeout=5.0)
                    auth_data = json.loads(auth_message)

                    if auth_data.get("type") == "auth_result" and not auth_data.get("success", False):
                        logger.error("Authentication rejected by server")
                        await self.disconnect()
                        raise AuthenticationError("Authentication rejected by server")

                    logger.info("Server authenticated connection")

                except asyncio.TimeoutError:
                    # If we don't get an explicit auth confirmation, assume it's OK
                    logger.warning("No explicit authentication confirmation from server")


        except Exception as e:
            self.connected = False
            logger.error(f"Failed to connect: {str(e)}")
            raise ConnectionError(f"Failed to connect: {str(e)}")

    async def disconnect(self) -> None:
        """Disconnect from the WebSocket server."""
        if self.websocket and self.connected:
            await self.websocket.close()
            self.connected = False
            logger.info("Disconnected from server")

    async def send(self, message: Dict[str, Any]) -> None:
        """Send a message to the server.

        Args:
            message: JSON-serializable message to send
        """
        if not self.connected or not self.websocket:
            raise ConnectionError("Not connected to server")

        try:
            await self.websocket.send(json.dumps(message))
        except Exception as e:
            logger.error(f"Failed to send message: {str(e)}")
            raise ConnectionError(f"Failed to send message: {str(e)}")


    async def send_json(self,message):
        """Send a JSON serializable message to the server.

        Args:
            message: JSON-serializable message to send
        """
        # This is an alias for the send method
        return await self.send(message)

    async def receive(self) -> Dict[str, Any]:
        """Receive a message from the server.

        Returns:
            Parsed JSON message from the server
        """
        if not self.connected or not self.websocket:
            raise ConnectionError("Not connected to server")

        try:
            message = await self.websocket.recv()
            return json.loads(message)
        except websockets.exceptions.ConnectionClosed:
            self.connected = False
            logger.warning("Connection closed")
            raise ConnectionError("Connection closed")
        except Exception as e:
            logger.error(f"Failed to receive message: {str(e)}")
            raise ConnectionError(f"Failed to receive message: {str(e)}")

__init__(host, port, meeting_id, api_key=None)

Initialize the connection.

Parameters:

Name Type Description Default
host str

Server hostname

required
port int

Server port

required
meeting_id str

ID of the meeting to join

required
api_key Optional[str]

Optional API key for authentication

None
Source code in framewise_meet_client/connection.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __init__(self, host: str, port: int, meeting_id: str, api_key: Optional[str] = None):
    """Initialize the connection.

    Args:
        host: Server hostname
        port: Server port
        meeting_id: ID of the meeting to join
        api_key: Optional API key for authentication
    """
    self.host = host
    self.port = port
    self.meeting_id = meeting_id
    self.api_key = api_key
    self.websocket = None
    self.connected = False

connect() async

Connect to the WebSocket server.

Source code in framewise_meet_client/connection.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
async def connect(self) -> None:
    """Connect to the WebSocket server."""
    url = f"ws://{self.host}:{self.port}/listen/{self.meeting_id}"

    # Add API key to headers if provided
    headers = {}
    if self.api_key:
        headers["Authorization"] = f"Bearer {self.api_key}"
        logger.debug("Added API key to connection headers")

    try:
        self.websocket = await websockets.connect(url)
        self.connected = True
        logger.info(f"Connected to server at {url}")

        # Wait for auth confirmation if API key was provided
        if self.api_key:
            try:
                # Wait for auth confirmation message with timeout
                auth_message = await asyncio.wait_for(self.websocket.recv(), timeout=5.0)
                auth_data = json.loads(auth_message)

                if auth_data.get("type") == "auth_result" and not auth_data.get("success", False):
                    logger.error("Authentication rejected by server")
                    await self.disconnect()
                    raise AuthenticationError("Authentication rejected by server")

                logger.info("Server authenticated connection")

            except asyncio.TimeoutError:
                # If we don't get an explicit auth confirmation, assume it's OK
                logger.warning("No explicit authentication confirmation from server")


    except Exception as e:
        self.connected = False
        logger.error(f"Failed to connect: {str(e)}")
        raise ConnectionError(f"Failed to connect: {str(e)}")

disconnect() async

Disconnect from the WebSocket server.

Source code in framewise_meet_client/connection.py
69
70
71
72
73
74
async def disconnect(self) -> None:
    """Disconnect from the WebSocket server."""
    if self.websocket and self.connected:
        await self.websocket.close()
        self.connected = False
        logger.info("Disconnected from server")

send(message) async

Send a message to the server.

Parameters:

Name Type Description Default
message Dict[str, Any]

JSON-serializable message to send

required
Source code in framewise_meet_client/connection.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
async def send(self, message: Dict[str, Any]) -> None:
    """Send a message to the server.

    Args:
        message: JSON-serializable message to send
    """
    if not self.connected or not self.websocket:
        raise ConnectionError("Not connected to server")

    try:
        await self.websocket.send(json.dumps(message))
    except Exception as e:
        logger.error(f"Failed to send message: {str(e)}")
        raise ConnectionError(f"Failed to send message: {str(e)}")

send_json(message) async

Send a JSON serializable message to the server.

Parameters:

Name Type Description Default
message

JSON-serializable message to send

required
Source code in framewise_meet_client/connection.py
92
93
94
95
96
97
98
99
async def send_json(self,message):
    """Send a JSON serializable message to the server.

    Args:
        message: JSON-serializable message to send
    """
    # This is an alias for the send method
    return await self.send(message)

receive() async

Receive a message from the server.

Returns:

Type Description
Dict[str, Any]

Parsed JSON message from the server

Source code in framewise_meet_client/connection.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async def receive(self) -> Dict[str, Any]:
    """Receive a message from the server.

    Returns:
        Parsed JSON message from the server
    """
    if not self.connected or not self.websocket:
        raise ConnectionError("Not connected to server")

    try:
        message = await self.websocket.recv()
        return json.loads(message)
    except websockets.exceptions.ConnectionClosed:
        self.connected = False
        logger.warning("Connection closed")
        raise ConnectionError("Connection closed")
    except Exception as e:
        logger.error(f"Failed to receive message: {str(e)}")
        raise ConnectionError(f"Failed to receive message: {str(e)}")