简体   繁体   English

使用sendb进行使用Autobahn WebSocket库的CouchDB更改

[英]Use sendMessage for CouchDB Changes using Autobahn WebSocket Library

So far I have basic code to connect my websocket server to a websocket client. 到目前为止,我已经有基本的代码将我的websocket服务器连接到一个websocket客户端。 I am using Autobahn for the server code, and Advanced REST Client as the client. 我将Autobahn用于服务器代码,并将Advanced REST Client作为客户端。 In a separate method in the DBAlertProtocol class I am long polling a database in CouchDB for any changes that occur, ie add, delete, update, etc. This method gets call 5 seconds after the websocket connection is open. DBAlertProtocol类的一个单独方法中,我长时间在CouchDB中轮询数据库是否发生了任何更改,即添加,删除,更新等。打开Websocket连接后5秒钟将调用此方法。

There's is an issue with using sendMessage where the data is not showing up on the client side, or sometimes it takes a very long time to arrive. 使用sendMessage存在一个问题,即数据没有显示在客户端,或者有时需要很长时间才能到达。

Is there a way to change the communication options? 有没有办法更改通讯选项? Could the data be too large in size to send? 数据太大可能无法发送吗? I am trying to figure out why my other examples can send data successfully, but the couchdb changes notifications cannot. 我试图弄清楚为什么我的其他示例可以成功发送数据,但是ouchdb更改通知无法成功。

Below is the code that I have so far. 下面是我到目前为止的代码。

Thanks in advance! 提前致谢!

server.py server.py

import sys
import logging
import couchdb
from twisted.python import log
from twisted.internet import reactor

from autobahn.twisted.websocket import WebSocketServerFactory, \
                        WebSocketServerProtocol, listenWS
from autobahn.twisted.resource import WebSocketResource

couch = couchdb.Server("http://localhost:5984/")
db = couch['event_db']

class DBAlertProtocol(WebSocketServerProtocol):

  def onConnect(self, request):
    print("Connection made on server side")

  def onOpen(self):
    print("WebSocket connection open.")
    reactor.callLater(5, self.check_db_changes)

  def check_db_changes(self):
    since = 1
    print("\nstart loop\n")
    while True:
      changes = db.changes(since=since, include_docs=True)
      since = changes['last_seq'] 
      no_docs_changed = len(changes)
      counter = 0
      for changeset in changes['results']:
        print("\nChange detected!\n")
        try:
          doc = db[changeset['id']]
        except couchdb.http.ResourceNotFound:
          print("Resource not found, or was deleted.")
        else:
          counter += 1
          print("Number of docs effected: {}".format(str(counter)))
          # Send change data to MW
          self.sendMessage(str(changeset))

  def onClose(self, wasClean, code, reason):
    print("WebSocket closed on server side: {}".format(reason))

  def onMessage(self, payload, isBinary):
    print("Data received from database: {}".format(payload))
    self.sendMessage("Message received.")


class DBAlertFactory(WebSocketServerFactory):
  protocol =  DBAlertProtocol


def main():
  log.startLogging(sys.stdout)

  port = 8000

  factory = DBAlertFactory(u"ws://127.0.0.1:8000")

  listenWS(factory)
  print("Listening on port: {}".format(str(port)))
  print("Starting reactor...")
  reactor.run()


if __name__ == "__main__":
  main()

check_db_changes never gives up control so no other part of your program can ever run: check_db_changes永远不会放弃控制,因此程序的其他任何部分都无法运行:

while True:

Instead, try something like twisted.internet.task.LoopingCall . 而是尝试使用twisted.internet.task.LoopingCall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM