laravel中使用workman進(jìn)行長(zhǎng)連接

workman官方文檔http://doc3.workerman.net/640361

1.將推送的端口號(hào)在服務(wù)器開(kāi)放,阿里云在安全組規(guī)則里進(jìn)行添加

2.本人使用的是寶塔,用寶塔的小伙伴還需要再寶塔面板的安全中,放行端口號(hào)

3.php.ini中的extension=php_sockets.dll 擴(kuò)展打開(kāi),(去掉前面的;即可)

4.查看phpinfo中的socket是否是enable

以上這些確定之后就可以正式開(kāi)始開(kāi)發(fā)socket了~

服務(wù)端

安裝workman

? composer require workerman/workerman

執(zhí)行php artisan

? ?執(zhí)行php artisan make:command orderSocketCommand?在app\Console\Commands生成orderSocketCommand?.php文件

? ??<?php

? ? ?namespace App\Console\Commands;

????use Illuminate\Console\Command;

????use Workerman\Worker;

????use Workerman\Lib\Timer;

????use Log;

????class orderSocketCommand extends Command

????{

? ? ????protected $signature = 'orderSocketCommand {action} {-d?}';#若果加上該選項(xiàng),則以守護(hù)進(jìn)程運(yùn)行

? ? ????protected $description = 'Start a orderSocketCommand server';

? ? ????protected $heartbeatTime = 60;#消息間隔

? ? ????public function __construct() {

? ? ? ? ????parent::__construct();

? ????? }

? ????? public function handle() {

? ? ? ? ????global $argv;

? ? ? ? ????global $worker;

? ? ? ????? // argv[0] 默認(rèn)是當(dāng)前文件,可以不修改

? ? ? ? ????$argv[1] = $argv[2];

? ? ? ? ????$argv[2] = isset($argv[3]) ? "-{$argv[3]}" : '';

? ? ? ????? // 創(chuàng)建一個(gè)Worker監(jiān)聽(tīng)6666端口,不使用任何應(yīng)用層協(xié)議

? ? ? ? ????$worker = new Worker("websocket://0.0.0.0:6666");

? ? ? ? ????$arg = $this->argument('action');

? ? ? ? ????switch ($arg) {

? ? ? ? ? ? ????case 'start':

? ? ? ? ? ? ? ? ????$this->start($worker);

? ? ? ? ? ? ? ????? break;

? ? ? ? ? ? ????case 'stop':

? ? ? ? ? ? ? ? ????$this->stop($worker);

? ? ? ? ? ? ? ? ????break;

? ? ? ????? }

? ????? }

? ? ????private function start($worker) {

? ? ? ? ????// 啟動(dòng)1個(gè)進(jìn)程對(duì)外提供服務(wù)

? ? ? ? ????$worker->count = 1;

? ? ? ? ????// worker進(jìn)程啟動(dòng)后建立一個(gè)內(nèi)部通訊端口

? ? ? ? ????$worker->onWorkerStart = function($worker){

? ? ? ? ? ????? // 每10秒執(zhí)行一次------需要加心跳檢測(cè)的打開(kāi)這個(gè)注釋?zhuān)⑿薷淖约旱倪壿?/p>

? ? ? ? ? ? ????// $timeInterval = 10;

? ? ? ? ? ????? // Timer::add($timeInterval, function() use ($worker) {

? ? ? ? ? ? ????//? ? ?$timeNow = time();

? ? ? ? ? ? ????//? ? ?foreach ($worker->connections as $connection) {

? ? ? ? ? ? ????//? ? ? ? ?// 有可能該connection還沒(méi)收到過(guò)消息,則lastMessageTime設(shè)置為當(dāng)前時(shí)間

? ? ? ? ? ? ????//? ? ? ? ?if (empty($connection->lastMessageTime)) {

? ? ? ? ? ? ????//? ? ? ? ? ? ?$connection->lastMessageTime = $timeNow;

? ? ? ? ? ? ????//? ? ? ? ?}

? ? ? ? ? ? ????//? ? ? ? ?// 上次通訊時(shí)間間隔大于心跳間隔,則認(rèn)為客戶(hù)端已經(jīng)下線(xiàn),關(guān)閉連接

? ? ? ? ? ????? //? ? ? ? ?if ($timeNow - $connection->lastMessageTime > $this->heartbeatTime) {

? ? ? ? ? ????? //? ? ? ? ? ? ?// 檢測(cè)客戶(hù)端是否斷開(kāi),客戶(hù)端是否回應(yīng),未回應(yīng)斷開(kāi)

? ? ? ? ? ????? //? ? ? ? ? ? ?$connection->close();

? ? ? ? ? ????? //? ? ? ? ?}

? ? ? ? ? ? ????//? ? ?}

? ? ? ? ? ????? // });

? ? ? ? ? ????? // 開(kāi)啟一個(gè)內(nèi)部端口,方便內(nèi)部系統(tǒng)推送數(shù)據(jù),Text協(xié)議格式 文本+換行符

? ? ? ? ? ? ????$inner_text_worker = new Worker('text://0.0.0.0:8888');

? ? ? ? ? ? ????$inner_text_worker->onMessage = function($connection, $buffer)

? ? ? ? ? ? ????{

? ? ? ? ? ? ? ? ????global $worker;

? ? ? ? ? ? ? ? ????// $data數(shù)組格式,里面有orderType,表示向那個(gè)orderType的頁(yè)面推送數(shù)據(jù)

? ? ? ? ? ? ? ? ????$data = json_decode($buffer, true);

? ? ? ? ? ? ? ? ????$orderType = $data['orderType'];

? ? ? ? ? ? ? ? ????// 通過(guò)workerman,向orderType的頁(yè)面推送數(shù)據(jù)

? ? ? ? ? ? ? ????? $result = $this->sendMessageByorderType($orderType, $buffer);

? ? ? ? ? ? ? ????? // 返回給客戶(hù)端推送結(jié)果

? ? ? ? ? ? ? ????? $connection->send($result? 'success' : 'fail');

? ? ? ? ? ????? };

? ? ? ? ? ? ????$inner_text_worker->listen();

? ? ? ? ????};

? ? ? ????? // 新增加一個(gè)屬性,用來(lái)保存orderType到connection的映射

? ? ? ? ????$worker->orderTypeConnections = array();

? ? ? ? ????// 當(dāng)有客戶(hù)端發(fā)來(lái)消息時(shí)執(zhí)行的回調(diào)函數(shù)

? ? ? ? ????$worker->onMessage = function($connection, $data)use($worker) {

? ? ? ? ? ????? // 判斷當(dāng)前客戶(hù)端是否已經(jīng)驗(yàn)證,既是否設(shè)置了orderType

? ? ? ? ? ????? if(!isset($connection->orderType)) {

? ? ? ? ? ? ? ? ????Log::info("Command orderSocketCommand 當(dāng)有客戶(hù)端發(fā)來(lái)orderType消息時(shí)執(zhí)行的回調(diào)函數(shù)中的send data:".$data);

? ? ? ? ? ? ? ????? $data = json_decode($data,true);

? ? ? ? ? ? ? ? ????$type = $data['orderType'];

? ? ? ? ? ? ? ? ????$connection->orderType =? $data['uid'] .'-'. $data['orderType'];

? ? ? ? ? ? ? ????? // 保存orderType到connection的映射,這樣可以方便的通過(guò)orderType查找connection,實(shí)現(xiàn)針對(duì)特定orderType推送數(shù)據(jù)

? ? ? ? ? ? ? ????? $worker->orderTypeConnections[$type][$connection->orderType] = $connection;

? ? ? ? ? ? ? ? ????$connection->send(json_encode($data));

? ? ? ? ? ? ? ? ????return;

? ? ? ? ? ????? }else{

? ? ? ? ? ? ? ? ????Log::info("Command orderSocketCommand 客戶(hù)端發(fā)來(lái)的所有消息:".json_encode($data));

? ? ? ? ? ? ? ? ????$connection->send($data);

? ? ? ? ? ? ? ? ????return;

? ? ? ? ? ????? }

? ? ? ????? };

? ? ? ? ????Worker::runAll();

? ? ????}

? ? ????// 針對(duì)orderType推送數(shù)據(jù)

? ????? private function sendMessageByorderType($orderType, $message) {

? ? ? ? ????global $worker;

? ? ? ? ????foreach($worker->orderTypeConnections[$orderType] as $connection) {

? ? ? ? ? ? ????$connection->send($message);

? ? ? ? ? ????? Log::info("Command orderSocketCommand 一個(gè)內(nèi)部端口,方便內(nèi)部系統(tǒng)推送數(shù)據(jù)send data:".json_encode($message));

? ? ? ?????}

? ? ? ?????return true;

? ? ????}

? ? ????private function stop($worker) {

? ? ? ? ????$worker->reloadable = false;

? ? ? ? ????$worker->onClose = function($connection) {

? ? ? ? ? ? ????echo "orderSocketCommand stop";

? ? ? ? ????};

? ? ? ? ????// 運(yùn)行worker

? ? ? ????? Worker::runAll();

? ????? }

????}

