connection = $connection; return $this; } public function send(string $type, ?array $data = null, bool $close = false, array $other = []) { $this->connection->lastMessageTime = time(); $res = compact('type'); if (!is_null($data)) $res['data'] = $data; $data = array_merge($res, $other); if ($close) $data['close'] = true; $json = json_encode($data); return $close ? $this->connection->close($json) : $this->connection->send($json); } public function success($type = 'success', ?array $data = null) { if (is_array($type)) { $data = $type; $type = 'success'; } return $this->send($type, $data); } public function fail($type = 'error', ?array $data = null) { if (is_array($type)) { $data = $type; $type = 'error'; } return $this->send($type, $data); } public function close($type = 'error', ?array $data = null) { if (is_array($type)) { $data = $type; $type = 'error'; } return $this->send($type, $data, true); } }