From simple socket reader to server in PHP

After I have pub­lished Sock­et­Server on PHPClasses.org, I received com­ments about some ugly places and ideas of improve­ment. So now, it can be used as a skel­eton for some­thing more inter­est­ing then “Hello, World!” sample. In this post I’ll guide you through mod­i­fied example.php step by step as in pre­vi­ous one but with new func­tions and improve­ment of Sock­et­Server in focus. So in fact it’s just an exten­sion of pre­vi­ous post.

Since pre­vi­ous ver­sion, request hand­ler setter’s name was changed from setHandler() to setRequestHandler(). Also, class has obtained three events (with cor­res­pond­ing hand­ler setters):

  1. onOpensetOnOpenHandler()
  2. onCleanupsetOnCleanupHandler()
  3. onClosesetOnCloseHandler()

Now, let me intro­duce sample hand­lers for that events. First event, onOpen is fired upon new con­nec­tion arrives. You can use it to have your registry of con­nec­tions to decide which exact action should be handled accord­ing to spe­cified (passed) request, con­nec­tion id, remote address and remote port (in case of INET* sockets).

function my_open_handler($id, $addr, $port = null)
{
    echo sprintf('New connection [%d] arrived from %s:%d', $id, $addr, $port) . "\n";
}

Second event onCleanup is called with only one argu­ment passed — con­nec­tion id, upon cli­ent shut­downed con­nec­tion manu­ally but server didn’t know about it.

function my_cleanup_handler($id)
{
    echo sprintf('Connection [%d] cleaned-up', $id) . "\n";
}

And finally, third event onClose is sim­ilar to onClenup but called upon request hand­ler asks to close this connection.

function my_close_handler($id)
{
    echo sprintf('Connection [%d] closed', $id) . "\n";
}

Of course request hand­ler now also receives con­nec­tion id too. So here’s new example of request handler:

function my_handler($request, $id)
{
    if (1 === preg_match('/quit|exit/i', $request)) {
        return null;
    }

    if (1 === preg_match('/stop|halt/i', $request)) {
        return false;
    }

    echo sprintf('*** Got "%s" from %d', $request, $id) . PHP_EOL;
    return md5($request) . PHP_EOL;
}

After all was explained (hope so, if not — feel free to com­ment here or send me an email), all we need is to register neces­sary hand­lers just as it was with request hand­ler. So you can down­load attached pack­age to post (below) with class itself and example file.

UPD (26 Octo­ber 2009) Since today all updated ver­sions of class and it’s example will be placed on phpclasses.org.

  • del.icio.us
  • Google Bookmarks
  • Identi.ca
  • Twitter
  • Technorati
  • Digg
  • Slashdot
  • Facebook
  • MisterWong
  • Reddit
  • StumbleUpon
  • Mixx
  • HelloTxt
  • LinkedIn
  • PDF
  • email
  • Print
This entry was posted in Development, Networking and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>