PHP code example of kitty / websocket

1. Go to this page and download the library: Download kitty/websocket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kitty / websocket example snippets


 return  [
        'address' => env('SOCKET_ADDRESS', '127.0.0.1'),        //监听地址
        'port' => env('SOCKET_PORT', 2000),                     //监听端口
        'max_coon' => 50,                                       //最大连接数
        'console' => true,                                      //是否开启控制台信息
 ];

'providers' => [
  		//...
         Kitty\WebSocket\Providers\WebSocketServiceProvider::class,
 ]

     php artisan socket:run

    show user                           //以列表形式查看当前所有连接客户端
    send 发送内容 --id 12                //向id为12的用户发送 
    send 发送内容 --ip 192.168.1.173     //向ip为192.168.1.173所有连接用户发送信息
    send 发送内容 --uuid xxx             //向uuid为xxx的用户发送消息
    broadcast 发送内容                   //向当前所有用户发送消息
    close 12                            //关闭id为12的用户连接
    close --ip 192.168.1.173            //关闭ip为192.168.1.173的所有用户

    // \App\Job\WebSocketJob
	

    public function login()
    {
        $this->sendById($this->user->id,'欢迎进入!');
    }

	id          //(int)用户ID

	uuid        //(string)用户UUID

	socket      //(resourse)用户连接套字

	hand        //(bool)是否握手

	ip          //(string)用户ip

	port        //(int)用户连接端口

	type        //(string)用户类型 

	//示例场景说明:当有新的文章被发布的时候,向订阅该栏目的用户推送一条提示消息。
    public function addArticle()
    {
        $res = Article::Ceate(['title' => '这是一篇测试文章','category_id' => 10]);
        $users = $res->User::has('category',function($q){
       	    $q->where('id',10);
        });
        if($res) {
            $ma = new SocketManager;
            $ma->connect();
            foreach($users as $user) $ma->send('send 您订阅的栏目有新文章发布 --id '.$user->id);
      	}
    }
composer
    进入进程管理控制台:php artisan socket:manager
  php
    // \App\Job\WebSocketJob
	
    $this->where('ip','192.168.1.173')->send('连接将被关闭')->close();