Socket.php 970 字节
<?php
/**
 * @remark :
 * @name   :Socket.php
 * @author :lyh
 * @method :post
 * @time   :2023/8/24 10:43
 */

namespace App\Helper;
require __DIR__ . '/vendor/autoload.php';
use WebSocket\Client;
class Socket
{
    private $client;

    public $serverIp = '127.0.0.1';

    public $serverPort = '9555';

    public function __construct() {
        $socketUrl = "ws://{$this->serverIp}:{$this->serverPort}";
        $this->client = new Client($socketUrl);
    }

    /**
     * @remark :发送消息
     * @name   :send
     * @author :lyh
     * @method :post
     * @time   :2023/8/31 10:18
     */
    public function send($data) {
        $this->client->send($data);
    }

    public function receive() {
        return $this->client->receive();
    }

    /**
     * @remark :关闭连接
     * @name   :close
     * @author :lyh
     * @method :post
     * @time   :2023/8/31 10:21
     */
    public function close() {
        $this->client->close();
    }
}