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