LiveDanmaku如何在连接时调用disconnect? #376
Answered
by
Drelf2018
tp1415926535
asked this question in
Q&A
Replies: 4 comments 4 replies
-
你希望在发生什么情况下断开连接呢,比如说收到指定弹幕?或者是程序内某个函数执行完成后?还是单纯想运行三秒就断开? |
Beta Was this translation helpful? Give feedback.
0 replies
-
#324 你也可以参考这个 |
Beta Was this translation helpful? Give feedback.
2 replies
-
import asyncio
import sys
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QLineEdit, QPushButton, QVBoxLayout, QWidget
from bilibili_api import live
class Listener(QThread):
"""
监听器
用来开启、关闭和发送弹幕给窗体
"""
sinOut = pyqtSignal(str) # 发送消息的信号
def __init__(self):
"""
room: 要连接的直播间
loop: 异步事件循环
"""
super().__init__()
self.room = live.LiveDanmaku(510)
self.loop = asyncio.new_event_loop()
def run(self):
"""
为直播间绑定监听并多线程开启
"""
@self.room.on("DANMU_MSG")
async def _(evt):
"""
发送直播间弹幕
"""
uname = evt["data"]["info"][2][1]
msg = evt["data"]["info"][1]
self.sinOut.emit(uname + ":" + msg)
self.loop.run_until_complete(self.room.connect())
def close(self):
"""
断开连接
"""
self.sinOut.emit("") # 发送空白清空文字框
self.loop.create_task(self.room.disconnect())
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(350, 150)
self.setWindowTitle("直播间,启动!")
vbox = QVBoxLayout()
self.setLayout(vbox)
self.qle = QLineEdit(self)
self.qb1 = QPushButton("连接")
self.qb2 = QPushButton("断开")
vbox.addWidget(self.qle)
vbox.addWidget(self.qb1)
vbox.addWidget(self.qb2)
self.qb1.clicked.connect(self.new_listener)
def new_listener(self):
self.listener = Listener()
self.qb2.clicked.connect(self.listener.close)
self.listener.sinOut.connect(lambda s: self.qle.setText(s))
self.listener.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show() # 显示主窗口
app.exec_() # 等待直到登录窗口关闭 |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
tp1415926535
-
还有一点就是 live.LiveDanmaku() 也要重新实例化,不然会打印多次。 import asyncio,threading
from bilibili_api import live, sync
from threading import Thread
import time
class newThread(Thread):
def __init__(self, danmaku):
super().__init__()
self.danmaku = danmaku
self.loop =asyncio.new_event_loop()
@danmaku.on('DANMU_MSG')
async def on_danmaku(event):
name=event["data"]["info"][2][1]
text=event["data"]["info"][1]
print(f'{name} :{text}')
def end(self):
self.loop.create_task(self.danmaku.disconnect())
def run(self):
self.loop.run_until_complete(self.danmaku.connect())
print('ending')
if __name__ == "__main__":
live_danmaku = live.LiveDanmaku(510)
td = newThread(live_danmaku)
td.start()
time.sleep(3)
td.end()
td.join()
time.sleep(3)
live_danmaku = live.LiveDanmaku(510)#如果没有这行会打印两次
td = newThread(live_danmaku)
td.start()
time.sleep(3)
td.end() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
disconnect方法应该怎么调用?
新开loop会报attached to a different loop,用同个loop会报event loop is already running
Beta Was this translation helpful? Give feedback.
All reactions