Example   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A onReady() 0 7 2
A connect() 0 17 2
A onShutdown() 0 7 2
A getConfigDefaults() 0 8 1
1
<?php
2
namespace PHPDaemon\Clients\WebSocket;
3
4
use PHPDaemon\Core\AppInstance;
5
6
/**
7
 * @package    Clients
8
 * @subpackage WebSocket
9
 *
10
 * @author     Vasily Zorin <[email protected]>
11
 */
12
class Example extends AppInstance
13
{
14
15
    public $wsclient;
16
17
    public $wsconn;
18
19
    /**
20
     * Constructor.
21
     * @return void
22
     */
23
    public function init()
24
    {
25
        if ($this->isEnabled()) {
26
            $this->wsclient = Pool::getInstance($this->config->wsclientname->value);
27
        }
28
    }
29
30
    /**
31
     * Called when the worker is ready to go.
32
     * @return void
33
     */
34
    public function onReady()
35
    {
36
        if ($this->wsclient) {
37
            $this->wsclient->onReady();
38
            $this->connect();
39
        }
40
    }
41
42
    public function connect()
43
    {
44
        $this->wsclient->getConnection($this->config->url->value, function ($conn) {
45
            $this->wsconn = $conn;
46
            if ($conn->connected) {
47
                $conn->sendFrame('foobar');
48
                $conn->on('disconnect', function ($conn) {
0 ignored issues
show
Unused Code introduced by
The parameter $conn is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
                    $this->log('Connection lost... Reconnect in ' . $this->config->reconnect->value . ' sec');
50
                    $this->connect();
51
                })->on('frame', function ($conn, $frame) {
52
                    $this->log('Got frame: ' . $frame);
53
                });
54
            } else {
55
                $this->log('Couldn\'t connect to ' . $this->config->url->value);
56
            }
57
        });
58
    }
59
60
    /**
61
     * Called when application instance is going to shutdown.
62
     * @return boolean Ready to shutdown?
63
     */
64
    public function onShutdown($graceful = false)
65
    {
66
        if ($this->wsclient) {
67
            return $this->wsclient->onShutdown();
68
        }
69
        return true;
70
    }
71
72
    /**
73
     * Setting default config options
74
     * Overriden from AppInstance::getConfigDefaults
75
     * @return array|false
76
     */
77
    protected function getConfigDefaults()
78
    {
79
        return [
80
            'url' => 'tcp://echo.websocket.org:80/',
81
            'reconnect' => 1,
82
            'wsclient-name' => ''
83
        ];
84
    }
85
}
86