本篇文章给大家带来的内容是关于Python中Tornado下WebSocket客户端编程的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
由于WebSocket是HTML5的标准之一,所以主流浏览器的Web客户端编程语音JavaScript已经支持WebSocket的客户端编程。
客户端编程围绕着WebSocket对象展开,在JavaScript中可以通过如下代码初始化WebSocket对象:
var Socket=new WebSocket(url);
在代码中只需给WebSocket构造函数传入服务器的URL地址,比如http://mysite.com/point.
可以为该对象的如下事件指定处理函数以相应它们:
WebSocket.onopen:此事件发生在WebSocket链接建立时。
WebSocket.onmessage:此事件发生在收到了来自服务器的消息时。
WebSocket.onerror:此事件发生在通信过程中有任何错误时。
WebSocket.onclose:此事件发生在服务器的链接关闭时。
除了这些事件处理函数,还可以通过WebSocket对象的两个方法进行主动操作:
WebSocket.send(data):向服务器发送消息。
WebSocket.close():主动关闭现有链接。
客户端WebSocket编程实例程序如下:index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebSocket</title> </head> <body> <a href="javascript:WebSocketTest()">运行WebSocket</a> <p id="messages" style="height: 200px;background: black;color:white"></p> </body> <script type="text/javascript"> 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> </html>
对上述代码解析如下:
客户端页面主体是有两部分组成:一个Run WebSocket链接用于让用户启动WebSocket;另一个id=message的
标签用于显示服务器端的消息。
使用JavaScript语句if("WebSocket" in window)可以判断当前浏览器是否支持WebSocket对象。
如何浏览器支持WebSocket对象,则定义实例ws链接到服务器的WebSocket地址,并传入自己的标识符参数。然后通过js语法定义事件:onopen、onmessage、onclose的处理函数。除了在onopen事件中客户端向服务器用WebSocket.send()函数发送了消息,其余事件均只将事件结果显示在页面
标签中。
运行效果如下:
以上是Python中Tornado下WebSocket客户端编程的介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。