php : tp框架swoole實現websocket,wss
class Swoole extends \think\swoole\Server
{
protected static $token;
protected $host = '0.0.0.0';
protected $port = 9503;
//protected $serverType = 'socket';
protected $serverType = 'ssl';
protected static $uid = '';
protected $mode = SWOOLE_PROCESS;
protected $sockType = SWOOLE_SOCK_TCP | SWOOLE_SSL;
protected $option = [
'worker_num' => 4, // 設定啟動的Worker程式數
'daemonize' => true, //守護程式化。
'backlog' => 128, //Listen佇列長度,
'dispatch_mode' => 2,
'heartbeat_check_interval' => 5, //5秒跟新
'heartbeat_idle_time' => 100, //連線超時
'ssl_cert_file' => "/www/cert/wss.qhoo.top.pem",
'ssl_key_file' => "/www/cert/wss.qhoo.top.key"
];
//連線
public function onOpen( $server, $req)
{
//fd:進入ws給的id每次進入都會變
$fd = $req->fd;
//token={{$token}}&appid={{$appid}}&nonce={{$nonce}}×tamps={{$timestamps}}
$token = $req->get['token'];
$appid = $req->get['appid'];
$nonce = $req->get['nonce'];
$timestamps = $req->get['timestamps'];
$tokens = md5($appid.'hudong233'.$nonce.$timestamps);
if($token !== $tokens) {
$server->push($fd, Tool::showAjax(['status' => 3, 'message' => '請先登入!']));
$server->close($fd);
return;
}
/* //獲取openid;
$token = $req->get['token'] ?? ''; //拿到客戶端使用者ID
if (!$token) {
$server->push($fd, Tool::showAjax(['status' => 3, 'message' => '請先登入!']));
$server->close($fd);
return;
}
//可以存入登陸狀態
$userData = Cache::get($token);
if (!$userData) {
$server->push($fd, Tool::showAjax(['status' => 2, 'message' => '使用者身份無效,請重新登入!']));
$server->close($fd);
return;
}
self::$uid = $req->get['friendId'];
if (self::$uid == $userData['uid']) {
//傳送資料
$server->push($fd, Tool::showAjax(['status' => 2, 'message' => '非法操作!']));
$server->close($fd);
}
$server->bind($fd, $userData['uid']);
$messages = Message::getMessage($userData['uid']);
//$req->fd 伺服器識別id(會變)
(new UserModel())->where('id', $userData['uid'])->update(['fd' => $req->fd]);
$arr = $messages->toArray();
sort($arr);*/
$server->push($fd, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => '888']));
}
//接受資料
public function onMessage($server, $frame)
{
//$frame->data 傳送的資料
//$frame->fd 傳送人的伺服器id
$message = $frame->data;
if (empty($message)) return;
//$message = file_put_contents("./log.txt", "$message", FILE_APPEND);
//給所有人傳送
db('user')->insert(['v'=>$message]);
foreach ($server->connections as $key => $value) {
$server->push($value, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => $message]));
//$ws->push($value, $data1);
}
//$server->push($frame->fd, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => $arr]));
}
function onClose( $server, int $fd, int $reactorId)
{
/*(new UserModel())->where('fd', $fd)->update(['fd' => '']);*/
}
//
private function open1($server,$req) {
//fd:進入ws給的id每次進入都會變
$fd = $req->fd;
//獲取openid;
$token = $req->get['token'] ?? ''; //拿到客戶端使用者ID
if (!$token) {
$server->push($fd, Tool::showAjax(['status' => 3, 'message' => '請先登入!']));
$server->close($fd);
return;
}
//可以存入登陸狀態
$userData = Cache::get($token);
if (!$userData) {
$server->push($fd, Tool::showAjax(['status' => 2, 'message' => '使用者身份無效,請重新登入!']));
$server->close($fd);
return;
}
self::$uid = $req->get['friendId'];
if (self::$uid == $userData['uid']) {
$server->push($fd, Tool::showAjax(['status' => 2, 'message' => '非法操作!']));
$server->close($fd);
}
$server->bind($fd, $userData['uid']);
$messages = Message::getMessage($userData['uid']);
//$req->fd 伺服器識別id(會變)
(new UserModel())->where('id', $userData['uid'])->update(['fd' => $req->fd]);
$arr = $messages->toArray();
sort($arr);
$server->push($fd, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => $arr]));
}
}
相關文章
- nginx配置支援websocket協議wssNginxWeb協議
- 用 golang 去實現類似 swoole 的 websocket 服務 ?GolangWeb
- 基於 swoole 的 websocket 服務實現狀態同步Web
- JavaScript實現線上websocket WSS測試工具 -toolfk程式設計師工具網JavaScriptWeb程式設計師
- PHP TP框架同時使用where whereOrPHP框架
- Laravel 中引入 Swoole Websocket 並實現熱更新 Reload 程式碼LaravelWeb
- PHP下用Swoole實現Actor併發模型PHP模型
- 採用nginx反向代理讓websocket 支援 wssNginxWeb
- php-fpm模式下與Swoole Websocket服務如何聯絡PHP模式Web
- Light-PHP 基於 swoole 的高效能 PHP 框架PHP框架
- Fiddler抓包和修改WebSocket資料,支援wssWeb
- Netty 實現SSL安全連線(wss://)Netty
- php swoolePHP
- PHP 框架中介軟體實現PHP框架
- swoole 框架 swoft2.0 實戰框架
- 老司機帶你用 PHP 實現 Websocket 協議PHPWeb協議
- 基於 Apaache 的 laravel-websocket SSL配置(wss連線)LaravelWeb
- Redis 訂閱 + swoole_websocket 推送RedisWeb
- PHP、TP6框架及JavaScript的單步除錯PHP框架JavaScript除錯
- 利用swoole_process和eventloop實現php非同步程式設計OOPPHP非同步程式設計
- 使用 Apache 反向代理來配置 Laravel-Websocket SSL(WSS 連線)ApacheLaravelWeb
- MixPHP:基於 Swoole 的常駐記憶體型 PHP 框架PHP記憶體框架
- PHP 高效能框架 Workerman 憑什麼能硬剛 Swoole ?PHP框架
- Swoft 框架 1.0 正式來襲,首個基於 Swoole 原生協程的 PHP 框架框架PHP
- swoole加速laravel框架Laravel框架
- Swoole 系列之二(Websocket 要點講解)Web
- 基於 Swoole 搭建 WebSocket 服務詳解Web
- PHP-FPM vs SwoolePHP
- 通過 Swoole\Table 實現 Swoole 多程式資料共享
- 【Swoole原始碼研究】深入理解Swoole協程實現原始碼
- PHP websocket 應用PHPWeb
- PHP_TP5框架開發後端介面(程式碼編寫思路)PHP框架後端
- websocket是如何實現握手?Web
- 使用 Swoole 加速 Lumen 框架框架
- 淺談WebSocket協議、WS協議和WSS協議原理及關係Web協議
- Tp框架 生成二維碼框架
- Swoole MySQL 連線池的實現MySql
- Flutter websocket 實現訊息推送FlutterWeb