【问题标题】:Laravel prevent timeout in a controller functionLaravel 防止控制器功能超时
【发布时间】:2020-08-01 00:36:29
【问题描述】:

我尝试在我的网站中包含聊天服务器。为此,我在控制器中创建了一个函数来初始化我通过路由调用的 ChatServer,但 5 分钟后,超时关闭我的套接字,所以我需要重新启动它。有没有办法防止特定功能超时?

public function startServer(){
        set_time_limit(0);
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
        socket_bind($this->socket, 0, $this->port);
        socket_listen($this->socket);
        $this->clients = array($this->socket);
        while (true) {
            $changed = $this->clients;
            socket_select($changed, $null, $null, 0, 10);
            if(in_array($this->socket, $changed)){
                $socket_new = socket_accept($this->socket);
                $this->clients[] = $socket_new;
                $header = socket_read($socket_new, 1024);
                $this->perform_handshaking($header, $socket_new, $this->host, $this->port);
                socket_getpeername($socket_new, $ip);
                $response = $this->mask(json_encode(array('type'=>'system', 'message'=>$ip.' connected')));
                $this->send_message($response);
                $found_socket = array_search($this->socket, $changed);
                unset($changed[$found_socket]);
            }
            foreach ($changed as $changed_socket) {
                while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
                {
                    $received_text = $this->unmask($buf);
                    $tst_msg = json_decode($received_text);
                    if($tst_msg != null){
                        $user_name = $tst_msg->name;
                        $user_message = $tst_msg->message;
                        $user_color = $tst_msg->color;
                        $response_text = $this->mask(json_encode(array('type'=>'usermsg', 'name'=>$user_name, 'message'=>$user_message, 'color'=>$user_color)));
                        $this->send_message($response_text);
                    }
                    break 2;
                }
                $buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
                if ($buf === false) {
                    $found_socket = array_search($changed_socket, $this->clients);
                    socket_getpeername($changed_socket, $ip);
                    unset($this->clients[$found_socket]);
                    $response = $this->mask(json_encode(array('type'=>'system', 'message'=>$ip.' disconnected')));
                    $this->send_message($response);
                }

            }
        }
        socket_close($sock);
    }

【问题讨论】:

    标签: php laravel controller timeout


    【解决方案1】:

    最好使用外部库来实现套接字。棘轮可能是您正在寻找的。

    http://socketo.me/

    对于在您的服务器中自动重新启动脚本,请尝试实现supervisord

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      相关资源
      最近更新 更多