-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Client.php
157 lines (136 loc) · 4.69 KB
/
Client.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Amp\Websocket;
use Amp\ByteStream\InputStream;
use Amp\Promise;
use Amp\Socket\SocketAddress;
use Amp\Socket\TlsInfo;
interface Client
{
/**
* Receive a message from the remote Websocket endpoint.
*
* @return Promise<Message|null> Resolves to message sent by the remote.
*
* @throws ClosedException Thrown if the connection is closed.
*/
public function receive(): Promise;
/**
* @return int Unique identifier for the client.
*/
public function getId(): int;
/**
* @return bool True if the client is still connected, false otherwise. Returns false as soon as the closing
* handshake is initiated by the server or client.
*/
public function isConnected(): bool;
/**
* @return SocketAddress Local socket address.
*/
public function getLocalAddress(): SocketAddress;
/**
* @return SocketAddress Remote socket address.
*/
public function getRemoteAddress(): SocketAddress;
/**
* @return TlsInfo|null TlsInfo object if connection is secure.
*/
public function getTlsInfo(): ?TlsInfo;
/**
* @return int Number of pings sent that have not been answered.
*/
public function getUnansweredPingCount(): int;
/**
* @return int Client close code (generally one of those listed in Code, though not necessarily).
*
* @throws \Error Thrown if the client has not closed.
*/
public function getCloseCode(): int;
/**
* @return string Client close reason.
*
* @throws \Error Thrown if the client has not closed.
*/
public function getCloseReason(): string;
/**
* @return bool True if the peer initiated the websocket close.
*
* @throws \Error Thrown if the client has not closed.
*/
public function isClosedByPeer(): bool;
/**
* Sends a text message to the endpoint. All data sent with this method must be valid UTF-8. Use `sendBinary()` if
* you want to send binary data.
*
* @param string $data Payload to send.
*
* @return Promise<void> Resolves once the message has been sent to the peer.
*
* @throws ClosedException Thrown if sending to the client fails.
*/
public function send(string $data): Promise;
/**
* Sends a binary message to the endpoint.
*
* @param string $data Payload to send.
*
* @return Promise<void> Resolves once the message has been sent to the peer.
*
* @throws ClosedException Thrown if sending to the client fails.
*/
public function sendBinary(string $data): Promise;
/**
* Streams the given UTF-8 text stream to the endpoint. This method should be used only for large payloads such as
* files. Use send() for smaller payloads.
*
* @param InputStream $stream
*
* @return Promise<void> Resolves once the message has been sent to the peer.
*
* @throws ClosedException Thrown if sending to the client fails.
*/
public function stream(InputStream $stream): Promise;
/**
* Streams the given binary to the endpoint. This method should be used only for large payloads such as
* files. Use sendBinary() for smaller payloads.
*
* @param InputStream $stream
*
* @return Promise<void> Resolves once the message has been sent to the peer.
*
* @throws ClosedException Thrown if sending to the client fails.
*/
public function streamBinary(InputStream $stream): Promise;
/**
* Sends a ping to the endpoint.
*
* @return Promise<int> Resolves with the number of bytes sent to the other endpoint.
*/
public function ping(): Promise;
/**
* @return Options The options object associated with this client.
*/
public function getOptions(): Options;
/**
* Returns connection metadata.
*
* @return ClientMetadata
*/
public function getInfo(): ClientMetadata;
/**
* Closes the client connection.
*
* @param int $code
* @param string $reason
*
* @return Promise<array> Resolves with an array containing the close code at key 0 and the close reason at key 1.
* These may differ from those provided if the connection was closed prior.
*/
public function close(int $code = Code::NORMAL_CLOSE, string $reason = ''): Promise;
/**
* Attaches a callback invoked when the client closes. The callback is passed this object as the first parameter,
* the close code as the second parameter, and the close reason as the third parameter.
*
* @param callable(Client $client, int $code, string $reason) $callback
*/
public function onClose(callable $callback): void;
}