[英]How to send server message to specific client in tornado websocket
嘿,我是python的新手,這是龍卷風中的Websocket服務器代碼
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
self.write(loader.load("index.html").generate())
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'connection opened...'
self.write_message("The server says: 'Hello'. Connection was accepted.")
def on_message(self, message):
self.write_message("The server says: " + message + " back at you")
print 'received:', message
def on_close(self):
print 'connection closed...'
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/', MainHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": "./resources"}),
])
if __name__ == "__main__":
application.listen(9090)
tornado.ioloop.IOLoop.instance().start()
它工作正常並在服務器上接收到我的消息(客戶端消息),但可惜它沒有向我發送其他客戶端消息。 就像我有這個HTML
<!DOCTYPE html>
<html>
<head>
<title>WebSockets Client</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
Enter text to send to the websocket server:
<div id="send">
<input type="text" id="data" size="100"/><br>
<input type="button" id="sendtext" value="send text"/>
</div>
<div id="output"></div>
</body>
</html>
<script>
jQuery(function($){
if (!("WebSocket" in window)) {
alert("Your browser does not support web sockets");
}else{
setup();
}
function setup(){
// Note: You have to change the host var
// if your client runs on a different machine than the websocket server
var host = "ws://localhost:9090/ws";
var socket = new WebSocket(host);
console.log("socket status: " + socket.readyState);
var $txt = $("#data");
var $btnSend = $("#sendtext");
$txt.focus();
// event handlers for UI
$btnSend.on('click',function(){
var text = $txt.val();
if(text == ""){
return;
}
socket.send(text);
$txt.val("");
});
$txt.keypress(function(evt){
if(evt.which == 13){
$btnSend.click();
}
});
// event handlers for websocket
if(socket){
socket.onopen = function(){
//alert("connection opened....");
}
socket.onmessage = function(msg){
showServerResponse(msg.data);
}
socket.onclose = function(){
//alert("connection closed....");
showServerResponse("The connection has been closed.");
}
}else{
console.log("invalid socket");
}
function showServerResponse(txt){
var p = document.createElement('p');
p.innerHTML = txt;
document.getElementById('output').appendChild(p);
}
}
});
</script>
當我點擊客戶端發送按鈕(使用上面的html)時,它向服務器發送了我的消息,但是我想向其他客戶端發送消息。 如何將消息從服務器發送到其他客戶端(如任何所需客戶端) 。 注釋中給出的鏈接為我提供了一種方法(將全局列表變量,在其中添加每個客戶端,然后在消息事件中循環並發送消息)將消息發送給所有客戶端,但我也希望將消息發送給特定客戶端。
您需要一些外部系統來執行此操作。 就像本·達內爾(Ben Darnell)所說的那樣,另一個問題解釋了一些吸引客戶的原始方法。
您需要的是在初始化時收集每個客戶端的某種ID。 這可能是您系統中的一個帳戶,或者您可以為每個新連接生成一個新帳戶:
import uuid
clients = {}
class WSHandler(tornado.websocket.WebSocketHandler):
def __init__(self, application, request, **kwargs):
super(WSHandler, self).__init__(application, request, **kwargs)
self.client_id = str(uuid.uuid4())
def open(self):
print 'connection for client {0} opened...'.format(self.client_id)
clients[self.client_id] = self
self.write_message("The server says: 'Hello'. Connection was accepted.")
def on_message(self, message):
self.write_message("The server says: " + message + " back at you")
print 'received:', message
def on_close(self):
clients.pop(self.client_id, None)
print 'connection closed...'
以后,您可以使用完全由client_id
這個名稱來告訴其他客戶端,存在具有該ID的客戶端。 以后你可以用
clients[<id>].write_message("Hello!")
不過,要注意的是,這種方法無法很好地擴展。 實際上,您將只能僅尋址連接到當前龍卷風實例的客戶端。 如果您需要多個實例,並且需要一種到達任何客戶端的方式,請參閱消息代理,例如rabbitmq
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.