<?php
/**
* ws 优化 基础类库
* User: 攸乐
* Date: 18/5/15
* Time: 上午12:34
*/
class Ws {
CONST HOST = "0.0.0.0";
CONST PORT = 8812;
CONST CHAR_PORT=8811;//多端口
public $ws = null;
public function __construct() {
$this->ws = new swoole_websocket_server(self::HOST,self::PORT);
$this->ws->listen(self::HOST,self::CHAR_PORT,SWOOLE_SOCK_TCP);
$this->ws->set(
[
'worker_num' => 4,
'task_worker_num' => 4,
'enable_static_handler'=>true,
'document_root' =>'/storage/shared/swoole/swoole_1/worker/tp5/',
]
);
$this->ws->on("start", [$this, 'onStart']);
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("WorkerStart", [$this, 'onWorkerStart']);//开始前
$this->ws->on("request", [$this, 'onRequest']);//发起请求
$this->ws->on("task", [$this, 'onTask']);
$this->ws->on("finish", [$this, 'onFinish']);
$this->ws->on("close", [$this, 'onClose']);
//if(\app\common\RedisDemo::getInstance()->smemeber('image_game'))\app\common\RedisDemo::getInstance()->delAll();
$this->ws->start();
}
/**
* @param $server
* 设置进程名称
*/
public function onStart($server){
swoole_set_process_name('live_name');
}
/**
* @param swoole_server $server
* @param $worker_id
*/
public function onWorkerStart(\swoole_server $server ,$worker_id){
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
require __DIR__ . '/../thinkphp/base.php';
}
public function onRequest($request,$response){
//判断有favicon.ico请求 返回404
if($request->server['request_uri']=="/favicon.ico"){
$response->status(404);
$response->end();
return;
}
$_SERVER = [];
if(isset($request->server)){
foreach ($request->server as $k=>$v){
$_SERVER[strtoupper($k)]=$v;
}
}
if(isset($request->header)){
foreach ($request->header as $k=>$v){
$_SERVER[strtoupper($k)]=$v;
}
}
$_FILES = [];
if(isset($request->files)){
foreach ($request->files as $k=>$v){
$_FILES[$k]=$v;
}
}
$_GET = [];
if(isset($request->get)){
foreach ($request->get as $k=>$v){
$_GET[$k]=$v;
}
}
$_POST = [];
if(isset($request->psot)){
foreach ($request->post as $k=>$v){
$_POST[$k]=$v;
}
}
$this->writeLog();
$_POST['ws_server']=$this->ws;
// 执行应用并响应
ob_start();
try{
// 2. 执行应用
think\App::run()->send();
}catch (\Exception $e){
//todo
}
$web = ob_get_contents();
ob_end_clean();
$response->end($web);
}
/**
* 监听ws连接事件
* @param $ws
* @param $request
*/
public function onOpen( $ws, $request) {
\app\common\RedisDemo::getInstance()->sadd('image_game',$request->fd);
}
/**
* 监听ws消息事件
* @param $ws
* @param $frame
*/
public function onMessage( $ws, $frame) {
/*while (true){
$ws->push($frame->fd, "------------".$frame->fd."running----------");
sleep(2);
}*/
}
/**
* @param $serv
* @param $taskId
* @param $workerId
* @param $data
*/
public function onTask($serv, $taskId, $workerId, $data) {
print_r($data);
// 耗时场景 10s
sleep(10);
return "on task finish"; // 告诉worker
}
/**
* @param $serv
* @param $taskId
* @param $data
*/
public function onFinish($serv, $taskId, $data) {
echo "taskId:{$taskId}\n";
echo "finish-data-sucess:{$data}\n";
}
/**
* close
* @param $ws
* @param $fd
*/
public function onClose($ws, $fd) {
\app\common\RedisDemo::getInstance()->redis->srem('image_game',$fd);
}
public function writeLog(){
$logs = array_merge(['date'=>date('Ymd His')],$_GET,$_POST,$_SERVER);
$log='';
foreach ($logs as $k=>$value){
$log .=$k.":".$value." ";
}
swoole_async_writefile('../runtime/log/'.date('Ym')."/".date('d')."_swoole.log",$log.PHP_EOL,function ($filename){
},FILE_APPEND);
}
}
$obj = new Ws();
swoole_websocket_server
猜你喜欢
转载自blog.csdn.net/qq_40393605/article/details/80355871
今日推荐
周排行