成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的臨澤網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
laravel使用workerman 用戶(hù)交互、服務(wù)器交互
使用workeman實(shí)現(xiàn)瀏覽器相互通信、服務(wù)器瀏覽器交互
composer require workerman/workerman
php artisan make:command Workerman
修改文件
argument('action'); $argv[1] = $arg; $argv[2] = $this->option('daemonize') ? '-d' : '';//該參數(shù)是以daemon(守護(hù)進(jìn)程)方式啟動(dòng) global $text_worker; // 創(chuàng)建一個(gè)Worker監(jiān)聽(tīng)2345端口,使用websocket協(xié)議通訊 $text_worker = new Worker("websocket://0.0.0.0:2345"); $text_worker->uidConnections = array();//在線用戶(hù)連接對(duì)象 $text_worker->uidInfo = array();//在線用戶(hù)的用戶(hù)信息 // 啟動(dòng)4個(gè)進(jìn)程對(duì)外提供服務(wù) $text_worker->count = 4; //當(dāng)啟動(dòng)workerman的時(shí)候 觸發(fā)此方法 $text_worker->onWorkerStart =function(){ }; //當(dāng)瀏覽器連接的時(shí)候觸發(fā)此函數(shù) $text_worker->onConnect = function($connection){ }; //向用戶(hù)發(fā)送信息的時(shí)候觸發(fā) //$connection 當(dāng)前連接的人的信息 $data 發(fā)送的數(shù)據(jù) $text_worker->onMessage = function($connection,$data){ }; //瀏覽器斷開(kāi)鏈接的時(shí)候觸發(fā) $text_worker->onClose = function($connection){}; }}
$ php artisan Workerman start --daemonize Deprecated: Directive 'track_errors' is deprecated in Unknown on line 0----------------------- WORKERMAN -----------------------------Workerman version:4.0.19 PHP version:7.2.9------------------------ WORKERS -------------------------------worker listen processes status none websocket://0.0.0.0:2345 4 [ok]
var socket = new WebSocket("ws://localhost:2345//ws"); // 建立連接時(shí)觸發(fā) 建立鏈接的時(shí)候,需要向workerman發(fā)送一條指令,告訴他我是誰(shuí),使用id或者用戶(hù)標(biāo)識(shí)作為uid,告訴workerman 例如,當(dāng)前html 用戶(hù)id是37 socket.onopen = function(event) { console.log('連接開(kāi)始...'); socket.send('{"uid":36,"type":'login'}'); } //workerman發(fā)送消息的時(shí)候,接收并打印 socket.onmessage = function(event) { var msg = event.data; console.log(msg ); }
瀏覽器發(fā)來(lái)了用戶(hù)uid,需要workerman保留一下,網(wǎng)上有文檔說(shuō)在觸發(fā)的時(shí)候保存,還有用session的,我試驗(yàn)了沒(méi)成功,所有用瀏覽器建立鏈接的時(shí)候,向workerman發(fā)送一條信息來(lái)創(chuàng)建uid,在workerman上接收一下
//$connection 當(dāng)前連接的人的信息 $data 發(fā)送的數(shù)據(jù)$text_worker->onMessage = function($connection,$data){ $data = json_decode($data); if($data['type']=='login'){ $this->create_uid($connection,$data); }};//創(chuàng)建uid方法 public function create_uid($connection,$data){ global $text_worker; $connection->uid = $data['uid']; //保存用戶(hù)的uid $text_worker->uidConnections["{$connection->uid}"] = $connection; //向自己的瀏覽器返回創(chuàng)建成功的信息 $connection->send("用戶(hù):[{$connection->uid}] 創(chuàng)建成功"); }
這時(shí)候?yàn)g覽器就會(huì)出現(xiàn)打印信息
向用戶(hù)是37的瀏覽器發(fā)送信息
//js代碼 socket.send('{"type":"login","to_uid":36,"uid":36,"message":"nihao"}'); //workerman //$connection 當(dāng)前連接的人的信息 $data 發(fā)送的數(shù)據(jù) $text_worker->onMessage = function($connection,$data){ $data = json_decode($data,true); var_dump($data); if($data['type']=='login'){ $this->create_uid($connection,$data); } if($data['type']=='send_message'){ $this->send_message($connection,$data); } }; public function send_message($connection,$data){ global $text_worker; if(isset($data['to_uid'])){ var_dump($data['to_uid']); if(isset($text_worker->uidConnections["{$data['to_uid']}"])){ $to_connection=$text_worker->uidConnections["{$data['to_uid']}"]; $to_connection->send($data['uid'].$data['message']); } } }
//當(dāng)啟動(dòng)workerman的時(shí)候 觸發(fā)此方法 $text_worker->onWorkerStart =function(){ //監(jiān)聽(tīng)一個(gè)內(nèi)部端口,用來(lái)接收服務(wù)器的消息,轉(zhuǎn)發(fā)給瀏覽器 $inner_text_worker = new Worker('Text://127.0.0.1:5678'); $inner_text_worker->onMessage = function($connection_admin, $data) { global $text_worker; // $data數(shù)組格式,里面有uid,表示向那個(gè)uid的頁(yè)面推送數(shù)據(jù) $data = json_decode($data, true); var_dump($data); $to_uid = $data['to_uid']; var_dump($to_uid); // 通過(guò)workerman,向uid的頁(yè)面推送數(shù)據(jù) // $ret = sendMessageByUid($uid, $buffer); $connection = $text_worker->uidConnections[$to_uid]; $connection->send($buffer); // 返回推送結(jié)果 $connection_admin->send(true ? 'ok' : 'fail'); }; $inner_text_worker->listen(); };//控制器代碼class TestController extends Controller{ public function send(){ $client = stream_socket_client('tcp://127.0.0.1:5678', $errno, $errmsg, 1); // 推送的數(shù)據(jù),包含用戶(hù),表示是給這個(gè)用戶(hù)推送 $data = array('uid'=>37,'group'=>'admin', 'message'=>'發(fā)送成功啦'); // 發(fā)送數(shù)據(jù),注意5678端口是Text協(xié)議的端口,Text協(xié)議需要在數(shù)據(jù)末尾加上換行符 fwrite($client, json_encode($data)."\n");}}