Thursday, March 30, 2017

Php send array bytes in web socket

I am using following library https://github.com/Textalk/websocket-php

i can able to send string using following code

require('vendor/autoload.php');

use WebSocket\Client;

$client = new Client("ws://echo.websocket.org/");
$client->send("Hello WebSocket.org!");

echo $client->receive(); // Will output 'Hello WebSocket.org!'

Now i need to convert string to array bytes .

   require('vendor/autoload.php');

    use WebSocket\Client;

    $client = new Client("ws://echo.websocket.org/");
$byte_array = unpack('C*', 'The quick fox jumped over the lazy brown dog');
    $client->send($byte_array);

    echo $client->receive(); // Will output 'Hello WebSocket.org!'

But i am getting error

Warning: strlen() expects parameter 1 to be string, array

i have checked their base class of the library

public function send($payload, $opcode = 'text', $masked = true) {
    if (!$this->is_connected) $this->connect(); /// @todo This is a client function, fixme!

    if (!in_array($opcode, array_keys(self::$opcodes))) {
      throw new BadOpcodeException("Bad opcode '$opcode'.  Try 'text' or 'binary'.");
    }

    // record the length of the payload
    $payload_length = strlen($payload);

    $fragment_cursor = 0;
    // while we have data to send
    while ($payload_length > $fragment_cursor) {
      // get a fragment of the payload
      $sub_payload = substr($payload, $fragment_cursor, $this->options['fragment_size']);

      // advance the cursor
      $fragment_cursor += $this->options['fragment_size'];

      // is this the final fragment to send?
      $final = $payload_length <= $fragment_cursor;

      // send the fragment
      $this->send_fragment($final, $sub_payload, $opcode, $masked);

      // all fragments after the first will be marked a continuation
      $opcode = 'continuation';
    }

  }



via iCoders

Advertisement