Ejemplo n.º 1
0
 /**
  * @param string $payload
  *
  * @return string
  * @throws ConnectionException
  */
 public function send($payload)
 {
     if (!$this->ws->isConnected()) {
         throw new ConnectionException(sprintf('Could not connect to "%s"', $this->address));
     }
     $this->ws->send($payload);
     $response = $this->ws->receive();
     if (($opCode = $this->ws->getLastOpcode()) !== 'text') {
         throw new ConnectionException(sprintf('Received non-text frame of type "%s" with text: "%s"', $opCode, $response));
     }
     return $response;
 }
Ejemplo n.º 2
0
 public function sendApiRequest($number, $content, $id)
 {
     $client = new Client(getSocketUrl());
     $client->setTimeout(10000);
     $client->send($this->buildApiRegisterRequest());
     $result = $client->receive();
     hist('api.response', $result, $id);
     $client->send($this->buildApiRequest($number, $content));
     $result = $client->receive();
     hist('api.response', $result, $id);
     hist('sms.send', 'Sent SMS to ' . $number . ': ' . $content, $id);
 }
 /**
  * Emit Event
  *
  * @param string       $event
  * @param array        $data
  * @param Closure|null $callback
  *
  * @return void
  */
 public function emit($event, array $data, Closure $callback = null)
 {
     try {
         $eventData = ['event' => $event, 'data' => $data];
         $sendData = (string) @json_encode($eventData);
         $this->websocket->send($sendData);
         $this->error = null;
         return true;
     } catch (Exception $e) {
         $this->error = $e->getMessage();
         return false;
     }
 }
Ejemplo n.º 4
0
    /**
     * @param bool $insert
     * @param array $changedAttributes
     * @throws \WebSocket\BadOpcodeException
     */
    public function afterSave($insert, $changedAttributes)
    {
        Yii::trace('try to send a call information to site ' . $this->paramint, 'app\\modules\\event\\PhoneEvent::afterSave');
        //initialize the response object
        $res = [];
        $res['type'] = 'call';
        $res['site'] = $this->paramint;
        $WSServer = \Yii::$app->params['websocketServerProtocol'] . \Yii::$app->params['clientCallWebsocketServerHost'] . ':' . \Yii::$app->params['clientCallWebsocketServerPort'];
        $client = new Client($WSServer);
        $timeinfo = Yii::$app->formatter->asDateTime($this->updated_at);
        $html = <<<EOF
\t<div style="width:250px" class="event_notification">
\t\t<div class="{$this->action}">
\t\t\t<i class="fa fa-phone"></i> {$this->action}
\t\t</div>
\t\t<small>{$timeinfo}</small><br>
\t\t<a href="#">
\t\t\tYou have an {$this->mod_table} call from {$this->paramtext}.
\t\t</a>
\t</div>
EOF;
        $res['data'] = $html;
        try {
            $client->send(Json::encode($res));
        } catch (ConnectionException $e) {
            echo "\n" . microtime(true) . " Client died: {$e}\n";
        }
        return parent::afterSave($insert, $changedAttributes);
    }
 function subsendcmd($qname)
 {
     $obj = new \stdClass();
     $obj->qname = $qname;
     $obj->cmd = "SUB";
     $data = json_encode($obj);
     parent::send($data);
 }
Ejemplo n.º 6
0
 /**
  * Send message to WebSocket server
  * If server offline - attempt to reconnect
  * @param $message
  * @throws \WebSocket\BadOpcodeException
  */
 private function sendWSMessage($message)
 {
     if (is_array($message) || is_object($message)) {
         $message = json_encode($message);
     }
     try {
         $this->wsClient->send($message);
     } catch (\Exception $e) {
         $this->logger->error(__METHOD__ . ' | ' . 'WebSocket client error. Reason: ' . $e->getMessage() . '.');
     }
 }
 public function testRandomInt()
 {
     /**
      * Message returned by WebSocket server is string
      */
     $txt = (string) rand(10, 1000);
     $ws = new Client("ws://{$GLOBALS["ip"]}:{$GLOBALS["port"]}/?service=hello");
     $ws->send($txt);
     $this->assertEquals($txt, $ws->receive());
     $ws->close();
 }
<?php

require dirname(__DIR__) . '/vendor/autoload.php';
use WebSocket\Client;
$client = new Client("ws://85.214.151.129:8080");
$client->send("[{CHECK\\/\\/TIMEOUT}]");
Ejemplo n.º 9
0
 /**
  * Reset the send counter
  */
 public function send($payload, $opcode = 'text', $masked = true)
 {
     $this->fragment_count['send'] = 0;
     return parent::send($payload, $opcode, $masked);
 }
Ejemplo n.º 10
0
 /**
  * @dataProvider serverPortProvider
  *
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Stream context in $options['context'] isn't a valid context
  */
 public function testSetInvalidStreamContextOptions($port)
 {
     $context = false;
     $options = array('context' => $context);
     $ws = new Client('ws://localhost:' . self::$ports[$port], $options);
     $ws->send('foo');
 }
Ejemplo n.º 11
0
<?php

require dirname(dirname(__FILE__)) . '/vendor/autoload.php';
use WebSocket\Client;
$client = new Client("ws://localhost:{$argv[1]}");
$client->send($argv[2]);
echo $client->receive();
Ejemplo n.º 12
0
 /**
  * @expectedException WebSocket\BadOpcodeException
  * @expectedExceptionMessage Bad opcode 'bad_opcode'
  */
 public function testSendBadOpcode()
 {
     $ws = new Client('ws://localhost:' . self::$port);
     $ws->send('foo', 'bad_opcode');
 }
Ejemplo n.º 13
0
<?php

require '../../websocket-php/lib/Base.php';
require '../../websocket-php/lib/Client.php';
use WebSocket\Client;
echo "trying to do something useful<br />";
/*
$client = new Client("ws://echo.websocket.org/");
$client->send("Hello WebSocket.org! Buh!");
*/
$client = new Client("ws://bot.betatechnologies.info:8000/");
if ($_REQUEST['msg']) {
    $client->send($_REQUEST['msg']);
} else {
    $client->send("Hello My server");
}
echo $client->receive();
// Will output 'Hello WebSocket.org!'
<?php

require dirname(__DIR__) . '/vendor/autoload.php';
use WebSocket\Client;
$client = new Client("ws://85.214.151.129:8080");
$client->send("[{INSERT\\/\\/CLICK\\/\\/COORDINATES}]");
$client->close();
sleep(60);
$client = new Client("ws://85.214.151.129:8080");
$client->send("[{INSERT\\/\\/MOUSEMOVE\\/\\/COORDINATES}]");
$client->close();
sleep(60);
$client = new Client("ws://85.214.151.129:8080");
$client->send("[{INSERT\\/\\/SCROLL\\/\\/COORDINATES}]");
$client->close();
?>