/** * reads the websocket message. * * * @access public * @return Message */ public function read() { $socket = $this->getSocket(); $reader = ByteReader::create($socket); $message = Message::create(); $buff = $reader->read(1); $message->setOpcode($buff->first()); $length = $reader->read(1)->first(); //client reading from server, should never be masked. $buff->push($length); if ($length < 126) { //validation, nothing to do. } elseif ($length == 126) { $length = $reader->read(2)->sum(); } elseif ($length == 127) { $length = $reader->read(8)->sum(); } else { throw new \RuntimeException('Failed to determine length in websocket client read method'); } $message->setLength($length); $message->setPayload($reader->read($length)); return $message; }
/** * Uses a serialization function to turn the data into a string. * The string is then passed to websocket message instance which packages * the data string into a WS Payload and sends it through the socket. * * @access public * @param mixed $message string or object that represents the data that needs to be passed * @param mixed $opCode (default: Message::TEXT_FRAME) * @return void */ public function sendMessage($message, $opCode = Message::TEXT_FRAME) { $input = $this->serializer->serialize($message); $message = Message::create($message); $message->setOpcode($opCode); return $this->write($message->serialize()); }