我對龍捲風框架比較陌生,迄今爲止,事情一直很混亂,特別是在處理web套接字時。到目前爲止,我的代碼如下所示:帶龍捲風的Websockets
import tornado.options
import tornado.web
import tornado.websocket
import tornado.ioloop
from tornado.options import options, define
define(name='port', default=8000, help='.', type=int)
class WSHandler(tornado.websocket.WebSocketHandler):
clients = []
def open(self):
WSHandler.clients.append(self)
self.write('client connected')
def on_close(self):
WSHandler.clients.remove(self)
self.write('client removed')
def on_message(self, message):
for client in WSHandler.clients:
WSHandler.client.write_message(message)
def check_origin(self, origin):
return True
if __name__ == '__main__':
tornado.options.parse_command_line()
application = tornado.web.Application(
handlers=[
(r'/webSocket', WSHandler),
],
)
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
和JavaScript文件看起來像這樣
var ws = new WebSocket("ws://localhost:8000/webSocket")
ws.onopen = function() {
ws.setData('Hello World');
};
ws.onmessage = function(evt) {
ws.send(evt.data);
};
我目前正在讀的書「介紹龍捲風」,並意識到,有些事情是現已棄用。我確實下載了git-hub上提供的演示,並嘗試瞭解websocket演示。
我試圖創建一個簡單的WebSocket應用程序,但它的作用是給我這個錯誤:
Can "Upgrade" only to "WebSocket"
另外,我有龍捲風
另一個問題是什麼_()
是什麼意思?在文檔中給出的示例是
_("A person liked this", "%(num)d people liked this", len(people)) % {"num": len(people)}
並且這應該返回「一個人喜歡這個」,如果只有一個人在人。我不太明白這是如何工作
我道歉,如果我犯了一個錯誤,就是愚蠢明顯
我們面臨着類似的問題,因爲我們是從代理服務器後面的網絡測試。該代理正在刪除標題「連接:升級」。看到這個[答案](https://stackoverflow.com/a/23522393/1761793) – Ajoy