小編給大家分享一下怎么使用workerman進(jìn)行消息推送,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)建站專注于和碩企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。和碩網(wǎng)站建設(shè)公司,為和碩等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站建設(shè),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
Workerman是一款純PHP開發(fā)的開源高性能的PHP socket 服務(wù)器框架。被廣泛的用于手機(jī)app、移動(dòng)通訊,微信小程序,手游服務(wù)端、網(wǎng)絡(luò)游戲、PHP聊天室、硬件通訊、智能家居、車聯(lián)網(wǎng)、物聯(lián)網(wǎng)等領(lǐng)域的開發(fā)。
支持TCP長連接,支持Websocket、HTTP等協(xié)議,支持自定義協(xié)議。擁有異步MySQL、異步redis、異步Http、異步消息隊(duì)列等眾多高性能組件。與之類似的還有swoole,MeepoPS。
首先下載workerman的Web消息推送系統(tǒng) web-msg-sender。
# wget http://www.workerman.net/download/senderzip # unzip senderzip #cd web-msg-sender #vim start.php
use Workerman\Worker; // composer 的 autoload 文件 include __DIR__ . '/vendor/autoload.php'; if(strpos(strtolower(PHP_OS), 'win') === 0) { exit("start.php not support windows, please use start_for_win.bat\n"); } // 標(biāo)記是全局啟動(dòng) define('GLOBAL_START', 1); // 加載IO 和 Web require_once __DIR__ . '/start_io.php'; 可以注釋掉 webServer 服務(wù) 沒什么用 省點(diǎn)資源 // require_once __DIR__ . '/start_web.php'; // 運(yùn)行所有服務(wù) Worker::runAll();
保存
#vim start_io.php 找到 將端口改成你要監(jiān)聽的端口 我是2120 記住要在安全組里入方向添加白名單 // PHPSocketIO服務(wù) $sender_io = new SocketIO(2120); 服務(wù)端設(shè)置完畢后 #php start.php start -d //開啟服務(wù) 并保持進(jìn)程
推送類 我用的tp5
<?php namespace app\index\moudel; /** * 推送事件 * 典型調(diào)用方式: * $push = new WebSocket(); * $push->setUser($user_id)->setContent($string)->push();//連貫操作 * * Class WebSocket * @package app\index\moudel; */ class WebSocket { /** * @var string 目標(biāo)用戶id */ protected $to_user = ''; /** * @var string 推送服務(wù)地址 */ protected $push_api_url = 'http://127.0.0.1:2000'; /** * @var string 推送內(nèi)容 */ protected $content = ''; /** * 設(shè)置推送用戶,若參數(shù)留空則推送到所有在線用戶 * * @param string $user * @return $this */ public function setUser($user = '') { $this->to_user = $user ? : ''; return $this; } /** * 設(shè)置推送內(nèi)容 * * @param string $content * @return $this */ public function setContent($content = '') { $this->content = $content; return $this; } /** * 推送 */ public function push() { $data = [ 'type' => 'publish', 'content' => $this->content, 'to' => $this->to_user, ]; // var_dump($data); // var_dump($this->push_api_url); $ch = curl_init (); curl_setopt($ch, CURLOPT_URL, $this->push_api_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); $res = curl_exec($ch); curl_close($ch); dump($res); } }
操作控制器
<?php namespace app\index\controller; use think\Controller; use app\index\moudel\WebSocket; class Index extends Controller { /** * 推送一個(gè)字符串 */ public function push_msg(){ $uid = input('uid','');//uid為空的時(shí)候推送給所有用戶 $string = '這是一個(gè)推送的測試'; $string = input('msg') ? : $string; $push = new WebSocket(); $push->setUser($uid)->setContent($string)->push(); } /** * 推送目標(biāo)頁 * * @return \think\response\View */ public function targetPage(){ return view(); } }
推送目標(biāo)的前端顯示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <strong id="count"></strong> <h2 id="target"></h2> </body> </html> <script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script> <script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script> <script> jQuery(function ($) { // 連接服務(wù)端 var socket = io('http://39.106.132.216:2000/'); //這里當(dāng)然填寫真實(shí)的地址了 // uid可以是自己網(wǎng)站的用戶id,以便針對uid推送以及統(tǒng)計(jì)在線人數(shù),但一定是唯一標(biāo)識 uid = 321; // socket連接后以uid登錄 socket.on('connect', function () { socket.emit('login', uid); }); // 后端推送來消息時(shí) socket.on('new_msg', function (msg) { console.log("收到消息:" + msg); $('#target').append(msg).append('<br>'); }); // 后端推送來在線數(shù)據(jù)時(shí) socket.on('update_online_count', function (online_stat) { console.log(online_stat); $('#count').html(online_stat); }); }) </script>
http://我自己的域名/index/index/pushAString?uid=123 ok 為推送成功 offline 為未在線 fail 為失敗
前端成功展示 321為我自定義的uid
看完了這篇文章,相信你對“怎么使用workerman進(jìn)行消息推送”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:怎么使用workerman進(jìn)行消息推送
本文網(wǎng)址:http://bm7419.com/article16/gocgdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站導(dǎo)航、定制網(wǎng)站、Google、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)