欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用swoole擴展php websocket示例

 更新時間:2014年02月13日 09:03:49   作者:  
WebSocket規(guī)范的目標是在瀏覽器中實現(xiàn)和服務(wù)器端雙向通信。雙向通信可以拓展瀏覽器上的應(yīng)用類型,如果你想要用PHP來寫websocket應(yīng)用,那swoole_framework一定是最好的選擇,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

<?php
define('DEBUG', 'on');
define("WEBPATH", str_replace("\\","/", __DIR__));
require __DIR__ . '/../libs/lib_config.php';

class WebSocket extends Swoole\Network\Protocol\WebSocket
{
    /**
     * 下線時,通知所有人
     */
    function onClose($serv, $client_id, $from_id)
    {
        //將下線消息發(fā)送給所有人
        //$this->log("onOffline: " . $client_id);
        //$this->broadcast($client_id, "onOffline: " . $client_id);
        parent::onClose($serv, $client_id, $from_id);
    }

    /**
     * 接收到消息時
     * @see WSProtocol::onMessage()
     */
    function onMessage($client_id, $ws)
    {
        $this->log("onMessage: ".$client_id.' = '.$ws['message']);
        $this->send($client_id, "Server: ".$ws['message']);
  //$this->broadcast($client_id, $ws['message']);
    }

    function broadcast($client_id, $msg)
    {
        foreach ($this->connections as $clid => $info)
        {
            if ($client_id != $clid)
            {
                $this->send($clid, $msg);
            }
        }
    }
}


$AppSvr = new WebSocket();
$AppSvr->loadSetting(__DIR__."/swoole.ini"); //加載配置文件
$AppSvr->setLogger(new \Swoole\Log\EchoLog(true)); //Logger

$server = new \Swoole\Network\Server('0.0.0.0', 9503);
$server->setProtocol($AppSvr);
//$server->daemonize(); //作為守護進程
$server->run(array('worker_num' =>4));

相關(guān)文章

最新評論