Kernel.php中添加

? ???protected $commands = [

? ? ? ? ? ? ? ? ......

????????? ? ? ? Commands\orderSocketCommand::class

????????? ? ];

?運(yùn)行

????????php artisan workerman start d

? ??????

????????Workerman[artisan] start in DEBUG mode

????????----------------------- WORKERMAN -----------------------------

????????Workerman version:3.5.22? ? ? ? ? PHP version:7.1.13

????????------------------------ WORKERS -------------------------------

????????user? ? ? ? ? worker? ? ? ? listen? ? ? ? ? ? ? ? ? ? ? ? ? processes status

????????root? ? ? ? ? none? ? ? ? ? websocket://0.0.0.0:666? ?1? ? ? ? ?[OK]?

????????----------------------------------------------------------------

????????Start success

這樣服務(wù)端就寫(xiě)好了,官方文檔要好好看真的很有用

其中connection的映射這一塊一定要仔細(xì)看官網(wǎng)的例子:

????WorkerMan中如何向某個(gè)特定客戶(hù)端發(fā)送數(shù)據(jù)http://doc3.workerman.net/315238

客戶(hù)端

?將需要的數(shù)據(jù)信息推送到服務(wù)端(我寫(xiě)的數(shù)據(jù)邏輯之后,可以公共調(diào)用)

