php实现socket推送技术的示例(2)

<html> <head> <title>demo</title> <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script> </head> <body> <input type="text"> <input type="button" value="send"> <script type="text/javascript"> var ws = new WebSocket("ws://localhost:4000"); ws.onopen = function(){ console.log("握手成功"); } ws.onmessage = function(e){ console.log("message:" + e.data); } ws.onerror = function(){ console.log("error"); } $("#send").click(function(){ content = $("#content").val(); console.log(content); ws.send(content); }) </script> </body> </html>

然后执行php demo.php 开启socket(从运维那偷学一招,linux下执行nohup php demo.php &可以在后台执行),浏览器打开多个index.html,就能建立通讯了。

代码解析:

1.属性$sockets数组保存每个accept连接(不知道这么描述对不对);

2.属性$handshake数组保存连接是否在连接状态;

以上这篇php实现socket推送技术的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/89a15a492b3298ea9560f98d36854997.html