This article brings you an introduction to WebSocket client programming under Tornado in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Since WebSocket is one of the standards of HTML5, the Web client programming voice JavaScript of mainstream browsers already supports WebSocket client programming.
Client programming revolves around the WebSocket object. In JavaScript, you can initialize the WebSocket object through the following code:
var Socket=new WebSocket(url);
In the code, you only need to pass in the URL address of the server to the WebSocket constructor, such as http://mysite.com/point.
You can specify handler functions for the following events of this object to respond to them:
WebSocket.onopen: This event occurs on When the WebSocket connection is established.
WebSocket.onmessage: This event occurs when a message from the server is received.
WebSocket.onerror: This event occurs when there is any error during communication.
WebSocket.onclose: This event occurs when the server's link is closed.
In addition to these event handling functions, you can also perform active operations through two methods of the WebSocket object:
WebSocket.send(data): Send a message to the server.
WebSocket.close(): Actively close existing links.
The client WebSocket programming example program is as follows: index.html
nbsp;html> <meta> <title>WebSocket</title> <a>运行WebSocket</a> <p></p> <script> var messageContainer=document.getElementById("messages"); function WebSocketTest() { if("WebSocket" in window){ messageContainer.innerHTML="浏览器支持WebSocket"; var ws=new WebSocket("ws://localhost:8888/websocket?Id=12345"); ws.onopen=function () { ws.send("onopen") }; ws.onmessage=function (evt) { var received_msg=evt.data; messageContainer.innerHTML=messageContainer.innerHTML+"<br/>收到的信息:"+received_msg; } ws.onclose=function () { messageContainer.innerHTML=messageContainer.innerHTML+"<br/> 连接关闭了"; } }else{ messageContainer.innerHTML="浏览器不支持WebSocket" } } </script>
The above code is analyzed as follows:
Client The main body of the page consists of two parts: a Run WebSocket link is used to allow users to start WebSocket; the other
tag with id=message is used to display server-side messages.
Use the JavaScript statement if("WebSocket" in window) to determine whether the current browser supports the WebSocket object.
If the browser supports the WebSocket object, define the instance ws to connect to the WebSocket address of the server and pass in its own identifier parameter. Then define events through js syntax: onopen, onmessage, onclose processing functions. Except for the onopen event where the client sends a message to the server using the WebSocket.send() function, the other events only display the event results in the page
tag.
The operation effect is as follows:
The above is the detailed content of Introduction to WebSocket client programming under Tornado in Python. For more information, please follow other related articles on the PHP Chinese website!