modifié: superTornado.py
modifié: test.html
This commit is contained in:
		@@ -47,19 +47,22 @@ class VideoHandler(tornado.web.RequestHandler):
 | 
			
		||||
    def get(self):
 | 
			
		||||
        self.render("test.html")
 | 
			
		||||
 | 
			
		||||
class WSHandler(tornado.websocket.WebSocketHandler):
 | 
			
		||||
    def open(self):
 | 
			
		||||
        self.callback = PeriodicCallback(self.send_hello, 120)
 | 
			
		||||
        self.callback.start()
 | 
			
		||||
 | 
			
		||||
    def send_hello(self):
 | 
			
		||||
        self.write_message('hello')
 | 
			
		||||
class WebSocketHandler(tornado.websocket.WebSocketHandler):
 | 
			
		||||
    def open(self, *args):
 | 
			
		||||
        self.id = self.get_argument("Id")
 | 
			
		||||
        self.stream.set_nodelay(True)
 | 
			
		||||
        clients[self.id] = {"id": self.id, "object": self}
 | 
			
		||||
 | 
			
		||||
    def on_message(self, message):
 | 
			
		||||
        pass
 | 
			
		||||
        """
 | 
			
		||||
        when we receive some message we want some message handler..
 | 
			
		||||
        for this example i will just print message to console
 | 
			
		||||
        """
 | 
			
		||||
        print "Client %s received a message : %s" % (self.id, message)
 | 
			
		||||
 | 
			
		||||
    def on_close(self):
 | 
			
		||||
        self.callback.stop()
 | 
			
		||||
        if self.id in clients:
 | 
			
		||||
            del clients[self.id]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
application = tornado.web.Application([
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										53
									
								
								test.html
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								test.html
									
									
									
									
									
								
							@@ -1,30 +1,31 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html>
 | 
			
		||||
<head>
 | 
			
		||||
<script>
 | 
			
		||||
 | 
			
		||||
function fun(){
 | 
			
		||||
 | 
			
		||||
    alert("in fun()");
 | 
			
		||||
 | 
			
		||||
    var val=document.getElementById("txt");
 | 
			
		||||
    var ws = new WebSocket("ws://192.168.1.23/test:80");
 | 
			
		||||
 | 
			
		||||
    ws.onopen = function(evt) { alert("Connection open ...");
 | 
			
		||||
    ws.send(val.value); };
 | 
			
		||||
    ws.onmessage = function(evt) {
 | 
			
		||||
         alert("from server: "+evt.data);
 | 
			
		||||
    <head>
 | 
			
		||||
        <meta charset="utf-8">
 | 
			
		||||
    <script type="text/javascript">
 | 
			
		||||
        var messageContainer = document.getElementById("messages");
 | 
			
		||||
        function WebSocketTest() {
 | 
			
		||||
            if ("WebSocket" in window) {
 | 
			
		||||
                messageContainer.innerHTML = "WebSocket is supported by your Browser!";
 | 
			
		||||
                var ws = new WebSocket("ws://localhost:8888/?Id=123456789");
 | 
			
		||||
                ws.onopen = function() {
 | 
			
		||||
                    ws.send("Message to send");
 | 
			
		||||
                };
 | 
			
		||||
                ws.onmessage = function (evt) {
 | 
			
		||||
                    var received_msg = evt.data;
 | 
			
		||||
                    messageContainer.innerHTML = "Message is received...";
 | 
			
		||||
                };
 | 
			
		||||
                ws.onclose = function() {
 | 
			
		||||
                    messageContainer.innerHTML = "Connection is closed...";
 | 
			
		||||
                };
 | 
			
		||||
            } else {
 | 
			
		||||
                messageContainer.innerHTML = "WebSocket NOT supported by your Browser!";
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
    ws.onclose = function(evt) {
 | 
			
		||||
        alert("Connection closed.");
 | 
			
		||||
        }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
</head>
 | 
			
		||||
<body bgcolor="#FFFFFF">
 | 
			
		||||
    <input type="text" id="txt" />
 | 
			
		||||
    <button onClick="fun()">click</button>
 | 
			
		||||
</body>
 | 
			
		||||
        </script>
 | 
			
		||||
    </head>
 | 
			
		||||
    <body>
 | 
			
		||||
        <a href="javascript:WebSocketTest()">Run WebSocket</a>
 | 
			
		||||
        <div id="messages" style="height:200px;background:black;color:white;"></div>
 | 
			
		||||
    </body>
 | 
			
		||||
</html>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user