Example #1
0
 function test_DoubleEchoResourceHandlerResponse()
 {
     $input = str_repeat("a", 1024);
     $input2 = str_repeat("b", 1024);
     $msg = WebSocketMessage::create($input);
     $client = new WebSocket("ws://127.0.0.1:12345/echo/");
     $client->setTimeOut(1000);
     $client->open();
     $client->sendMessage($msg);
     $client->sendMessage(WebSocketMessage::create($input2));
     $msg = $client->readMessage();
     $msg2 = $client->readMessage();
     $client->close();
     $this->assertEquals($input, $msg->getData());
     $this->assertEquals($input2, $msg2->getData());
 }
 public function sendString($msg)
 {
     try {
         $m = WebSocketMessage::create($msg);
         return $this->sendMessage($m);
     } catch (Exception $e) {
         $this->disconnect();
     }
 }
Example #3
0
 public function sendInstantMessage($xmlContent, $repositoryId, $targetUserId = null, $targetGroupPath = null, $nodePathes = array())
 {
     if ($repositoryId == AJXP_REPO_SCOPE_ALL) {
         $userId = $targetUserId;
     } else {
         $scope = ConfService::getRepositoryById($repositoryId)->securityScope();
         if ($scope == "USER") {
             if ($targetUserId) {
                 $userId = $targetUserId;
             } else {
                 $userId = AuthService::getLoggedUser()->getId();
             }
         } else {
             if ($scope == "GROUP") {
                 $gPath = AuthService::getLoggedUser()->getGroupPath();
             } else {
                 if (isset($targetUserId)) {
                     $userId = $targetUserId;
                 } else {
                     if (isset($targetGroupPath)) {
                         $gPath = $targetGroupPath;
                     }
                 }
             }
         }
     }
     // Publish for pollers
     $message = new stdClass();
     $message->content = $xmlContent;
     if (isset($userId)) {
         $message->userId = $userId;
     }
     if (isset($gPath)) {
         $message->groupPath = $gPath;
     }
     if (count($nodePathes)) {
         $message->nodePathes = $nodePathes;
     }
     if ($this->msgExchanger) {
         $this->msgExchanger->publishInstantMessage("nodes:{$repositoryId}", $message);
     }
     // Publish for WebSockets
     $configs = $this->getConfigs();
     if ($configs["WS_SERVER_ACTIVE"]) {
         require_once $this->getBaseDir() . "/vendor/phpws/websocket.client.php";
         // Publish for websockets
         $input = array("REPO_ID" => $repositoryId, "CONTENT" => "<tree>" . $xmlContent . "</tree>");
         if (isset($userId)) {
             $input["USER_ID"] = $userId;
         } else {
             if (isset($gPath)) {
                 $input["GROUP_PATH"] = $gPath;
             }
         }
         if (count($nodePathes)) {
             $input["NODE_PATHES"] = $nodePathes;
         }
         $input = serialize($input);
         $msg = WebSocketMessage::create($input);
         if (!isset($this->wsClient)) {
             $this->wsClient = new WebSocket("ws://" . $configs["WS_SERVER_HOST"] . ":" . $configs["WS_SERVER_PORT"] . $configs["WS_SERVER_PATH"]);
             $this->wsClient->addHeader("Admin-Key", $configs["WS_SERVER_ADMIN"]);
             @$this->wsClient->open();
         }
         @$this->wsClient->sendMessage($msg);
     }
 }
 public function sendMessage($msg)
 {
     $wsmsg = WebSocketMessage::create(json_encode($msg));
     parent::sendMessage($wsmsg);
 }
<?php

require_once "websocket.client.php";
$input = "Hello World!";
$msg = WebSocketMessage::create($input);
$client = new WebSocket("ws://127.0.0.1:12345/echo/");
$client->open();
$client->sendMessage($msg);
// Wait for an incoming message
$msg = $client->readMessage();
$client->close();
echo $msg->getData();
// Prints "Hello World!" when using the demo.php server