-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSockets.php
executable file
·91 lines (85 loc) · 2.37 KB
/
WebSockets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of Numbers Framework.
*
* (c) Volodymyr Volynets <volodymyr.volynets@gmail.com>
*
* This source file is subject to the Apache 2.0 license that is bundled
* with this source code in the file LICENSE.
*/
class WebSockets
{
/**
* Web socket object
*
* @var object
*/
public $object;
/**
* Constructing web socket object
*
* @param string $web_socket_link
* @param string $class
* @param array $options
*/
public function __construct($web_socket_link = 'default', $class = null, $options = [])
{
// get object from factory
$temp = Factory::get(['websockets', $web_socket_link]);
// if we have class
if (!empty($class) && !empty($web_socket_link)) {
// check if backend has been enabled
if (!Application::get($class, ['submodule_exists' => true])) {
throw new Exception('You must enable ' . $class . ' first!');
}
// replaces in case we have it as submodule
$class = str_replace('.', '\\', trim($class));
// need to manually close connection
if (!empty($temp['object']) && $temp['class'] != $class) {
$temp['object']->close();
unset($this->object);
}
$this->object = new $class($web_socket_link, $options);
// putting every thing into factory
Factory::set(['websockets', $web_socket_link], [
'object' => $this->object,
'class' => $class
]);
} elseif (!empty($temp['object'])) {
$this->object = $temp['object'];
} else {
throw new Exception('Could not initialize web socket object!');
}
}
/**
* Connect
*
* @param array $options
* @return array
*/
public function connect($options)
{
return $this->object->connect($options);
}
/**
* Close
*
* @return array
*/
public function close()
{
return $this->object->close();
}
/**
* Send
*
* @param string $message
* @param array $data
* @param bool|null $ack
* @return array
*/
public function send(string $message, array $data = [], ?bool $ack = null): array
{
return $this->object->send($message, $data, $ack);
}
}