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

断线重连 #23

Open
elseif opened this issue Dec 4, 2023 · 0 comments
Open

断线重连 #23

elseif opened this issue Dec 4, 2023 · 0 comments

Comments

@elseif
Copy link

elseif commented Dec 4, 2023

    async def ws_event(self):
        async with aiohttp.ClientSession() as session:
            while not self.closed:
                try:
                    async with session.ws_connect(f"{self.baseurl}", headers=self.headers) as ws_connection:
                        logger.info("Protocol: connected")
                        while True:
                            if self.closed or ws_connection.closed:
                                break
                            try:
                                received_data = await ws_connection.receive_json(timeout=3)
                            except:
                                continue
                            if received_data:
                                post_type = received_data["post_type"]
                                try:
                                    if post_type == "message":
                                        received_data = MessageTypes[received_data["message_type"]].parse_obj(received_data)
                                    elif post_type == "notice":
                                        received_data = NoticeTypes[received_data["notice_type"]].parse_obj(received_data)
                                    elif post_type == "request":
                                        received_data = RequestTypes[received_data["request_type"]].parse_obj(received_data)
                                    else:
                                        continue
                                except KeyError:
                                    logger.error("Protocol: data parse error: " + str(received_data))
                                    continue
                                except pydantic.error_wrappers.ValidationError:
                                    logger.error("Protocol: data parse error: " + str(received_data))
                                    continue
                                await self.queue.put(InternalEvent(
                                    name=self.getEventCurrentName(type(received_data)),
                                    body=received_data
                                ))
                    logger.info("Protocol: disconnected")
                except Exception as e:
                    logger.error(e)
                await asyncio.sleep(3)  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant