Server:
? <PHP class Chat { const the HOST = '0.0.0.0'; // ip address ip 0.0.0.0 Representative accept all access const PART = 8080; // port number private $ server = null; // singleton objects stored websocket_server the __construct function public () { on // instantiate swoole_websocket_server and stored in our Chat class attributes, to design a single embodiment of $ this-> = new new swoole_websocket_server Server (Self :: the HOST, the PART :: Self); // listener connection event $ this-> Server-> ON ( 'Open', [$ the this, 'the onOpen']); // monitor received event message $ this-> server-> on ( ' message', [$ this, 'onMessage ']); // listen for close event $ this-> Server-> ON (' use Close ', [$ the this,' onClose ']); // settings allow access to static files $ this-> server-> set ([ 'document_root' => '/ www / chat1', // where the incoming directory static files 'enable_static_handler' => true // allow access to static files ]); // start the service $ this-> server-> start () ; } / ** * successful connection callback * @param Server $ * @param Request $ * / public function the onOpen (Server $, $ Request) { . echo $ request-> FD 'connected' PHP_EOL;. // Print our terminal } / ** * callback function receives information * @param Server $ * @param Frame $ * / public function the onMessage (Server $, $ Frame) { echo Frame-$> FD 'came and said:' $ frame-> data PHP_EOL; /... / print to our terminal the foreach ($ Server-> Connections AS $ FD) {// TCP connection traversal iterator, take to each online client ID // the message sent by the client, pushed to all users, can also be called broadcast to all online clients $ server-> push ($ fd, json_encode ([ 'no' => $ Frame-> FD, 'MSG' => $ Frame-> Data])); } } / ** * disconnect callback * @param Server $ * @param $ FD * / public function the onClose (Server $, $ FD) { . echo $ FD 'go' PHP_EOL;. // print our terminal } } $ obj = new new Chat ();
Client
<!doctype html> <html> <head> <meta charset="utf-8"> <title>聊天室</title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> </head> <body> <textarea class="log" style="width: 100%; height: 500px;"> =======聊天室====== </textarea> <input type="button" value="连接" onClick="link()"> <input type="button" value="断开" onClick="dis()"> <input type="text" id="text"> <input type="button" value="发送" onClick="send()"> <script> function link(){ var url='ws://192.168.33.60:8080'; socket=new WebSocket(url); socket.onopen=function(){log1('连接成功')} socket.onmessage=function(msg){log(msg.data);console.log(msg);} socket.onclose=function(){log1('断开连接')} } function dis(){ socket.close(); socket=null; } function log1(var1) { $('.log').append(var1+'\r\n'); } function log(var1){ var v=$.parseJSON(var1) $('.log').append('用户'+v['no']+'说:'+v['msg']+'\r\n'); } function send(){ var text=$('#text').val(); socket.send(text); } function send2(){ var json = JSON.stringify({'type':'php','msg':$('#text2').attr('value')}) socket.send(json); } </script> </body> </html>
Start the server
php Chat.php
Access to the client's browser to open the html
Reference https://www.jianshu.com/p/ac77f05bee56