emit Dont Work: Solved
Opened this issue · 1 comments
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.
### 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.