laravel+swoole 实现websocket(php+web 双向通信)

1.环境配置

安装swoole扩展(这里使用的是宝塔开启)
php.ini 增加 extension=swoole.so

实现流程
1.swoole创建socket服务
2.web连接socket完成订阅发布
3.php连接socket完成订阅发布

2.文件创建
#app/Console/Commands
php artisan make:command Socket
#在Kernel.php里增加命令列表
Commands\Socket::Class
3.运行socket服务

php artisan socket



namespace App\Console\Commands;

use App\Model\Ws;
use Illuminate\Console\Command;

class Socket extends Command
{
   
    public $ws;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'socket';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
   
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
   
        //
        $this->start();
    }

    public function start()
    {
   
        //创建websocket服务器对象,监听0.0.0.0:7104端口
        $this->ws = new \swoole_websocket_server("0.0.0.0", 7104);

        //监听WebSocket连接打开事件
        

你可能感兴趣的:(php_swoole,php,swoole,websocket)