我正在使用以下库 https://github.com/Textalk/websocket-php
我可以使用以下代码发送字符串
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!'
现在我需要将字符串转换为数组字节。
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!'
但我收到错误
警告:strlen()期望参数1为字符串,数组
我检查了他们的库基类
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';
}
}