簡體   English   中英

在收到通知后從服務器向客戶端發送消息(Tornado + websockets)

[英]Send message from server to client after received notification (Tornado+websockets)

我最近開始學習Websocket,並且我決定嘗試學習和使用Python的framweork Tornado創建我的簡單測試項目(沒什么特別的,只是基本項目可以幫助我學習有關Tornado和Websockets的一般知識)。

因此,這是我的想法(工作流程):

1)我收到來自其他應用的http發布請求到我的服務器(例如,有關某人的姓名和電子郵件的信息)

2)我將收到的數據保存到我的postgresql數據庫中,並通知偵聽器(發布/訂閱)新數據已添加到數據庫中

3)接收到通知服務器后,應向客戶端發送消息(write_message方法)

這是我到目前為止的代碼

simple_server.py

import tornado.httpserver
import tornado.ioloop
import tornado.options 
import tornado.web
import tornado.websocket
import psycopg2
import psycopg2.extensions
import os
from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

io_loop = tornado.ioloop.IOLoop.instance()

connection = psycopg2.connect('dbname=mydb user=myusername password=mypassword')
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

class ReceivedDataHandler(tornado.web.RequestHandler):  
    def post(self):     
        cursor = connection.cursor()

        name=self.get_argument('name', 'No name info received')
        email = self.get_argument('email', 'No email info received')
        self.write("New person with name %s and email %s" %(name, email))

        cursor.execute("INSERT INTO mydata VALUES (%s, %s)" %(name, email))
        cursor.execute("NOTIFY test_channel;")

class EchoHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        self.write_message('connected!')
    def on_message(self, message):
        self.write_message("Received info about new person: "+message)
    def on_close(self):
        print 'connection closed'

def listen():    
    cursor = connection.cursor()
    cursor.execute("LISTEN test_channel;") 

def receive(fd, events):
    """Receive a notify message from channel I listen."""
    state = connection.poll()
    if state == psycopg2.extensions.POLL_OK:
        if connection.notifies:
            notify = connection.notifies.pop()
            print "New notify message"
io_loop.add_handler(connection.fileno(), receive, io_loop.READ)

if __name__=="__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r'/', IndexHandler),
            (r'/person-info', ReceivedDataHandler),
            (r'/websocket', EchoHandler)
        ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    listen()
    io_loop.start()

當我測試通過Postman REST Client發送發帖請求到定義的URL時,一切正常。 數據確實保存到我的數據庫中,並且確實通知偵聽器有新的通知,但是我不知道如何在那之后將消息發送給客戶端。 如果我能做到這一點,那么它將在瀏覽器中顯示該消息,這是我這次要做的全部。

因此,我要做的實際上是在收到有關數據庫中新條目的通知后調用call_message函數(而不是僅打印“ new notify message”),但我只是不知道如何在Tornado中進行操作。 我認為實際上這應該很容易,但是由於我顯然對Tornado(和異步編程)缺乏經驗,所以我一點都沒有堅持。

謝謝你的幫助

最終,我找到了解決該問題的方法。 我剛剛添加了全局變量,在其中添加了所有連接的客戶端,然后在收到通知時向每個連接的客戶端發送消息。 (因為我確實想向所有連接的客戶端發送消息,所以對我來說還可以)

所以這就是現在的樣子

simple_server.py

import tornado.httpserver
import tornado.ioloop
import tornado.options 
import tornado.web
import tornado.websocket
import psycopg2
import psycopg2.extensions
import os
from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

io_loop = tornado.ioloop.IOLoop.instance()

connection = psycopg2.connect('dbname=mydb user=myusername password=mypassword')
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

# This is a global variable to store all connected clients
websockets = []

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

class ReceivedDataHandler(tornado.web.RequestHandler):  
    def post(self):     
        cursor = connection.cursor()

        name=self.get_argument('name', 'No name info received')
        email = self.get_argument('email', 'No email info received')
        self.write("New person with name %s and email %s" %(name, email))

        cursor.execute("INSERT INTO mydata VALUES (%s, %s)" %(name, email))
        cursor.execute("NOTIFY test_channel;")

class EchoHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        self.write_message('connected!')
    def on_message(self, message):
        self.write_message("Received info about new person: "+message)
    def on_close(self):
        print 'connection closed'

def listen():    
    cursor = connection.cursor()
    cursor.execute("LISTEN test_channel;") 

def receive(fd, events):
    """Receive a notify message from channel I listen."""
    state = connection.poll()
    if state == psycopg2.extensions.POLL_OK:
        if connection.notifies:
            notify = connection.notifies.pop()
            for ws in websockets:
                   ws.write_message("my message")
io_loop.add_handler(connection.fileno(), receive, io_loop.WRITE)

if __name__=="__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r'/', IndexHandler),
            (r'/person-info', ReceivedDataHandler),
            (r'/websocket', EchoHandler)
        ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    listen()
    io_loop.start()

可以將其添加到您的EchoHandler.open()方法中:

io_loop.add_handler(connection.fileno(), self.receive, io_loop.READ)

刪除接收函數和另一個io_loop.add_handler調用,然后編寫新版本的接收函數,但使其成為EchoHandler類的實例方法。 這樣,您可以從新的receive方法中調用write_message方法。

諸如此類(未經測試,但希望能闡明我的意思):

class EchoHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        io_loop.add_handler(connection.fileno(), self.receive, io_loop.READ)
        self.write_message('connected!')

    def on_message(self, message):
        self.write_message("Received info about new person: "+message)

    def on_close(self):
        print 'connection closed'

    def receive(self, fd, events):
        """Receive a notify message from channel I listen."""
        state = connection.poll()
        if state == psycopg2.extensions.POLL_OK:
            if connection.notifies:
                notify = connection.notifies.pop()
                #print "New notify message"
                self.write_message("Your message here")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM