Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how do I distinguish an incoming text msg from binary ? #1031

Open
Zixim opened this issue Aug 1, 2023 · 1 comment
Open

how do I distinguish an incoming text msg from binary ? #1031

Zixim opened this issue Aug 1, 2023 · 1 comment

Comments

@Zixim
Copy link

Zixim commented Aug 1, 2023

This is a snippet from my script :

require_once("vendor/autoload.php");
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class WebSocketHandler implements MessageComponentInterface {
    public function onOpen(ConnectionInterface $conn) {
		// Handle new WebSocket connection
		echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
		// Handle incoming WebSocket message
		$now = DateTime::createFromFormat('U.u', microtime(true));
		echo "New message! " . $now->format("m-d-Y H:i:s.u") . "\n";
		
		$myFile = $now->format("Y-m-d H:i:s.u") . "_" . uniqid() . ".txt";
		$fh = fopen($myFile, 'a');
		fwrite($fh, $msg);
		fclose($fh);
    }

When $msg contains binary... things don't go as expected.
I suppose because I'm treating the binary content as text.
How can I distinguish the two ?

@programarivm
Copy link

programarivm commented Sep 15, 2023

Hello @Zixim,

IMHO and if I'm not mistaken, some familiarization with the WebSocket protocol is required to conclude that at the moment this functionality isn't implemented in the Ratchet library.

Broadly speaking, there are types for textual data (which is interpreted as UTF-8 [RFC3629] text), binary data (whose interpretation is left up to the application), and control frames (which are not intended to carry data for the application but instead for protocol-level signaling, such as to signal that the connection should be closed).

The WebSocket Protocol. (2023, September 15). https://www.rfc-editor.org/rfc/rfc6455

5.7.  Examples

   o  A single-frame unmasked text message

      *  0x81 0x05 0x48 0x65 0x6c 0x6c 0x6f (contains "Hello")

   o  A single-frame masked text message

      *  0x81 0x85 0x37 0xfa 0x21 0x3d 0x7f 0x9f 0x4d 0x51 0x58
         (contains "Hello")

   o  A fragmented unmasked text message

      *  0x01 0x03 0x48 0x65 0x6c (contains "Hel")

      *  0x80 0x02 0x6c 0x6f (contains "lo")
      
   o  Unmasked Ping request and masked Ping response

      *  0x89 0x05 0x48 0x65 0x6c 0x6c 0x6f (contains a body of "Hello",
         but the contents of the body are arbitrary)

      *  0x8a 0x85 0x37 0xfa 0x21 0x3d 0x7f 0x9f 0x4d 0x51 0x58
         (contains a body of "Hello", matching the body of the ping)

   o  256 bytes binary message in a single unmasked frame

      *  0x82 0x7E 0x0100 [256 bytes of binary data]

   o  64KiB binary message in a single unmasked frame

      *  0x82 0x7F 0x0000000000010000 [65536 bytes of binary data]
11.8.  WebSocket Opcode Registry

This specification creates a new IANA registry for WebSocket Opcodes
  in accordance with the principles set out in RFC 5226 [RFC5226].

  As part of this registry, IANA maintains the following information:

  Opcode
     The opcode denotes the frame type of the WebSocket frame, as
     defined in Section 5.2.  The opcode is an integer number between 0
     and 15, inclusive.

  Meaning
     The meaning of the opcode value.

  Reference
     The specification requesting the opcode.

  WebSocket Opcode numbers are subject to the "Standards Action" IANA
  registration policy [RFC5226].

  IANA has added initial values to the registry as follows.

    |Opcode  | Meaning                             | Reference |
   -+--------+-------------------------------------+-----------|
    | 0      | Continuation Frame                  | RFC 6455  |
   -+--------+-------------------------------------+-----------|
    | 1      | Text Frame                          | RFC 6455  |
   -+--------+-------------------------------------+-----------|
    | 2      | Binary Frame                        | RFC 6455  |
   -+--------+-------------------------------------+-----------|
    | 8      | Connection Close Frame              | RFC 6455  |
   -+--------+-------------------------------------+-----------|
    | 9      | Ping Frame                          | RFC 6455  |
   -+--------+-------------------------------------+-----------|
    | 10     | Pong Frame                          | RFC 6455  |
   -+--------+-------------------------------------+-----------|

The OP_TEXT and OP_BINARY constants defined in src/Messaging/Frame.php could be used in the onControlFrame() method to distinguish an incoming frame, however, it is assumed a text-based connection.

I hope this will help,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants