WebSocket-for-Python icon indicating copy to clipboard operation
WebSocket-for-Python copied to clipboard

断线重连怎么做?

Open 54huige opened this issue 8 years ago • 2 comments

from ws4py.client.threadedclient import WebSocketClient from ws4py.exc import HandshakeError class DummyClient(WebSocketClient): def opened(self): self.send(input('请输入你想说的话:'))

def closed(self, code, reason=None):
    #print("Closed down", code, reason)
    print("Closed down")


def received_message(self, m):
    print (m)

if name == 'main': try: ws = DummyClient('ws://'+input('请输入ws地址连接:'), protocols=['chat']) ws.connect() ws.run_forever() except KeyboardInterrupt: ws.close()

54huige avatar Jan 19 '18 06:01 54huige

同求,我在配合爬虫使用的时候,运行几个小时后,会报1006going away的错误,然后关闭了

jeffcumt avatar Nov 20 '18 08:11 jeffcumt

    count = 0
    while True: 
    try: 
        ws = WSClient('xxx') 
        print("Start contenting...") 
        ws.connect() 
        ws.run_forever() 
    except KeyboardInterrupt: 
        ws.close() 
        break 
    except Exception as e: 
        count += 1 
        print(e, "Try again %s times..." % count) 
        continue

I hope it helps you

laiyazhou avatar Dec 27 '18 08:12 laiyazhou