Giter Club home page Giter Club logo

Comments (7)

marshhxx avatar marshhxx commented on July 19, 2024

Now I am sending the message from server like this "this.client.send(message);" but I am still not reading the whole message .., any idea why I cannot read properly?

from elephant.io.

marshhxx avatar marshhxx commented on July 19, 2024

Hey @jrdiaz your function "read" has saved my life. I am using your function but instead of returning $payload I am returning [ return (string) new Decoder($payload); ] and now I am reading the entire payload. Thank you very much !!! :)

from elephant.io.

jrdiaz avatar jrdiaz commented on July 19, 2024

I'm glad it helped you! :D :D

from elephant.io.

Taluu avatar Taluu commented on July 19, 2024

Hi,

I'm sorry, but the read method is more a POC than a functionnal feature (as it was adapted from the old legacy code, without any tests as we don't really need it here at wisembly...). But as you managed to make it work, could you share what you did (through a PR, a gist, ...), so we may enhance the current read method ? Thanks !

from elephant.io.

marshhxx avatar marshhxx commented on July 19, 2024

Hi @Taluu

This is the code I'm using:

<?php
// ...
    public function read() {
               // Ignore first byte, I hope Socket.io does not send fragmented frames, so we don't have to deal with FIN bit.
                // There are also reserved bit's which are 0 in socket.io, and opcode, which is always "text frame" in Socket.io
                $header  = fread($this->stream, 1);
                // There is also masking bit, as MSB, but it's 0 in current Socket.io
                $payload_len = ord(fread($this->stream, 1));

                switch ($payload_len) {
                        case 126:
                                $payload_len = unpack("n", fread($this->stream, 2));
                                $payload_len = $payload_len[1];
                                break;
                        case 127:
                                break;
                }
                // Reads the socket until full data is read
                //$payload = fread($this->fd, $payload_len); // Does not works if packet size > 16Kb
                $payload = '';
                $read    = 0;
                while( $read < $payload_len && ( $buf = fread( $this->stream, $payload_len - $read ) ) )
                {
                        $read    += strlen($buf);
                        $payload .= $buf;
                }
                 // decode the payload
                return (string) new Decoder($payload);
        }

I have not tested what happens if payload length is 127 or what happens with if (true === $mask) that's why I am not doing a PR. I am sure this method is reading the whole stream. Anyway if you want me to do a pull request let me know, I can do it. Hope it helps.

from elephant.io.

tot-ra avatar tot-ra commented on July 19, 2024

Thanks @marshhxx, indeed, read() function needs replacement or fix. Here is a test I ran against simple socket.io server with socket.broadcast.emit('push', msg);. Note two $client2->read() calls

function testBroadcast() {
    $client = new Client(new Version1X('https://localhost:3000'));
    $client2 = new Client(new Version1X('https://localhost:3000'));
    $client2->initialize();
    $client->initialize();

    $client2->read();
    $client->emit('message', ["integration testing socketIO"]);
    $r = $client2->read();
    $client->close();
    $client2->close();

    $this->assertEquals('["push",["integration testing socketIO"]]', $r);
}

from elephant.io.

titouan13 avatar titouan13 commented on July 19, 2024

Hi,
Thank you @marshhxx

This is a very useful function. I have just added one thing at the beginning :
if (!is_resource($this->stream)) {return;}

As I want to parse the response, I had some problems with the Decoder in the return so I don't decode the payload.

from elephant.io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.