如何解决WebSocket ping超时?

How to solve websocket ping timeout?(如何解决WebSocket ping超时?)

本文介绍了如何解决WebSocket ping超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行以下代码时(理论上它应该每分钟发送一个值)

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:UsersAdministratorDesktopDatasets	est_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)

返回以下错误

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"

我尝试解决此问题的内容:

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_MACHINESYSTEMCurrentControlSetControlSecurityProvidersSCHANNELProtocolsTLS

1.0]

[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSecurityProvidersSCHANNELProtocolsTLS

1.0客户端] "DisabledByDefault"=双字:00000000 "Enabled"=双字:00000001

IV)

安装证书模块(Pip Install Certifi)设置SSL_CERT_FILE,如 导出SSL_CERT_FILE="$(python-m证书)"

但仍有相同的错误。我在Windows10上运行,带有Crossbar演示路由器、Autobahn和Twisted。

路由器配置链接:

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

另外,以下示例代码工作正常:

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

推荐答案

要让Twisted处理进一步的I/O事件,您必须将控制权交还给反应器。Twisted实现了协作多任务系统。各种任务在反应器线程中运行。这是通过每项任务只花很短的时间进行控制来实现的。代码如下:

while True:
     ...
     sleep(1)

防止任何其他任务获得执行的控制权,也阻止反应器获得服务I/O事件的控制权。

由于此代码位于用inlineCallbacks修饰的函数中,因此有一个非常小的更改,它至少不会与Twisted的操作模式完全不兼容。

而不是time.sleep(1),请尝试此表达式:

yield deferLater(reactor, 1, lambda: None)

并从twisted.internet.task导入deferLater。这将执行"休眠",将控制权交还给反应堆,并让其他任务在休眠期间执行。这应该允许Autobahn发送必要的ping/pong消息,并允许它处理您的publish呼叫。

这篇关于如何解决WebSocket ping超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何解决WebSocket ping超时?

基础教程推荐