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

emit Dont Work: Solved #18

Open
qtkade opened this issue Aug 21, 2021 · 1 comment
Open

emit Dont Work: Solved #18

qtkade opened this issue Aug 21, 2021 · 1 comment

Comments

@qtkade
Copy link

qtkade commented Aug 21, 2021

hello guys..
i working at this library and i see that i can connect and disconnect to the nodejs socketio Server with this code but i have one problem, the emit the event didnt work .. i try to google it and i didnt find anything so i fixed by my some experiences so i let you guys use it if having same problem.

this is the final socket.io.php file:

<?php
/**
 * Class SocketIO
 * develope by psinetron (slybeaver)
 * Git: https://github.com/psinetron
 * web-site: http://slybeaver.ru
 *
 */
class SocketIO
{
    /**
     * @param null $host - $host of socket server
     * @param null $port - port of socket server
     * @param string $action - action to execute in sockt server
     * @param null $data - message to socket server
     * @param string $address - addres of socket.io on socket server
     * @param string $transport - transport type
     * @return bool
     */
    public function send($host = null, $port = null, $action= "message",  $data = null, $address = "/socket.io/?EIO=3", $transport = 'websocket')
    {
        $fd = fsockopen($host, $port, $errno, $errstr);
        if (!$fd) {
            return false;
        } //Can't connect tot server
        $key = $this->generateKey();
        $out = "GET $address&transport=$transport HTTP/1.1\r\n";
        $out.= "Host: http://$host:$port\r\n";
        $out.= "Upgrade: WebSocket\r\n";
        $out.= "Connection: Upgrade\r\n";
        $out.= "Sec-WebSocket-Key: $key\r\n";
        $out.= "Sec-WebSocket-Version: 13\r\n";
        $out.= "Origin: *\r\n\r\n";
        fwrite($fd, $out);
        // 101 switching protocols, see if echoes key
        $result= fread($fd,10000);
        preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $result, $matches);
        $keyAccept = trim($matches[1]);
        $expectedResonse = base64_encode(pack('H*', sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
        $handshaked = ($keyAccept === $expectedResonse) ? true : false;
        if ($handshaked){
             sleep(1);
            fwrite($fd, $this->hybi10Encode('42["' . $action . '", "'.$data. '"]'));
            fread($fd,1000000);
            return true;
        } else {return false;}
    }
    private function generateKey($length = 16)
    {
        $c = 0;
        $tmp = '';
        while ($c++ * 16 < $length) { $tmp .= md5(mt_rand(), true); }
        return base64_encode(substr($tmp, 0, $length));
    }
    private function hybi10Encode($payload, $type = 'text', $masked = true)
    {
        $frameHead = array();
        $payloadLength = strlen($payload);
        switch ($type) {
            case 'text':
                $frameHead[0] = 129;
                break;
            case 'close':
                $frameHead[0] = 136;
                break;
            case 'ping':
                $frameHead[0] = 137;
                break;
            case 'pong':
                $frameHead[0] = 138;
                break;
        }
        if ($payloadLength > 65535) {
            $payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8);
            $frameHead[1] = ($masked === true) ? 255 : 127;
            for ($i = 0; $i < 8; $i++) {
                $frameHead[$i + 2] = bindec($payloadLengthBin[$i]);
            }
            if ($frameHead[2] > 127) {
                $this->close(1004);
                return false;
            }
        } elseif ($payloadLength > 125) {
            $payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8);
            $frameHead[1] = ($masked === true) ? 254 : 126;
            $frameHead[2] = bindec($payloadLengthBin[0]);
            $frameHead[3] = bindec($payloadLengthBin[1]);
        } else {
            $frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
        }
        foreach (array_keys($frameHead) as $i) {
            $frameHead[$i] = chr($frameHead[$i]);
        }
        if ($masked === true) {
            $mask = array();
            for ($i = 0; $i < 4; $i++) {
                $mask[$i] = chr(rand(0, 255));
            }
            $frameHead = array_merge($frameHead, $mask);
        }
        $frame = implode('', $frameHead);
        for ($i = 0; $i < $payloadLength; $i++) {
            $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
        }
        return $frame;
    }
}

i changed these

'42["' . $action . '", "' . addslashes($data) . '"]'

to

'42["' . $action . '", "'.$data. '"]'

and

/socket.io/?EIO=3

to

/socket.io/?EIO=2

and also i add some delay after handshake ,before emit like this:

sleep(1);

**and this is my test that its the same with original example:

<?php

include_once ('socket.io.php');

$socketio = new SocketIO();

if ($socketio->send('**XXX.XXX.XXX.XXX**', **XX** ,$action,$data)){

    echo 'we sent the message and disconnected';

} else {

    echo 'Sorry, we have a mistake :\'(';

}

?>

you must replace the XXX.XXX.XXX.XXX and XX with your server ip and port.
Thanks for read.

@qtkade
Copy link
Author

qtkade commented Aug 21, 2021

### Oh i forgot to say that at the example code you must set the $action and $data to your Event name and Your data that you need to be send like this:

<?php

include_once ('socket.io.php');

$socketio = new SocketIO();

if ($socketio->send('**XXX.XXX.XXX.XXX**', **XX** ,"message","Hello World!")){

    echo 'we sent the message and disconnected';

} else {

    echo 'Sorry, we have a mistake :\'(';

}

?>

thanks.

@qtkade qtkade closed this as completed Aug 21, 2021
@qtkade qtkade reopened this Aug 21, 2021
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

1 participant