?<?php

????????namespace App\Models;

????????use Log;

????????class Common{

????????????public static function push($data){

????????????// 建立socket連接到內(nèi)部推送端口

????????????$client = stream_socket_client('tcp://127.0.0.1:8888', $errno, $errmsg, 1);//本地測(cè)試

????????????if (!$client) {

????????????????? ? ? ? ? ? throw new \Exception("{$errstr} ({$errno})", 1);

? ? ? ? ? ? ?}

????????????// 發(fā)送數(shù)據(jù),注意5678端口是Text協(xié)議的端口,Text協(xié)議需要在數(shù)據(jù)末尾加上換行符

????????????fwrite($client, json_encode($data)."\n");

????????????// 讀取推送結(jié)果

????????????$result = fread($client, 4096);

????????????fclose($client);

????????????return $result;

????????}

? ?}

客戶(hù)端接收數(shù)據(jù)

?????var ws = null;

? ? connect();

? ? function connect() {

? ? ? ? // 創(chuàng)建一個(gè) websocket 連接

? ? ? ? ws = new WebSocket("ws://127.0.0.1:6666");//本地測(cè)試

? ? ? ? // websocket 創(chuàng)建成功事件

? ? ? ? ws.onopen = onopen;

? ? ? ? // websocket 接收到消息事件

? ? ? ? ws.onmessage = onmessage;

? ? ? ? ws.onclose = onclose;

? ? ? ? ws.onerror = onerror;

? ? }

?????function onopen(){

? ????????var orderType = document.querySelector("input#type").value;//這個(gè)用來(lái)區(qū)分socket的不同數(shù)據(jù)

? ????????var uid = document.querySelector("input#uid").value;

? ? ? ? ????var data = {'orderType':orderType,'uid':uid}

? ? ? ? ????ws.send(JSON.stringify(data));

? ? ? ? ????heartCheck.reset().start();? ? ? //心跳檢測(cè)重置

? ? }

?????// 接受服務(wù)端數(shù)據(jù)是觸發(fā)事件

