diff --git a/lib/Base.php b/lib/Base.php index f5fea95..fd9b8c4 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -71,23 +71,13 @@ public function send($payload, $opcode = 'text', $masked = true) 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']; + $payload_chunks = str_split($payload, $this->options['fragment_size']); - // is this the final fragment to send? - $final = $payload_length <= $fragment_cursor; + for ($index = 0; $index < count($payload_chunks); ++$index) { + $chunk = $payload_chunks[$index]; + $final = $index == count($payload_chunks) - 1; - // send the fragment - $this->sendFragment($final, $sub_payload, $opcode, $masked); + $this->sendFragment($final, $chunk, $opcode, $masked); // all fragments after the first will be marked a continuation $opcode = 'continuation'; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 3c060fc..bbcb055 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -162,6 +162,11 @@ public function testPingPong() $this->assertEquals('Server ping', $message); $this->assertEquals('pong', $client->getLastOpcode()); + $client->send('', 'ping'); + $message = $client->receive(); + $this->assertEquals('', $message); + $this->assertEquals('pong', $client->getLastOpcode()); + $message = $client->receive(); $this->assertEquals('Client ping', $message); $this->assertTrue(MockSocket::isEmpty()); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index e789976..9edc011 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -165,6 +165,11 @@ public function testPingPong() $this->assertEquals('Server ping', $message); $this->assertEquals('pong', $server->getLastOpcode()); + $server->send('', 'ping'); + $message = $server->receive(); + $this->assertEquals('', $message); + $this->assertEquals('pong', $server->getLastOpcode()); + $message = $server->receive(); $this->assertEquals('Client ping', $message); diff --git a/tests/scripts/ping-pong.json b/tests/scripts/ping-pong.json index 77a5c51..9c7adf9 100644 --- a/tests/scripts/ping-pong.json +++ b/tests/scripts/ping-pong.json @@ -54,6 +54,45 @@ ], "return": "stream" }, + { + "function": "fwrite", + "params": [ + "@mock-stream" + ], + "return": 6 + }, + { + "function": "get_resource_type", + "params": [ + "@mock-stream" + ], + "return": "stream" + }, + { + "function": "fread", + "params": [ + "@mock-stream", + 2 + ], + "return-op": "chr-array", + "return": [138, 128] + }, + { + "function": "fread", + "params": [ + "@mock-stream", + 4 + ], + "return-op": "chr-array", + "return": [1, 1, 1, 1] + }, + { + "function": "get_resource_type", + "params": [ + "@mock-stream" + ], + "return": "stream" + }, { "function": "fread", "params": [