我最近开始学习websockets并且我决定尝试学习并使用Python的framweork Tornado来创建我的简单测试项目(没什么特别的,只是基本项目可以帮助我学习一些关于Tornado和websockets的东西)
所以,这是我的想法(工作流程):
1)我从其他应用程序收到http post请求到我的服务器(例如关于某个人姓名和电子邮件的信息)
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客户端)到定义的URL时,一切正常。数据真的保存到我的数据库中,它确实通知了监听器有新的通知但我不知道如何在那之后将该消息发送给客户端。 如果我可以这样做,那么它会在浏览器中显示该消息,这就是我现在想要做的所有事情。
所以,我想要做的就是在收到有关数据库中新条目的通知(而不是仅仅打印“新通知消息”)后调用write_message函数,但我只是不知道如何在Tornado中执行此操作。 我相信它实际上应该很容易,但由于我显然缺乏Tornado(和异步编程)的经验,我很少被卡住。
感谢您的帮助
答案 0 :(得分:1)
最终我找到了解决这个问题的方法。 我刚刚添加了全局变量,我添加了所有连接的客户端,然后在收到通知时向每个连接的客户端发送消息。 (这对我来说没问题,因为我实际上想要向所有连接的客户发送消息)
这就是现在的样子
<强> 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()
答案 1 :(得分:0)
可以将它添加到您的EchoHandler.open()方法中:
io_loop.add_handler(connection.fileno(), self.receive, io_loop.READ)
删除receive函数和另一个io_loop.add_handler调用,然后编写一个新版本的receive函数,但使其成为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")