[英]Web sockets / Tornado - Notify client on database update
我正在嘗試使用 Tornado web 套接字服務器在對數據庫進行實時更改時通知我的用戶。 我希望為此使用 HTML5 web sockets ,即使大多數瀏覽器不支持它們。 Tornado package 附帶的演示都沒有使用 web sockets 並且文檔中沒有提到它們,所以我不知道如何開始。 我可以在谷歌上找到的幾個例子要么不起作用,要么記錄不充分。
當 MySQL 數據庫已更新或類似的東西我可以用來學習時,有沒有人有任何示例說明我如何使用 Tornado 將數據推送到客戶端?
A Lee 的回答很好,如果您需要支持較舊的瀏覽器,您可能需要 socket.io。
Websockets 在龍卷風中非常容易:
import tornado.websocket
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
print "WebSocket opened"
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print "WebSocket closed"
然后將其路由為任何其他處理程序,並在您的視圖中包括 Websocket Javascript :
var ws = new WebSocket("ws://localhost:8888/websocket");
ws.onopen = function() {
ws.send("Hello, world");
};
ws.onmessage = function (evt) {
alert(evt.data);
};
有關更多信息,請參閱來源: https://github.com/facebook/tornado/blob/master/tornado/websocket.py
我在服務器端使用socket.io客戶端和torndio取得了成功。 Socket.IO
提供對 websocket 的抽象,並在瀏覽器不支持 websocket 時提供回退(長輪詢、flash 套接字等)。
In order to use it you just need to write a tornadio script a la the tornadio documentation that monitors your database and then include the socket.io JavaScript in your web pages and have it establish a connection to wherever your tornadio server resides at the URL route
you specified在你的龍卷風腳本中。
這篇使用websockets 和 redis的帖子很好地涵蓋了基本思想。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.