PHP_SocketIO_Client icon indicating copy to clipboard operation
PHP_SocketIO_Client copied to clipboard

JSON-Data?

Open TexxMedia opened this issue 9 years ago • 10 comments

Hi,

how i can change this great Class to send json-data? $data = json_encode ( array ( 'var' => 'content' ) )

fwrite($fd, $this->hybi10Encode('42["' . $action . '", "' . addslashes($data) . '"]'));

Thank you

TexxMedia avatar Aug 14 '15 00:08 TexxMedia

you need to remove addslashes and send data as JSON

Четверг, 13 августа 2015, 17:07 -07:00 от TexxMedia [email protected]:

Hi, how i can change this great Class to send json-data? $data = json_encode ( array ( 'var' => 'content' ) ) fwrite($fd, $this->hybi10Encode('42["' . $action . '", "' . addslashes($data) . '"]')); Thank you — Reply to this email directly or view it on GitHub .

Фаиль Галиевич Шахмаев http://slybeaver.ru

psinetron avatar Aug 17 '15 15:08 psinetron

Спасибо друг за простой такой клиент А как послать нормальный js объект так как его родной socket.io клиент посылает? чтоб Json не парсить :-)

NickyX3 avatar Mar 15 '16 06:03 NickyX3

Please give a sample, I always get a string on server side. How to correctly send JSON data ?

VigibotDev avatar Oct 03 '17 18:10 VigibotDev

You can send the JSON data as String. Just use json-encode/json_decode method. http://php.net/manual/en/function.json-decode.php http://php.net/manual/en/function.json-encode.php

psinetron avatar Oct 03 '17 18:10 psinetron

I know this, but I need a JSON on server side because it's a bridge and I don't want to modify the server code. The php client must be mimic the original socket.io client it's important.

My code :

<?php
 include_once("socket.io.php");

 $socketio = new SocketIO();

 $data = json_encode(array("commande" => "c"));

 if($socketio -> send("127.0.0.1", 83, "robotserveur", $data))
  echo "OK";
 else
  echo "NOK";

?>

My mod inside your class :

            //fwrite($fd, $this->hybi10Encode('42["' . $action . '", "' . addslashes($data) . '"]'));
            fwrite($fd, $this->hybi10Encode('42["' . $action . '", "' . $data . '"]'));

My server don't get the "robotserveur" (but I get a string type with your addslashes!)

VigibotDev avatar Oct 03 '17 18:10 VigibotDev

I must wireshark the content:(

VigibotDev avatar Oct 03 '17 19:10 VigibotDev

WebSocket content is not clear (gzip) ! I can't fgrep my string value inside a JSON. I can get some ASCII from the server Do you have an idea to debug this ?

(root|/var/www/temp/pascal) tcpdump -i lo port 83 -A -vv
tcpdump: listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
21:26:59.726477 IP (tos 0x0, ttl 64, id 25561, offset 0, flags [DF], proto TCP (6), length 60)
    localhost.39382 > localhost.83: Flags [S], cksum 0xfe30 (incorrect -> 0xd43c), seq 2589105809, win 43690, options [mss 65495,sackOK,TS val 10288456 ecr 0,nop,wscale 7], length 0
E..<c.@[email protected].........
...H........
21:26:59.726494 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60)
    localhost.83 > localhost.39382: Flags [S.], cksum 0xfe30 (incorrect -> 0x1dc1), seq 1883916347, ack 2589105810, win 43690, options [mss 65495,sackOK,TS val 10288456 ecr 10288456,nop,wscale 7], length 0
