How to solve websocket ping timeout?

佐手、 提交于 2020-01-14 05:23:16

问题


While running the following piece of code (in theory it should send a value every minute)

from __future__ import print_function
from twisted.internet.ssl import CertificateOptions
options = CertificateOptions()
from os import environ
from twisted.internet.defer import inlineCallbacks
from twisted.internet import reactor
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
from autobahn import wamp
from datetime import datetime, timedelta
import xlwings as wb
import time
import xlwings as wb

class Component(ApplicationSession):

"""
An application component that publishes an event every second.
"""

@inlineCallbacks
def onJoin(self, details):
    print("session attached")


while True:
    try:
        wb.Book(r'C:\Users\Administrator\Desktop\Datasets\test_feed.xlsx')
        e = wb.Range('A2').value
        b = wb.Range('C2').value
        c = wb.Range('E2').value
    except Exception:
        print("----Waiting for RTD server response----")
        time.sleep(1)          
    try:                
        epoch = datetime(now.year, now.month, now.day)
        result = epoch + timedelta(days=c)
        result = result.replace(microsecond=0, second=0)
        if result > now:
            now = result
            print("Stock", e, "Time", now, "Price", b)
            self.publish(u'com.myapp.ma', b)
    except Exception:
        print("-----Waiting1 for RTD server response----")
        time.sleep(1)
def onDisconnect(self):
        print("disconnected")
        reactor.stop()



if __name__ == '__main__':
    runner = ApplicationRunner(
        environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"),
        u"crossbardemo")
    runner.run(Component)

The following error is returned

2017-12-28T18:43:52+0100 [Router       1604] dropping connection to peer tcp4:127.0.0.1:61531 with abort=True: WebSocket ping timeout (peer did not respond with pong in time)
2017-12-28T18:43:52+0100 [Router       1604] session "8526139172223346" left realm "crossbardemo"

What I've tried to solve this problem:

I)

from twisted.internet.ssl import CertificateOptions
options = CertificateOptions()
if __name__ == '__main__':
    runner = ApplicationRunner(
        environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"),
        u"crossbardemo", ssl=options)
    runner.run(Component)

II)

if __name__ == '__main__':
    runner = ApplicationRunner(
        environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"),
        u"crossbardemo",
    )
    runner.run(Component, auto_reconnect=True)

III)

Regedit

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS

1.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS

1.0\Client] "DisabledByDefault"=dword:00000000 "Enabled"=dword:00000001

IV)

install certifi module (pip install certifi) set SSL_CERT_FILE, like export SSL_CERT_FILE="$(python -m certifi)"

With still the same error. I am running on Windows 10, with crossbar demo router, autobahn and twisted.

Link to router configuration:

https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/wamp/pubsub/basic/.crossbar

Also, the following example code is working properly:

counter = 100
        while True:
            print("publish: com.myapp.ma", counter)
            self.publish(u'com.myapp.ma', counter)
            counter += 100
            yield sleep(30)

回答1:


For Twisted to process further I/O events, you have to give control back to the reactor. Twisted implements a cooperative multitasking system. Various tasks run in the reactor thread. This is accomplished by each task only spending a brief time in control. Code like:

while True:
     ...
     sleep(1)

prevents any other tasks from gaining control to execute and also prevents the reactor from gaining control to service I/O events.

Since this code is within a function decorated with inlineCallbacks, there is a very small change that will make it at least not completely incompatible with Twisted's mode of operation.

Instead of time.sleep(1), try this expression:

yield deferLater(reactor, 1, lambda: None)

And import deferLater from twisted.internet.task. This will perform a "sleep" which gives control back to the reactor and lets other tasks execute during the sleep. This should allow Autobahn to send the necessary ping/pong messages as well as allow it to process your publish call.



来源:https://stackoverflow.com/questions/48012394/how-to-solve-websocket-ping-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!