龙卷风websocket blob对象

时间:2015-01-10 15:51:38

标签: jquery blob tornado

我当前的代码应该从网站向服务器发送数据,该网站会将消息发送回网站并打印出来。我在网站上使用JQuery,在服务器上使用Tornado。我的当前代码成功地将文本发送到服务器,但在服务器发回文本时接收到blob对象。

   function setup() {
       var host = "ws://localhost:8889/"; 
       var socket = new WebSocket(host);  
       var $txt = $("#inputField");       // the text field in the html
       var $btnSend = $("#pingButton");   // the send button in the html
       $txt.focus();
       // receiving message - issue is here
       socket.onmessage = function(msg) {
           var newmessage = msg;  // replace newmessage with msg when issue fixed
           console.log(newmessage); // shows content of blobobject
           // $("#messageText").text(newmessage.data+pingcount);
           alert(newmessage.data + pingcount);
           pingcount++;             // pingcount is to show stuff is happening
           // alert(newmessage.data);
       }
       $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
       // anything below this point seems fine
       if (socket) {
           socket.onopen = function() {
               // alert("connection opened....");
           }
           socket.onclose = function() {
               alert("connection closed....");
           }
       } else {
           console.log("invalid socket");
       }
   }

我的龙卷风服务器

import tornado.ioloop
import tornado.web
import tornado.websocket
from tornado.websocket import WebSocketHandler
from tornado.options import define, options, parse_command_line

define("port", default=8889, help="run on the given port", type=int)
#link - http://localhost:8889/
#experimental websocket
class EchoWebSocket(tornado.websocket.WebSocketHandler):
    def check_origin(self, origin):
        return True
    def open(self):
        print ("WebSocket opened")

    def on_message(self, message):

        self.write_message("You said: " , message)
        print("message received",message)

    def on_close(self):
        print("WebSocket closed")
app = tornado.web.Application([
    (r'/', EchoWebSocket),
])

if __name__ == '__main__':
    parse_command_line()
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

1 个答案:

答案 0 :(得分:0)

找到解决方案
write_message需要另一个参数

def on_message(self, message):
    Letter="You said:"+message
    self.write_message(Letter,False)
    print("message recieved",message)
相关问题