php simple websocket client

2,291 views
Skip to first unread message
Message has been deleted

stefanocudini

unread,
Jan 19, 2011, 12:39:09 PM1/19/11
to nodejs
hi

I am a new in this group, i experiencing websocket connections using
php,nodejs for server side and using jquery for client side, soon i
hope to publish my simple experiments for others newbies.
for now i with this simple php script that i used to debug my apps
it's a simple client that send data to nodejs http.server and stamp
the response received
i hope that for someone it will be useful

Stefano

the code:

<?
/* Simple Websocket Client
copyleft Stefano Cudini 2011-01-19
stefano...@gmail.com

inspired by:
http://caucho.com/resin-4.0/examples/websocket-php/
*/
$host = 'localhost'; //where is the websocket server
$port = 9000;
$local = "http://localhost/"; //url where this script run
$data = 'hello world!'; //data to be send

$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $local"."\r\n".
"Host: $host"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
////WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000); //receives the data included in the
websocket package "\x00DATA\xff"
$retdata = trim($wsdata,"\x00\xff"); //extracts data
////WebSocket handshake
fclose($sock);

echo $retdata //data result
?>

Hsu Ping Feng

unread,
Jan 19, 2011, 8:21:59 PM1/19/11
to nod...@googlegroups.com
hi,

Please take a look at this document: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76

The handshake is not so simple in draft 76 (it's the current draft version implemented by most websocket supported browsers).


Fillano

2011/1/20 stefanocudini <stefano...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.




--
AUFKLÄRUNG ist der Ausgang des Menschen aus seiner selbstverschuldeten Unmündigkeit. Unmündigkeit ist das Unvermögen, sich seines Verstandes ohne Leitung eines anderen zu bedienen. Selbstverschuldet ist diese Unmündigkeit, wenn die Ursache derselben nicht am Mangel des Verstandes, sondern der Entschließung und des Mutes liegt, sich seiner ohne Leitung eines andern zu bedienen. Sapere aude! Habe Mut, dich deines eigenen Verstandes zu bedienen! ist also der Wahlspruch der Aufklärung.

Micheil Smith

unread,
Jan 19, 2011, 10:13:33 PM1/19/11
to nod...@googlegroups.com
For what it's worth, this is the node.js mailing list, not the php mailing list; that said,
websockets change quite a lot, and rather then trying to implement a client or server,
most times it's easier to use what pre-exists (unless you really understand the
protocol or want to spend a lot of time on it).

Try having a look at node-websocket-server, node-websocket-client and socket.io-node for example.

Yours,
Micheil Smith
--
BrandedCode.com

stefanocudini

unread,
Jan 20, 2011, 8:25:25 AM1/20/11
to nodejs
you're right

i have just used node-websocket-server in serverside ;-)

i had a little ajax/polling application consist of two parts, client
is jquery polling requester and server side is a php actions actuator
with a set of features activated from GET parameters

i had used this code to interface this application php core with
nodejs websocket server. When the arrive ajax request in the php core
it forward the parameters to the websocket server.
however if the browser support websocket it open direct communication
with nodejs websocket server that forward parameters to the php
actuator.

in this mode i converted the application in hybrid of ajax and
websocket and the server side core of the application is remained
same!!
also if i think that actually it isn't very very good architecture,
but it works! :-)

I did this because I did not know if nodejs supports all the features
that my php actuator, for example: mysql connections, file permission
operations, management sessions and other that php resolve simply

the my second step is the convert my php application completely in
nodejs application!

On 20 Gen, 04:13, Micheil Smith <mich...@brandedcode.com> wrote:
> For what it's worth, this is the node.js mailing list, not the php mailing list; that said,
> websockets change quite a lot, and rather then trying to implement a client or server,
> most times it's easier to use what pre-exists (unless you really understand the
> protocol or want to spend a lot of time on it).
>
> Try having a look at node-websocket-server, node-websocket-client and socket.io-node for example.
>
> Yours,
> Micheil Smith
> --
> BrandedCode.com
>
> On 20/01/2011, at 4:39 AM, stefanocudini wrote:
>
>
>
>
>
>
>
> > hi
>
> > I am a new in this group, i experiencing websocket connections using
> > php,nodejs for server side and using jquery for client side, soon i
> > hope to publish my simple experiments for others newbies.
> > for now i with this simple php script that i used to debug my apps
> > it's a simple client that send data to nodejs http.server and stamp
> > the response received
> > i hope that for someone it will be useful
>
> > Stefano
>
> > the code:
>
> > <?
> > /*      Simple Websocket Client
> >    copyleft Stefano Cudini 2011-01-19
> >    stefano.cud...@gmail.com
Reply all
Reply to author
Forward
0 new messages