예제 #1
0
 /**
  * Triggered when a client sends data through the socket
  * @param  \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
  * @param  string $msg The message received
  * @throws \Exception
  */
 function onMessage(ConnectionInterface $from, $msg)
 {
     $json = json_decode($msg, true);
     Logger::debug("WS message received: {$msg}");
     switch ($json["type"]) {
         case "message":
             if ($this->clients[$from]->isAuthenticated()) {
                 if ($this->clients[$from]->isMemberOf($json["payload"]["channel"])) {
                     $this->sendMessageToChannel($json["payload"]["message"], $from, $json["payload"]["channel"]);
                     if ($this->isBridged()) {
                         $this->irc->sendMessageToChannel($json["payload"]["message"], "{$this->clients[$from]->getUser()["_id"]}!{$this->clients[$from]->getUser()["_id"]}@{$from->remoteAddress}", $json["payload"]["channel"]);
                     }
                 }
             }
             break;
         case "channel":
             if ($this->clients[$from]->isAuthenticated()) {
                 if ($json["payload"]["verb"] == "add") {
                     $reply = $json;
                     $chan = Channels::getChannel($json["payload"]["channel"]);
                     if ($chan !== null) {
                         if (!$chan["private"] && !in_array($this->clients[$from]->getUser()['_id'], $chan["banned"])) {
                             $reply["payload"]["verb"] = "add";
                             $this->clients[$from]->addChannel($json["payload"]["channel"]);
                         } elseif (Users::isRepoOwner($this->clients[$from]->getUser()["_id"], $json["payload"]["channel"])) {
                             $reply["payload"]["verb"] = "add";
                             $this->clients[$from]->addChannel($json["payload"]["channel"]);
                         } else {
                             $reply["payload"]["verb"] = "error";
                         }
                     } else {
                         $reply["payload"]["verb"] = "error";
                     }
                     $from->send(json_encode($reply));
                 } elseif ($json["payload"]["verb"] == "remove") {
                     $reply = $json;
                     if ($this->clients[$from]->isMemberOf($json["payload"]["channel"])) {
                         $reply["payload"]["verb"] = "remove";
                         $this->clients[$from]->removeChannel($json["payload"]["channel"]);
                     } else {
                         $reply["payload"]["verb"] = "error";
                     }
                     $from->send(json_encode($reply));
                 }
             }
             break;
         case "auth":
             $from->send(json_encode(["type" => "authreply", "payload" => ["done" => $this->clients[$from]->authenticate($json["payload"])]]));
             break;
         default:
             Logger::warning("Bad message got.");
             break;
     }
 }
예제 #2
0
 public function close()
 {
     $this->server->closeClient($this);
 }