|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SW client socket |
|
4
|
|
|
* User: moyo |
|
5
|
|
|
* Date: 2018/7/2 |
|
6
|
|
|
* Time: 3:25 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Carno\HTTP\Powered\Swoole\Chips; |
|
10
|
|
|
|
|
11
|
|
|
use Carno\HTTP\Client\Frame; |
|
12
|
|
|
use Carno\HTTP\Client\Framing; |
|
13
|
|
|
use Carno\HTTP\Contracts\WSocket; |
|
14
|
|
|
use Carno\HTTP\Contracts\WSOpcode; |
|
15
|
|
|
use Carno\HTTP\Exception\ErrorResponseException; |
|
16
|
|
|
use Carno\HTTP\Standard\Utils\CodePhrases; |
|
17
|
|
|
use Psr\Http\Message\RequestInterface as Request; |
|
18
|
|
|
use Swoole\Http\Client as SWHClient; |
|
|
|
|
|
|
19
|
|
|
use Swoole\WebSocket\Frame as SWSFrame; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
trait Socket |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
private function socket(Request $request) : array |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var SWHClient $http |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
$http = $this->http; |
|
34
|
|
|
|
|
35
|
|
|
// -- frame handler |
|
36
|
|
|
|
|
37
|
|
|
$framing = new Framing($socket = new class($http) implements WSocket { |
|
38
|
|
|
/** |
|
39
|
|
|
* @var SWHClient |
|
40
|
|
|
*/ |
|
41
|
|
|
private $c = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private $v = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* anonymous constructor. |
|
50
|
|
|
* @param SWHClient $c |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(SWHClient $c) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->c = $c; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param bool $valid |
|
59
|
|
|
*/ |
|
60
|
|
|
public function status(bool $valid) : void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->v = $valid; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function valid() : bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->v; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Frame $frame |
|
75
|
|
|
*/ |
|
76
|
|
|
public function push(Frame $frame) : void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->c->push($frame->data(), $frame->code()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
*/ |
|
83
|
|
|
public function close() : void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->push(new Frame(pack('n', 1000), WSOpcode::CLOSING)); |
|
86
|
|
|
} |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
// -- message event |
|
90
|
|
|
|
|
91
|
|
|
$http->on('message', static function (SWHClient $c, SWSFrame $f) use ($framing, $socket) { |
|
92
|
|
|
switch ($f->opcode) { |
|
93
|
|
|
case WSOpcode::CLOSING: |
|
94
|
|
|
$socket->status(false); |
|
95
|
|
|
$framing->message()->close(); |
|
96
|
|
|
return; |
|
97
|
|
|
default: |
|
98
|
|
|
$framing->message()->send(new Frame($f->data, $f->opcode)); |
|
99
|
|
|
} |
|
100
|
|
|
}); |
|
101
|
|
|
|
|
102
|
|
|
$http->on('close', static function (SWHClient $c) use ($framing, $socket) { |
|
|
|
|
|
|
103
|
|
|
$socket->status(false); |
|
104
|
|
|
$framing->message()->close(); |
|
105
|
|
|
}); |
|
106
|
|
|
|
|
107
|
|
|
// -- socket dial |
|
108
|
|
|
|
|
109
|
|
|
$connector = function ($fn) use ($http, $request) { |
|
110
|
|
|
$http->upgrade($this->getUriPath($request->getUri()), $fn); |
|
|
|
|
|
|
111
|
|
|
}; |
|
112
|
|
|
|
|
113
|
|
|
// -- socket resp |
|
114
|
|
|
|
|
115
|
|
|
$response = function (SWHClient $c) use ($request, $framing, $socket) { |
|
116
|
|
|
$code = $c->statusCode; |
|
117
|
|
|
|
|
118
|
|
|
if ($code === 101) { |
|
119
|
|
|
$socket->status(true); |
|
120
|
|
|
return $framing; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($code > 0) { |
|
124
|
|
|
throw new ErrorResponseException(CodePhrases::resolve($code), $code); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->throwing($request, $c); |
|
|
|
|
|
|
128
|
|
|
}; |
|
129
|
|
|
|
|
130
|
|
|
// -- packing |
|
131
|
|
|
|
|
132
|
|
|
return [$connector, $response]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths