0

I try to include a chat server in my website. To do that, I've created a function in my controller to init the ChatServer that I call by the route but after 5 minutes, the timeout close my socket so I need to relauch it. Is there a way to prevent timeout in a particular function ?

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);
    }
0

1 Answer 1

1

Sockets are best implemented with external library. Ratchet might be what you're looking for.

http://socketo.me/

Also for automatic script relaunch within your server try to implement supervisord.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.