send() public method

public send ( $clientId, string $data ) : Amp\Promise
$data string
return Amp\Promise
Esempio n. 1
0
 public function onData(int $clientId, Websocket\Message $msg)
 {
     // yielding $msg buffers the complete payload into a single string. For very large payloads, you may want to
     // stream those instead of buffering them.
     $body = (yield $msg);
     // We use the IP as name for this simple chat app.
     $ip = $this->connections[$clientId];
     // If someone mentions an IP, we send the message only to clients with that IP and the sender itself.
     if (preg_match("~@(\\d+\\.\\d+\\.\\d+\\.\\d+)\\b~", $body, $match)) {
         list($all, $receiver) = $match;
         $payload = $ip . " (private): " . substr($body, strlen($all));
         $clients = array_keys($this->ips[$receiver] ?? []);
         if (!empty($clients)) {
             $this->endpoint->send($clients, $payload);
         }
         $this->endpoint->send($clientId, $payload);
     } else {
         $payload = $ip . ": " . $body;
         $this->endpoint->send(null, $payload);
     }
 }
Esempio n. 2
0
 protected function writeResponse(int $clientId, $requestId, ApiResponse $response)
 {
     $this->endpoint->send($clientId, json_encode(["request_id" => $requestId, "status" => $response->getStatus(), "data" => $response->getData(), "links" => $response->getLinks()]));
 }