這篇文章主要介紹了thinkphp 6.0 swoole擴(kuò)展websocket的使用方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司是一家網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,提供網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需網(wǎng)站制作,網(wǎng)站開發(fā)公司,成立與2013年是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開發(fā),后臺程序制作以及后期項(xiàng)目運(yùn)營并提出專業(yè)建議和思路。
即將推出的tp6.0,已經(jīng)適配swoole.并推出think-swoole 3.0,并且默認(rèn)適配了socketio。和2.0版本在使用方法上面有些許不同。
Websocket 繼承與Http,進(jìn)行websocket連接之前需要一次HTTP請求,如果當(dāng)期地址支持websocket則返回101,然后進(jìn)行連接。也就是說并不是我的服務(wù)支持websocket后,請求每個連接地址都可以進(jìn)行websocket連接,而是需要預(yù)先適配才可以連接。
如果要使用websocket需要在配置中啟用,將websocket下的enable設(shè)置為true
'server' => [ 'host' => '0.0.0.0', // 監(jiān)聽地址 'port' => 808, // 監(jiān)聽端口 'mode' => SWOOLE_PROCESS, // 運(yùn)行模式 默認(rèn)為SWOOLE_PROCESS 'sock_type' => SWOOLE_SOCK_TCP, // sock type 默認(rèn)為SWOOLE_SOCK_TCP 'options' => [ 'pid_file' => runtime_path() . 'swoole.pid', 'log_file' => runtime_path() . 'swoole.log', 'daemonize' => false, // Normally this value should be 1~4 times larger according to your cpu cores. 'reactor_num' => swoole_cpu_num(), 'worker_num' => swoole_cpu_num(), 'task_worker_num' => 4,//swoole_cpu_num(), 'enable_static_handler' => true, 'document_root' => root_path('public'), 'package_max_length' => 20 * 1024 * 1024, 'buffer_output_size' => 10 * 1024 * 1024, 'socket_buffer_size' => 128 * 1024 * 1024, 'max_request' => 3000, 'send_yield' => true, ], ], 'websocket' => [ 'enabled' => true,// 開啟websocket 'handler' => Handler::class, //自定義wbesocket綁定類 'parser' => Parser::class, //自定義解析類 'route_file' => base_path() . 'websocket.php', 'ping_interval' => 25000, 'ping_timeout' => 60000, 'room' => [ 'type' => TableRoom::class, 'room_rows' => 4096, 'room_size' => 2048, 'client_rows' => 8192, 'client_size' => 2048, ], ], 'auto_reload' => true, 'enable_coroutine' => true, 'resetters' => [], 'tables' => [],
handler和parser大大方便了自定義websocket服務(wù),默認(rèn)系統(tǒng)集成socketio。
本文主要介紹如何使用socketio,這里假設(shè)大家有socketio有一定了解和使用基礎(chǔ)。
socketIo默認(rèn)會在請求地址后加相應(yīng)的參數(shù)
同時,socketio默認(rèn)情況下,會認(rèn)為 http://url/socket.io/ 是支持websocket服務(wù)的地址。
而在tp-swoole3.0內(nèi)部已經(jīng)對該地址請求進(jìn)行了處理
<?php namespace think\swoole\websocket\socketio; use think\Config; use think\Cookie; use think\Request; class Controller { protected $transports = ['polling', 'websocket']; public function upgrade(Request $request, Config $config, Cookie $cookie) { if (!in_array($request->param('transport'), $this->transports)) { return json( [ 'code' => 0, 'message' => 'Transport unknown', ], 400 ); } if ($request->has('sid')) { $response = response('1:6'); } else { $sid = base64_encode(uniqid()); $payload = json_encode( [ 'sid' => $sid, 'upgrades' => ['websocket'], 'pingInterval' => $config->get('swoole.websocket.ping_interval'), 'pingTimeout' => $config->get('swoole.websocket.ping_timeout'), ] ); $cookie->set('io', $sid); $response = response('97:0' . $payload . '2:40'); } return $response->contentType('text/plain'); } public function reject(Request $request) { return json( [ 'code' => 3, 'message' => 'Bad request', ], 400 ); } }
TP6.0,插件注冊采用了service方式進(jìn)行了注冊,可在tp-swoole 服務(wù)注冊文件中查看路由注冊信息,如果想自定義鏈接規(guī)則,則可以覆蓋該路由。
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- namespace think\swoole; use Swoole\Http\Server as HttpServer; use Swoole\Websocket\Server as WebsocketServer; use think\App; use think\Route; use think\swoole\command\Server as ServerCommand; use think\swoole\facade\Server; use think\swoole\websocket\socketio\Controller; use think\swoole\websocket\socketio\Middleware; class Service extends \think\Service { protected $isWebsocket = false; /** * @var HttpServer | WebsocketServer */ protected static $server; public function register() { $this->isWebsocket = $this->app->config->get('swoole.websocket.enabled', false); $this->app->bind(Server::class, function () { if (is_null(static::$server)) { $this->createSwooleServer(); } return static::$server; }); $this->app->bind('swoole.server', Server::class); $this->app->bind(Swoole::class, function (App $app) { return new Swoole($app); }); $this->app->bind('swoole', Swoole::class); } public function boot(Route $route) { $this->commands(ServerCommand::class); if ($this->isWebsocket) { $route->group(function () use ($route) { $route->get('socket.io/', '@upgrade'); $route->post('socket.io/', '@reject'); })->prefix(Controller::class)->middleware(Middleware::class); } } /** * Create swoole server. */ protected function createSwooleServer() { $server = $this->isWebsocket ? WebsocketServer::class : HttpServer::class; $config = $this->app->config; $host = $config->get('swoole.server.host'); $port = $config->get('swoole.server.port'); $socketType = $config->get('swoole.server.socket_type', SWOOLE_SOCK_TCP); $mode = $config->get('swoole.server.mode', SWOOLE_PROCESS); static::$server = new $server($host, $port, $mode, $socketType); $options = $config->get('swoole.server.options'); static::$server->set($options); } }
Socketio默認(rèn)使用demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="./static/js/socket.io.js"></script> </head> <body> <script> const socket = io('http://localhost:808'); socket.emit("test", "your message"); socket.on("test",function(res){console.log(res)}); </script> </body> </html>
在app目錄下新建websocket.php文件,其中需要注意,由于使用了反射,閉包參數(shù)名稱不能隨意定義,不然無法注入。第一個參數(shù)是websocket,是當(dāng)前websocket的Server對象,第二個參數(shù)data是客戶端發(fā)送的數(shù)據(jù)。其中socketio emit的第一個參數(shù)和Websocket::on的第一個參數(shù)一致,作為事件名稱。
<?php /** * Author:Xavier Yang * Date:2019/6/5 * Email:499873958@qq.com */ use \think\swoole\facade\Websocket; Websocket::on("test", function (\think\swoole\Websocket $websocket, $data) { //var_dump($class); $websocket->emit("test", "asd"); }); Websocket::on("test1", function ($websocket, $data) { $websocket->emit("test", "asd"); }); Websocket::on("join", function (\think\swoole\Websocket $websocket, $data) { $websocket->join("1"); });
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享thinkphp 6.0 swoole擴(kuò)展websocket的使用方法內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!
當(dāng)前名稱:thinkphp6.0swoole擴(kuò)展websocket的使用方法
文章路徑:http://chinadenli.net/article48/gogjep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、App設(shè)計(jì)、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)