import asyncio import websockets import json import time async def connect_and_send_messages(): uri = "wss://live-ws.immomo.com/ws/im" async with websockets.connect(uri) as websocket: print(f"连接websocket:[{uri}] 完成") client_time = "1711123627734" momoid = "1062302597" roomid = "17066184578990" token = "b458d6fd04ee38a9e5330e76a3802df9" data = '{{"msg_id":1,"client_time":{0},"type":"Sauth","data":{{"momoid":"{1}","roomid":"{2}","role":6,"isVisitor":false,"token":"{3}","ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"}}}}'.format( client_time, momoid, roomid, token) await websocket.send(data) print("直播间通讯成功") msg_id = 3 # 初始化消息的 msg_id while True: client_time = str(int(time.time() * 1000)) message_data = '{{"msg_id":{0},"client_time":{1},"type":"Bili","data":{{"text":"666"}}}}'.format(msg_id, client_time) await websocket.send(message_data) print("Send Message:", message_data) msg_id += 1 # 递增消息的 msg_id await asyncio.sleep(10) # 每隔10秒发送一次消息 async def main(): connect_task = asyncio.create_task(connect_and_send_messages()) await connect_task asyncio.run(main())