? ? function onmessage(e) {

? ????heartCheck.reset().start();? ? ? //拿到任何消息都說(shuō)明當(dāng)前連接是正常的

? ????// socket推送的數(shù)據(jù)屬于string類(lèi)型,將其轉(zhuǎn)換成json格式

? ? ? ? var data = e.data;

? ? ? ? data = JSON.parse(data);

? ? ? ? if (data.hasOwnProperty('uid')) {

????????? console.log('不是所需要的數(shù)據(jù)')

? ? ? ? }else {

????????????? if (typeof(data) != 'number') {//心跳檢測(cè)的數(shù)據(jù)是999,這是不需要的數(shù)據(jù)

? ? ? ? ? ? ? var _token=$('meta[name=token]').attr('content');

? ????????????$.ajax({

????????????????????type: "post",

????????????????????url: "/sys/order/deal_data",

????????????????????data: {'_token':_token,'type':data.orderType},

????????????????????dataType: 'JSON',

????????????????????success: function(json) {

????????????????????????? var html = ''

? ? ? ? ? ? ? ? ? ? ? ? ? for (var i = 0; i < json.data.length; i++) {

????????????????????????????html += ``

????????????????????????????if (json.data[i].users == null) {

? ????????????????????????????html += ``

????????????????????????????}else{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? html += `` + json.data[i].users['username'] + `

????????????????????????????}

????????????????????????????html += `` + json.data[i].number + ``

????????????????????}

????????????????????var content = document.querySelector('#content');

????????????????????content.innerHTML = html

????????????????????// 自動(dòng)播放鈴聲

????????????????????var mp3 = "/music/test.mp3";

????????????????????var mp3 = new Audio(mp3);

????????????????????mp3.play(); //播放音樂(lè)提醒有新訂單

????????????????}

????????????});

? ? ? ? ?}

?????}

}

function onclose(){}

function onerror(){}

?//心跳檢測(cè)

var heartCheck = {

????timeout: 300000,? ? ? ? //五分鐘發(fā)一次心跳

????timeoutObj: null,

????serverTimeoutObj: null,

????reset: function(){

????clearTimeout(this.timeoutObj);

????clearTimeout(this.serverTimeoutObj);

????return this;

},

start: function(){

????var self = this;

????this.timeoutObj = setTimeout(function(){

????//這里發(fā)送一個(gè)心跳,后端收到后,返回一個(gè)心跳消息,

????//onmessage拿到返回的心跳就說(shuō)明連接正常

????ws.send("9999");

????self.serverTimeoutObj = setTimeout(function(){//如果超過(guò)一定時(shí)間還沒(méi)重置,說(shuō)明后端主動(dòng)斷開(kāi)了

????ws.close();? ? ?//如果onclose會(huì)執(zhí)行reconnect,我們執(zhí)行ws.close()就行了.如果直接執(zhí)行reconnect 會(huì)觸發(fā)onclose導(dǎo)致重連兩次

????}, self.timeout)

????}, this.timeout)

}

}

這樣客戶(hù)端接收到數(shù)據(jù)的時(shí)候就會(huì)播放指定提示音了~

客戶(hù)端的心跳檢測(cè)參考:http://www.lxweimin.com/p/1141dcf6de3e

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 由于自己寫(xiě)了一個(gè)博客,一直對(duì)workman有想法的我,突發(fā)奇想的想在博客里面弄個(gè)聊天室,因此就研究了一下workm...
    youngxs閱讀 4,322評(píng)論 0 0
  • 我的博客文章網(wǎng)址: http://www.jloongking.cn/tp50/public/blog/index...
    非甲即丁閱讀 6,814評(píng)論 0 4
  • _________________________________________________________...
    fastwe閱讀 649評(píng)論 0 0
  • Web存儲(chǔ)API 對(duì)于任何程序來(lái)說(shuō),能夠?qū)崿F(xiàn)數(shù)據(jù)存儲(chǔ)是必備功能之一,并且在需要的時(shí)候能夠提供數(shù)據(jù)。但在過(guò)去的Web...
    云音流閱讀 968評(píng)論 0 1
  • Workerman Manual Workerman 不依賴(lài)于 php-fpm、apache、nginx 容器,這...
    xiaojianxu閱讀 972評(píng)論 1 1