发布于 2014-12-24 23:16:55 | 373 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Swoole扩展官方文档,程序狗速度看过来!

Swoole Framework PHP Web开发框架

Swoole是一个php扩展也是一种PHP高级Web开发框架,框架不是为了提升网站的性能,是为了提升网站的开发效率。最少的性能损耗,换取最大的开发效率。利用Swoole框架,开发一个复杂的Web功能,可以在很短的时间内完成!


本文是一个使用swoole扩展php websocket示例讲解,感兴趣的同学参考下.

WebSocket规范的目标是在浏览器中实现和服务器端双向通信。双向通信可以拓展浏览器上的应用类型,如果你想要用PHP来写websocket应用,那swoole_framework一定是最好的选择,需要的朋友可以参考下


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

class WebSocket extends SwooleNetworkProtocolWebSocket
{
    /**
     * 下线时,通知所有人
     */
    function onClose($serv, $client_id, $from_id)
    {
        //将下线消息发送给所有人
        //$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 SwooleLogEchoLog(true)); //Logger

$server = new SwooleNetworkServer('0.0.0.0', 9503);
$server->setProtocol($AppSvr);
//$server->daemonize(); //作为守护进程
$server->run(array('worker_num' =>4));



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务