您好,登錄后才能下訂單哦!
laravel使用workerman 用戶交互、服務器交互
使用workeman實現瀏覽器相互通信、服務器瀏覽器交互
composer require workerman/workerman
php artisan make:command Workerman
修改文件
<?php namespace App\Console\Commands;use Illuminate\Console\Command;use Workerman\Worker;class Workerman extends Command{ protected $signature = 'Workerman {action} {--daemonize}'; protected $description = 'Command description'; public function __construct() { parent::__construct(); } public function handle() { global $argv;//定義全局變量 $arg = $this->argument('action'); $argv[1] = $arg; $argv[2] = $this->option('daemonize') ? '-d' : '';//該參數是以daemon(守護進程)方式啟動 global $text_worker; // 創建一個Worker監聽2345端口,使用websocket協議通訊 $text_worker = new Worker("websocket://0.0.0.0:2345"); $text_worker->uidConnections = array();//在線用戶連接對象 $text_worker->uidInfo = array();//在線用戶的用戶信息 // 啟動4個進程對外提供服務 $text_worker->count = 4; //當啟動workerman的時候 觸發此方法 $text_worker->onWorkerStart =function(){ }; //當瀏覽器連接的時候觸發此函數 $text_worker->onConnect = function($connection){ }; //向用戶發送信息的時候觸發 //$connection 當前連接的人的信息 $data 發送的數據 $text_worker->onMessage = function($connection,$data){ }; //瀏覽器斷開鏈接的時候觸發 $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"); // 建立連接時觸發 建立鏈接的時候,需要向workerman發送一條指令,告訴他我是誰,使用id或者用戶標識作為uid,告訴workerman 例如,當前html 用戶id是37 socket.onopen = function(event) { console.log('連接開始...'); socket.send('{"uid":36,"type":'login'}'); } //workerman發送消息的時候,接收并打印 socket.onmessage = function(event) { var msg = event.data; console.log(msg ); }
瀏覽器發來了用戶uid,需要workerman保留一下,網上有文檔說在觸發的時候保存,還有用session的,我試驗了沒成功,所有用瀏覽器建立鏈接的時候,向workerman發送一條信息來創建uid,在workerman上接收一下
//$connection 當前連接的人的信息 $data 發送的數據$text_worker->onMessage = function($connection,$data){ $data = json_decode($data); if($data['type']=='login'){ $this->create_uid($connection,$data); }};//創建uid方法 public function create_uid($connection,$data){ global $text_worker; $connection->uid = $data['uid']; //保存用戶的uid $text_worker->uidConnections["{$connection->uid}"] = $connection; //向自己的瀏覽器返回創建成功的信息 $connection->send("用戶:[{$connection->uid}] 創建成功"); }
這時候瀏覽器就會出現打印信息
向用戶是37的瀏覽器發送信息
//js代碼 socket.send('{"type":"login","to_uid":36,"uid":36,"message":"nihao"}'); //workerman //$connection 當前連接的人的信息 $data 發送的數據 $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']); } } }
//當啟動workerman的時候 觸發此方法 $text_worker->onWorkerStart =function(){ //監聽一個內部端口,用來接收服務器的消息,轉發給瀏覽器 $inner_text_worker = new Worker('Text://127.0.0.1:5678'); $inner_text_worker->onMessage = function($connection_admin, $data) { global $text_worker; // $data數組格式,里面有uid,表示向那個uid的頁面推送數據 $data = json_decode($data, true); var_dump($data); $to_uid = $data['to_uid']; var_dump($to_uid); // 通過workerman,向uid的頁面推送數據 // $ret = sendMessageByUid($uid, $buffer); $connection = $text_worker->uidConnections[$to_uid]; $connection->send($buffer); // 返回推送結果 $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); // 推送的數據,包含用戶,表示是給這個用戶推送 $data = array('uid'=>37,'group'=>'admin', 'message'=>'發送成功啦'); // 發送數據,注意5678端口是Text協議的端口,Text協議需要在數據末尾加上換行符 fwrite($client, json_encode($data)."\n");}}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。