PHP实现websocket通信的方法示例

前端之家收集整理的这篇文章主要介绍了PHP实现websocket通信的方法示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP实现websocket通信的方法分享给大家供大家参考,具体如下:

执行方法:

修改server.PHP与index.html的ip 通过命令行执行 PHP路径]\PHP.exe "[文件路径]\server.PHP" 然后通过浏览器打开index.html

server.PHP

PHP;"> '192.168.0.200','port'=>'8000','event'=>'WSevent',//回调函数函数名 'log'=>true,); $websocket = new websocket($config); $websocket->run(); function WSevent($type,$event){ global $websocket; if('in'==$type){ $websocket->log('客户进入id:'.$event['k']); }elseif('out'==$type){ $websocket->log('客户退出id:'.$event['k']); }elseif('msg'==$type){ $websocket->log($event['k'].'消息:'.$event['msg']); roboot($event['sign'],$event['msg']); } } function roboot($sign,$t){ global $websocket; switch ($t) { case 'hello': $show='hello,GIt @ OSC'; break; case 'name': $show='Robot'; break; case 'time': $show='当前时间:'.date('Y-m-d H:i:s'); break; case '再见': $show='( ^_^ )/~~拜拜'; $websocket->write($sign,'Robot:'.$show); $websocket->close($sign); return; break; case '天王盖地虎': $array = array('小鸡炖蘑菇','宝塔震河妖','粒粒皆辛苦'); $show = $array[rand(0,2)]; break; default: $show='( ⊙o⊙?)不懂,你可以尝试说:hello,name,time,再见,天王盖地虎.'; } $websocket->write($sign,'Robot:'.$show); } ?>

index.html

<Meta charset="utf-8"> websocket_TEST