E..<..@.@.<..........S..pJH;.R.......0.........
...H...H....
21:26:59.726507 IP (tos 0x0, ttl 64, id 25562, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.39382 > localhost.83: Flags [.], cksum 0xfe28 (incorrect -> 0xf005), seq 1, ack 1, win 342, options [nop,nop,TS val 10288456 ecr 10288456], length 0
E..4c.@[email protected]<...V.(.....
...H...H
21:26:59.726554 IP (tos 0x0, ttl 64, id 25563, offset 0, flags [DF], proto TCP (6), length 257)
    localhost.39382 > localhost.83: Flags [P.], cksum 0xfef5 (incorrect -> 0xb4e5), seq 1:206, ack 1, win 342, options [nop,nop,TS val 10288456 ecr 10288456], length 205
E...c.@[email protected]<...V.......
...H...HGET /socket.io/?EIO=2&transport=websocket HTTP/1.1
Host: http://127.0.0.1:83
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Key: fqYLB8J7MFpHLG34pDgV2w==
Sec-WebSocket-Version: 13
Origin: *


21:26:59.727196 IP (tos 0x0, ttl 64, id 46453, offset 0, flags [DF], proto TCP (6), length 239)
    localhost.83 > localhost.39382: Flags [P.], cksum 0xfee3 (incorrect -> 0x6fb3), seq 1:188, ack 206, win 350, options [nop,nop,TS val 10288456 ecr 10288456], length 187
E....u@[email protected]<.R._...^.......
...H...HHTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: hZotW5irZCZZpiERYqaOKAOHXh8=
Sec-WebSocket-Version: 13
WebSocket-Server: uWebSockets


21:26:59.727213 IP (tos 0x0, ttl 64, id 25564, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.39382 > localhost.83: Flags [.], cksum 0xfe28 (incorrect -> 0xee75), seq 206, ack 188, win 350, options [nop,nop,TS val 10288456 ecr 10288456], length 0
E..4c.@[email protected]._pJH....^.(.....
...H...H
21:26:59.727290 IP (tos 0x0, ttl 64, id 25565, offset 0, flags [DF], proto TCP (6), length 100)
    localhost.39382 > localhost.83: Flags [P.], cksum 0xfe58 (incorrect -> 0xc39c), seq 206:254, ack 188, win 350, options [nop,nop,TS val 10288456 ecr 10288456], length 48
E..dc.@[email protected]._pJH....^.X.....
...H...H......'..9a..tg..ie..i1..9h..x|..z}..G1..9p..f1.
21:26:59.727420 IP (tos 0x0, ttl 64, id 46454, offset 0, flags [DF], proto TCP (6), length 140)
    localhost.83 > localhost.39382: Flags [P.], cksum 0xfe80 (incorrect -> 0xfc53), seq 188:276, ack 254, win 350, options [nop,nop,TS val 10288456 ecr 10288456], length 88
E....v@[email protected].....^.......
...H...H.V0{"sid":"-EPjOyjjiiBn4fiWAADv","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
21:26:59.727459 IP (tos 0x0, ttl 64, id 25566, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.39382 > localhost.83: Flags [F.], cksum 0xfe28 (incorrect -> 0xedec), seq 254, ack 276, win 350, options [nop,nop,TS val 10288456 ecr 10288456], length 0
E..4c.@[email protected]...^.(.....
...H...H
21:26:59.727617 IP (tos 0x0, ttl 64, id 46455, offset 0, flags [DF], proto TCP (6), length 56)
    localhost.83 > localhost.39382: Flags [P.], cksum 0xfe2c (incorrect -> 0x38ae), seq 276:280, ack 255, win 350, options [nop,nop,TS val 10288456 ecr 10288456], length 4
E..8.w@[email protected].....^.,.....
...H...H..40
21:26:59.727634 IP (tos 0x0, ttl 64, id 34400, offset 0, flags [DF], proto TCP (6), length 40)
    localhost.39382 > localhost.83: Flags [R], cksum 0xddd1 (correct), seq 2589106064, win 0, length 0
E..(.`@[email protected].......
^C
11 packets captured
24 packets received by filter
2 packets dropped by kernel

VigibotDev avatar Oct 03 '17 19:10 VigibotDev

No news to get a server side object and not a string ?

VigibotDev avatar Oct 12 '17 11:10 VigibotDev

Socket.io has "automatic JSON encoding/decoding" when I use .emit() How to spoof the socket.io .emit() behavior ? How to force the server side (nodejs) to parce my JSON and return an object ?

VigibotDev avatar Oct 12 '17 12:10 VigibotDev

I found the problem !!! Don't double quote the JSON string ! and I finally get my server side object :)

socket.io sample frame :

42["eventName", {headers map}, data(primitive, array or object)]

42["createUser", {"requestId": "UUID"}, {
    "firstName": "John",
    "lastName": "Dow"
}]

42["deleteUser", {}, 10]

42["getUsers", {}, [1,2,3,4,5]]

Mod inside the class :

            //fwrite($fd, $this->hybi10Encode('42["' . $action . '", "' . addslashes($data) . '"]'));
            //fwrite($fd, $this->hybi10Encode('42["' . $action . '", "' . $data . '"]'));
            fwrite($fd, $this->hybi10Encode('42["' . $action . '", ' . $data . ']'));

My PHP socket.io client :

<?php
 include_once("socket.io.php");
 
 $socketio = new SocketIO();
 
 $data = json_encode(array("commande" => "c"));
 
 if($socketio -> send("127.0.0.1", 83, "robotserveur", $data))
  echo "OK";
 else
  echo "NOK";
?>

issue closed:)

VigibotDev avatar Oct 12 '17 13:10 VigibotDev