walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.03k stars 2.25k forks source link

Implementation of the onError() handler #1009

Closed programarivm closed 4 months ago

programarivm commented 5 months ago

Hello, I just noticed that the $ws_worker->onError() handler is missing in the WebSocket server example and was wondering if the following one would be okay.

// ...

        $this->worker->onError = function ($conn, $code, $msg) {
            $conn->close();

            echo "error $code $msg\n";
        };

// ...

Questions:

  1. Why is the WebSocket example ignoring this one handler?

  2. Is it necessary to close the connection when an error occurs?

  3. Or should it perhaps be as per the onError documentation?

// ...

        $this->worker->onError = function ($conn, $code, $msg) {
            echo "error $code $msg\n";
        };

// ...

Thanks for the help,

walkor commented 4 months ago

Workerman has many callback methods, and it's not possible to list all callbacks in every example. More detailed usage should be referred to the manual. https://manual.workerman.net/doc/en/worker/on-error.html The manual has listed all the situations where errors occur, and whether to close the connection depends on the business logic.