2015-01-10 6 views
0

현재 코드는 웹 사이트에서 서버로 데이터를 보내고 메시지를 웹 사이트로 보내 인쇄합니다. 웹 사이트에 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() 

답변

0

솔루션
write_message 다른 인수를 필요로 찾을 수

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