think-swoole4.0.4websocket使⽤环境说明:
php7.4.22
swoole4.6.1
think-swoole v4.0.4
安装步骤⽹上都有这边就不说了
swoole.php 配置
<?php
use think\swoole\websocket\socketio\Handler;
return [
'http'      => [
'enable'    => true,
'host'      => '0.0.0.0',
'port'      => 7001,
'worker_num' => swoole_cpu_num(),
'options'    => []
],
'websocket'  => [
'enable'        => true,
'handler'      => Handler::class,
'ping_interval' => 25000,
'ping_timeout'  => 60000,
'room'          => [
'type'  => 'table',
'table' => [
'room_rows'  => 4096,
'room_size'  => 2048,
'client_rows' => 8192,
'client_size' => 2048,
],
'redis' => [
'host'          => '127.0.0.1',
'port'          => 6379,
'max_active'    => 3,
'max_wait_time' => 5,
],
],
'listen'        => [
'event'  => \app\listener\WebsocketEvent::class, //重要的事件监听类
],
'subscribe'    => [],
],
'rpc'        => [
'server' => [
'enable'    => false,
'host'      => '0.0.0.0',
'port'      => 9000,
'worker_num' => swoole_cpu_num(),
'services'  => [],
],
'client' => [],
],
//队列
'queue'      => [
'enable'  => false,
'workers' => [],
]
,
'hot_update' => [
//        'enable'  => env('APP_DEBUG', false),
'enable'  => true,
'name'    => ['*.php'],
'include' => [app_path()],
'exclude' => [],
],
//连接池
'pool'      => [
'db'    => [
'enable'        => true,
'max_active'    => 3,
'max_wait_time' => 5,
],
'cache' => [
'enable'        => true,
'max_active'    => 3,
'max_wait_time' => 5,
],
//⾃定义连接池
],
'tables'    => [],
//每个worker⾥需要预加载以共⽤的实例
'concretes'  => [],
//重置器
'resetters'  => [],
//每次请求前需要清空的实例
'instances'  => [],
//每次请求前需要重新执⾏的服务
'services'  => [],
];
事件WebsocketEvent.php
<?php
declare (strict_types = 1);
namespace app\listener;
use think\Container;
use think\swoole\Websocket;
class WebsocketEvent
{
public$websocket = null;
public function __construct(Container $container)
{
$this->websocket = $container->make(Websocket::class);
}
/**
* 事件监听处理
* @param $event
*/
public function handle($event)
{
$func = $event->type;
$this->$func($event);
}
/**
* 测试类型
* @param $event
*/
public function test($event)
{
$this->websocket->emit('testcallback', ['aaaaa' => 1, 'getdata' => '123123']);    }
/**
* 加⼊房间
* @param $event
*/
public function join($event)
{
$data = $event->data;
$this->websocket->join($data[0]['room']);
}
/**
* 离开房间
* @param $event
*/
public function leave($event)
{
$data = $event->data;
$this->websocket->leave($data[0]['room']);
}
public function __call($name,$arguments)
{websocket和socket
$this->websocket->emit('testcallback', ['msg' => '不存在的请求类型:'.$name]);    }
}
demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="cdn.socket.io/socket.io-1.4.4.js"></script>
<script src="code.jquery/jquery-1.11.1.js"></script>
</head>
<body>
<div>
<div>
返回内容:
</div>
<div id="content">
</div>
</div>
<div>
在控制台中执⾏ it("test",{"asd":"我是内容"})
</div>
<div>
在控制台中执⾏ it("join",{"room":"roomtest"})  加⼊房间
</div>
<div>
在控制台中执⾏ it("leave",{"room":["roomtest"]})  离开房间
</div>
<script>
var  socket = io('192.168.0.187:7001',{transports: ['websocket']});
//xxx 这个⾃⼰替换成⾃⼰的环境thinkphp-swoole 的端⼝或者是nginx的代理端⼝//transports: ['websocket'] ⼀定要这个,改为websocket链接
//polling 这个不⽀持,轮询会导致请求变成http请求,post请求全部拒接掉
// it('test',{"asd":"asd"});
// //⾃定义msg事件,发送‘你好服务器’字符串向服务器
<('testcallback',(data)=>{
//监听浏览器通过msg事件发送的信息
console.log('data',data);//你好浏览器
});
// it('join',{"asd":"asd"});
// ('roomJoin',(data)=>{
//    console.log(data);//你好浏览器
// })
</script>
</body>
</html>
nginx配置
server
{
listen 80;
server_name webrtcswoole;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/strange-chat-room/public;
#SSL-START SSL相关配置,请勿删除或修改下⼀⾏带注释的404规则
#error_page 404/404.html;
#SSL-END
#ERROR-PAGE-START  错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START  PHP引⽤配置,可以注释或修改
f;
#PHP-INFO-END
#REWRITE-START URL重写规则引⽤,修改后将导致⾯板设置的伪静态规则失效include /www/server/panel/vhost/f;
#REWRITE-END
location /socket.io/ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
proxy_pass 0.0.0.0:7001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
#禁⽌访问的⽂件或⽬录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)    {
return 404;
}
#⼀键申请SSL证书验证⽬录相关设置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
error_log /dev/null;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires      12h;
error_log /dev/null;
access_log /dev/null;
}
access_log  /www/wwwlogs/webrtcswoole.log;
error_log  /www/log;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。