由於本文的能力有限,有好多聊天邏輯的細節沒有實現,只實現了群發,具體程式碼如下所示:
php程式碼:
$serv = new swoole_websocket_server("127.0.0.1",3999); //服务的基本设置 $serv->set(array( 'worker_num' => 2, 'reactor_num'=>8, 'task_worker_num'=>1, 'dispatch_mode' => 2, 'debug_mode'=> 1, 'daemonize' => true, 'log_file' => __DIR__.'/log/webs_swoole.log', 'heartbeat_check_interval' => 60, 'heartbeat_idle_time' => 600, )); $serv->on('connect', function ($serv,$fd){ // echo "client:$fd Connect.".PHP_EOL; }); //测试receive $serv->on("receive",function(swoole_server $serv,$fd,$from_id,$data){ // echo "receive#{$from_id}: receive $data ".PHP_EOL; }); $serv->on('open', function($server, $req) { // echo "server#{$server->worker_pid}: handshake success with fd#{$req->fd}".PHP_EOL;; // echo PHP_EOL; }); $serv->on('message',function($server,$frame) { // echo "message: ".$frame->data.PHP_EOL; $msg=json_decode($frame->data,true); switch ($msg['type']){ case 'login': $server->push($frame->fd,"欢迎欢迎~"); break; default: break; } $msg['fd']=$frame->fd; $server->task($msg); }); $serv->on("workerstart",function($server,$workerid){ // echo "workerstart: ".$workerid.PHP_EOL; // echo PHP_EOL; }); $serv->on("task","on_task"); $serv->on("finish",function($serv,$task_id,$data){ return ; }); $serv->on('close', function($server,$fd,$from_id) { // echo "connection close: ".$fd.PHP_EOL; // echo PHP_EOL; }); $serv->start(); function on_task($serv,$task_id,$from_id,$data) { switch ($data['type']){ case 'login': $send_msg="说:我来了~"; break; default: $send_msg="说:{$data['msg']['speak']}"; break; } foreach ($serv->connections as $conn){ if ($conn!=$data['fd']){ if (strpos($data['msg']['name'],"游客")===0){ $name=$data['msg']['name']."_".$data['fd']; }else{ $name=$data['msg']['name']; } }else{ $name="我"; } $serv->push($conn,$name.$send_msg); } return; } function on_finish($serv,$task_id,$data){ return true; }
前端程式碼:安裝swoole擴充的方法 使用swoole擴充php websocket範例
以上程式碼給大家分享了PHP+swoole實現簡單多人線上聊天群發的相關程式碼,希望本文所述對大家有所幫助。
以上就介紹了PHP+swoole實作簡單多人線上聊天群發,包含了方面的內容,希望對PHP教學有興趣的朋友有所幫助。