ServerHandler::onError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WSSCTEST;
4
5
use WSSC\Contracts\ConnectionContract;
6
use WSSC\Contracts\WebSocket;
7
use WSSC\Exceptions\WebSocketException;
8
use Monolog\Logger;
9
use Monolog\Handler\StreamHandler;
10
11
class ServerHandler extends WebSocket
12
{
13
    /*
14
     *  if You need to parse URI context like /messanger/chat/JKN324jn4213
15
     *  You can do so by placing URI parts into an array - $pathParams, when Socket will receive a connection 
16
     *  this variable will be appropriately set to key => value pairs, ex.: ':context' => 'chat'
17
     *  Otherwise leave $pathParams as an empty array
18
     */
19
20
    /**
21
     * @var array|string[]
22
     */
23
    public array $pathParams = [':entity', ':context', ':token'];
24
25
    /**
26
     * @var array
27
     */
28
    private array $clients = [];
29
30
    /**
31
     * @var Logger
32
     */
33
    private Logger $log;
34
35
    /**
36
     * ServerHandler constructor.
37
     *
38
     * @throws \Exception
39
     */
40
    public function __construct()
41
    {
42
        // create a log channel
43
        $this->log = new Logger('ServerSocket');
44
        $this->log->pushHandler(new StreamHandler('./tests/tests.log'));
45
    }
46
47
    public function onOpen(ConnectionContract $conn)
48
    {
49
        $this->clients[$conn->getUniqueSocketId()] = $conn;
50
        $this->log->debug('Connection opened, total clients: ' . count($this->clients));
51
    }
52
53
    public function onMessage(ConnectionContract $recv, $msg)
54
    {
55
        $this->log->debug('Received message:  ' . $msg);
56
        $recv->send($msg);
57
    }
58
59
    public function onClose(ConnectionContract $conn)
60
    {
61
        unset($this->clients[$conn->getUniqueSocketId()]);
62
        $this->log->debug('close: ' . print_r($this->clients, 1));
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->clients, 1) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $this->log->debug('close: ' . /** @scrutinizer ignore-type */ print_r($this->clients, 1));
Loading history...
63
        $conn->close();
64
    }
65
66
    /**
67
     * @param ConnectionContract $conn
68
     * @param WebSocketException $ex
69
     */
70
    public function onError(ConnectionContract $conn, WebSocketException $ex)
71
    {
72
        echo 'Error occured: ' . $ex->printStack();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $ex->printStack() targeting WSSC\Exceptions\WebSocketException::printStack() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
    }
74
75
    /**
76
     * You may want to implement these methods to bring ping/pong events
77
     *
78
     * @param ConnectionContract $conn
79
     * @param string $msg
80
     */
81
    public function onPing(ConnectionContract $conn, $msg)
82
    {
83
        // TODO: Implement onPing() method.
84
    }
85
86
    /**
87
     * @param ConnectionContract $conn
88
     * @param $msg
89
     * @return mixed
90
     */
91
    public function onPong(ConnectionContract $conn, $msg)
92
    {
93
        // TODO: Implement onPong() method.
94
    }
95
}
96