ThinkPHP使用Swoole須要安裝 think-swoole Composer包,前提系統已經安裝好了Swoole PECL 拓展*javascript
tp5的項目根目錄下執行composer命令安裝think-swoole:php
composer require topthink/think-swoole
話很少說,直接上代碼:css
新建WebSocket.php控制器html
(監聽端口要確認服務器放行,寶塔環境還須要添加安全組規則)前端
1 <?php 2 3 namespace app\home\controller; 4 use think\swoole\Server; 5 class WebSocket extends Server 6 { 7 protected $host = '0.0.0.0'; //監聽全部地址 8 protected $port = 9501; //監聽9501端口 9 protected $serverType = 'socket'; 10 protected $option = [ 11 'worker_num'=> 4, //設置啓動的Worker進程數 12 'daemonize' => false, //守護進程化(上線改成true) 13 'backlog' => 128, //Listen隊列長度 14 'dispatch_mode' => 2, //固定模式,保證同一個鏈接發來的數據只會被同一個worker處理 15 16 //心跳檢測:每60秒遍歷全部鏈接,強制關閉10分鐘內沒有向服務器發送任何數據的鏈接 17 'heartbeat_check_interval' => 60, 18 'heartbeat_idle_time' => 600 19 ]; 20 21 //創建鏈接時回調函數 22 public function onOpen($server,$req) 23 { 24 $fd = $req->fd;//客戶端標識 25 $uid = $req->get['uid'];//客戶端傳遞的用戶id 26 $token = $req->get['token'];//客戶端傳遞的用戶登陸token 27 28 //省略token驗證邏輯...... 29 if (!$token) { 30 $arr = array('status'=>2,'message'=>'token已過時'); 31 $server->push($fd, json_encode($arr)); 32 $server->close($fd); 33 return; 34 } 35 //省略給用戶綁定fd邏輯...... 36 echo "用戶{$uid}創建了鏈接,標識爲{$fd}\n"; 37 } 38 39 //接收數據時回調函數 40 public function onMessage($server,$frame) 41 { 42 $fd = $frame->fd; 43 $message = $frame->data; 44 45 //省略經過fd查詢用戶uid邏輯...... 46 $uid = 666; 47 $data['uid'] = $uid; 48 $data['message'] = '用戶'.$uid.'發送了:'.$message; 49 $data['post_time'] = date("m/d H:i",time()); 50 $arr = array('status'=>1,'message'=>'success','data'=>$data); 51 52 //僅推送給當前鏈接用戶 53 //$server->push($fd, json_encode($arr)); 54 55 //推送給所有鏈接用戶 56 foreach($server->connections as $fd) { 57 $server->push($fd, json_encode($arr)); 58 } 59 } 60 61 //鏈接關閉時回調函數 62 public function onClose($server,$fd) 63 { 64 echo "標識{$fd}關閉了鏈接\n"; 65 } 66 }
前端演示頁面:java
(省略控制器判斷登陸狀態、分配數據邏輯......)jquery
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8" /> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 6 <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> 7 <title>Chat</title> 8 <link rel="stylesheet" type="text/css" href="/static/liaotian/chat.css" /> 9 <script src="/static/liaotian/js/jquery.min.js"></script> 10 <script src="/static/liaotian/js/flexible.js"></script> 11 </head> 12 <body> 13 <header class="header"> 14 <a class="back" href="javascript:history.back()"></a> 15 <h5 class="tit">在線聊天</h5> 16 <a href=""><div class="right">退出</div></a> 17 </header> 18 19 <!-- 聊天內容 start--> 20 <div class="message"> </div> 21 <!-- 聊天內容 end--> 22 23 <!-- 底部 start--> 24 <div class="footer"> 25 <img id="setbtn" src="/static/liaotian/images/hua.png" alt="" /> 26 <img src="/static/liaotian/images/xiaolian.png" alt="" /> 27 <input type="text" id="msg" value="" maxlength="300"> 28 <p style="background: rgb(17, 79, 142);" id="sendBtn">發送</p> 29 </div> 30 <!-- 底部 end--> 31 </body> 32 </html> 33 <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 34 <script src="https://cdn.bootcss.com/layer/3.1.0/layer.js"></script> 35 <script type="text/javascript"> 36 $(function () { 37 var uid = 666;//當前用戶id 38 var token = 'abcdefg';//用戶token 39 40 //判斷瀏覽器是否支持WebSocket 41 var supportsWebSockets = 'WebSocket' in window || 'MozWebSocket' in window; 42 if (supportsWebSockets) { 43 //創建WebSocket鏈接(ip地址換成本身主機ip) 44 var ws = new WebSocket("ws://127.0.0.1:9501?uid="+uid+"&token="+token); 45 ws.onopen = function () { 46 layer.msg('服務器鏈接成功',{shade:0.1,icon:1,time:600}); 47 }; 48 ws.onerror = function () { 49 layer.msg('服務器鏈接失敗',{shade:0.1,icon:2,time:600}); 50 }; 51 ws.onmessage = function (evt) { 52 var data = $.parseJSON(evt.data); 53 //錯誤提示 54 if(data.status != 1){ 55 layer.alert(data.message,{icon:2}); 56 return; 57 } 58 //消息返回 59 if (data.status==1 && data.data.message!='') { 60 var html = ""; 61 if (data.data.uid == uid) { 62 html += "<div style='word-break:break-all' class=\"show\"><div class=\"time\">"+data.data.post_time+"</div><div class=\"msg\"><img src=\""+data.data.head_img+"\" alt=\"\" /><p><i clas=\"msg_input\"></i>"+data.data.message+"</p></div></div>"; 63 }else{ 64 html += "<div style='word-break:break-all' class=\"send\"><div class=\"time\">"+data.data.post_time+"</div><div class=\"msg\"><img src=\""+data.data.head_img+"\" alt=\"\" /><p><i clas=\"msg_input\"></i>"+data.data.message+"</p></div></div>"; 65 } 66 } 67 $(".message").append(html); 68 setTimeout(function () { 69 ($('.message').children("div:last-child")[0]).scrollIntoView();//向上滾動 70 },100); 71 }; 72 ws.onclose = function (res) { 73 74 }; 75 //按鈕發送 76 $("#sendBtn").click(function () { 77 var contents = $("#msg").val().trim(); 78 if(contents == null || contents == ""){ 79 layer.msg('內容爲空',{shade:0.1,icon:2,time:600}); 80 return false; 81 }else{ 82 ws.send(contents); 83 $("#msg").val(""); 84 } 85 }); 86 //回車發送 87 $("#msg").keydown(function (evel) { 88 var that = $(this); 89 if (evel.keyCode == 13) { 90 evel.cancelBubble = true; 91 evel.preventDefault(); 92 evel.stopPropagation(); 93 var contents = that.val().trim(); 94 if(contents == null || contents == ""){ 95 layer.msg('內容爲空',{shade:0.1,icon:2,time:600}); 96 return false; 97 }else{ 98 ws.send(contents); 99 that.val(""); 100 } 101 } 102 }); 103 }else{ 104 layer.alert("您的瀏覽器不支持 WebSocket!"); 105 } 106 }); 107 </script> 108
服務器移到項目根目錄開啓服務:laravel
php public/index.php Websocket/start
這裏的路徑,是由於我綁定了home模塊爲默認模塊,tp5默認狀況是:php public/index.php index/Websocket/start)sql
開啓成功,查看端口已經被監聽:chrome
lsof -i:9501
phper在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那裏入手去提高,對此我整理了一些資料,包括但不限於:分佈式架構、高可擴展、高性能、高併發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階乾貨須要的能夠免費分享給你們,須要的(點擊→)個人官方羣677079770