Example

Example – using Net_Server

Creating a simple daemon

The following example shows, how easy it is to build a forking server that receives data and sends it back to the user.

Creating a simple talkback daemon

#!/usr/local/bin/php
<?php
// server base class
require_once 'Net/Server.php';

// base class for the handler
require_once 'Net/Server/Handler.php';

/**
* simple example that implements a talkback.
*
* Normally this should be a bit more code and in a separate file
*/
class Net_Server_Handler_Talkback extends Net_Server_Handler
{
/**
* If the user sends data, send it back to him
*
* @access public
* @param integer $clientId
* @param string $data
*/
function onReceiveData( $clientId = 0, $data = "" )
{
$this->_server->sendData( $clientId, "You said: $data" );
}
}

// create a server that forks new processes
$server = &Net_Server::create('fork', 'localhost', 9090);

$handler = &new Net_Server_Handler_Talkback;

// hand over the object that handles server events
$server->setCallbackObject($handler);

// start the server
$server->start();
?>
Introduction to Net_Server (Previous) PHP socket server base class (Next)
Last updated: 2019年2月16日 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.

AltStyle によって変換されたページ (->オリジナル) /