1. 程式人生 > >swoole建立多人多房間聊天室一

swoole建立多人多房間聊天室一

核心的swoole程式碼

        基本的cs(client-sercer)結構不變,這裡利用的是redis的雜湊和set來儲存和分組;從而達到了分組,統計,定時推送等功能;最後利用onclose事件來剔除斷開的連線,全部程式碼如下:(沒做前端,就不展示了)

    核心的swoole ws.php

<?php

namespace app\common;
require_once 'Predis.php';
require_once 'Task.php';
/**
*    socket面向物件的編譯
*/
class Ws
{
    CONST HOST='0.0.0.0';
    CONST PORT='9501';
    public $ws=null;
    public $getmsg=null;
    public $server=null;

    public function __construct()
    {   
        $this->ws=new \swoole_websocket_server(self::HOST,self::PORT);
        $this->ws->set([
            //啟動task必須要設定其數量
            'worker_num' => 4,
            'task_worker_num' => 2,
            // 'heartbeat_check_interval' => 5,
            // 'heartbeat_idle_time' => 10,
        ]);
        //監聽新埠
        $this->server=$this->ws->listen("127.0.0.1", 9502, SWOOLE_SOCK_TCP);
        //關閉websocket模式
        $this->server->set([
            'open_websocket_protocol' => false,
        ]);

        $this->ws->on("start", [$this, 'onStart']);
        $this->ws->on('open',[$this,'onopen']);
        $this->server->on("receive", [$this, 'onReceive']);
        $this->ws->on('task',[$this,'onTask']);
        $this->ws->on('finish',[$this,'onFinish']);
        $this->ws->on('message',[$this,'onmessage']);
        $this->ws->on('close',[$this,'onclose']);
        $this->server->on("close", [$this, 'oncloses']);
        $this->ws->start();
    }
    //監聽資料接收事件
    public function onReceive($serv, $fd, $from_id, $data)
    {
        $shuju=json_decode($data,ture);
        // print_r($shuju).PHP_EOL;
        if (empty($shuju['data'])) {
            $this->ws->push(Predis::getInstance()->get('fd'), $data);
        }else{
            if (empty($shuju['msg'])) {
                //執行非同步任務
                $this->ws->task($shuju);
            }else{
                $push_arr=Predis::getInstance()->hvals($shuju['data']);
                // echo "叢集是:".print_r($push_arr);
                foreach ($push_arr as $v) {
                    $this->ws->push($v, $shuju['msg']);
                }
            }
        }
    }
    /**
     * 設定程序名,為後續平滑重啟程序
     * @param $server
     */
    public function onStart($server) {
        swoole_set_process_name("live_master");
    }      
   /**
        監聽開啟事件的回撥
    */
    public function onopen($server, $request)
    {
        print_r("這時的fd是:",$request->fd);
        Predis::getInstance()->set('fd',$request->fd);
    }
    
    /**
        監聽接收事件的回撥
    */
    public function onmessage($server, $frame)
    {
        $server->push($frame->fd, "{$frame->data}");
    }
    /**
        監聽關閉事件的回撥
    */
    public function onclose($ser, $fd)
    {
        print_r("你好,我的{$fd}\n");
        //退出並刪除多餘的分組fd
        $group=Predis::getInstance()->sMembers('group');
        foreach ($group as $v) {
            $fangjian=Predis::getInstance()->hgetall($v);
            foreach ($fangjian as $k => $vv) {
                if ($fd == $vv) {
                    Predis::getInstance()->hdel($v,$k);
                }
            }
        }
    }
    public function oncloses($ser, $fd)
    {
        print_r("這個是client{$fd}\n");
    }

    /**
    *   $serv           服務
    *   $task_id        任務ID,由swoole擴充套件內自動生成,用於區分不同的任務
    *   $src_worker_id  $task_id和$src_worker_id組合起來才是全域性唯一的,不同的worker程序投遞的任務ID可能會有相同
    *   $data           是任務的內容
    */
     public function onTask($serv,$task_id,$src_worker_id,$data)
    {
        //引入任務
        $obj = new Task;
        $method = $data['data'];
        $arr = $data['arr'];
        //釋出具體的任務
        $flag = $obj->$method($arr, $serv);
        return $flag; // 告訴worker
    }
    /**
    *   $task_id        是任務的ID
    *   $data           是任務處理的結果內容
    */
     public function onFinish($serv,$task_id,$data)
    {
        print_r($data).'/n';
    }

}

new Ws();

    分發任務 task.php

<?php
/**
 * 代表的是  swoole裡面 後續 所有  task非同步 任務 都放這裡來
 * Date: 18/3/27
 * Time: 上午1:20
 */
namespace app\common;
// include 'Predis.php';

class Task {
    //非同步建立房間
    public function chuangjian($data,$serv)
    {
        $time=$data['time']*1000;
        swoole_timer_after($time, function() use($data){
            //建立房間(修改拍賣商品狀態)
            self::post("https://code.77wx.cn/index/index/in");
        });
    }

    //進入房間並快取資訊
    public function jingru($data,$serv)
    {
    	$fd=Predis::getInstance()->get('fd');
    	//加入分組
    	Predis::getInstance()->hset($data['name'],$data['uid'],$fd);
    	//加入組集合
    	Predis::getInstance()->sadd('group',$data['name']);
    }


	public function post($url,$params=false,$ispost=0)
	{
		$httpInfo = array();
		$ch = curl_init();
		curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
		curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
		curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
		curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
		if( $ispost )
		{
		    curl_setopt( $ch , CURLOPT_POST , true );
		    curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
		    curl_setopt( $ch , CURLOPT_URL , $url );
		}
		else
		{
		    if($params){
		        curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
		    }else{
		        curl_setopt( $ch , CURLOPT_URL , $url);
		    }
		}
		//執行
		$response = curl_exec( $ch );
		if ($response === FALSE) {
		    //echo "cURL Error: " . curl_error($ch);
		    return false;
		}

		$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
		$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
		//關閉url請求
		curl_close( $ch );
		return json_decode($response,1);
	}

}


    客戶端 client.php

<?php
namespace app\common;

class Client
{
    public $msg='';

    public $data=[];

    public function lianjie(){

        $cli = new \swoole_client(SWOOLE_SOCK_TCP);
        //判斷連線狀態(同步連線模式)
        $res=$cli->connect('127.0.0.1', 9502);
        if (empty($res)) {
            return "連線失敗";
        }

        if (!empty($this->data)) {
        	//傳送訊息給server
        	$rel=$cli->send(json_encode($this->data));
        }else{
        	//傳送訊息給server
        	$rel=$cli->send($this->msg);
        }
        if (!empty($rel)) {
            return $rel;
        }else{
            return flash;
        }
    }
}

    控制器 index.php

<?php
namespace app\index\controller;

use app\common\Client;
use app\common\Predis;
use app\common\Sql;
use app\index\model\User;

class Index
{
    //建立房間(新增拍賣倒計時)
    public function chuangjian()
    {
        $data['time']=input("time");
        $data['id']=input("id");
        $cli = new Client();
        $cli->data = [
            'data' => 'chuangjian',
            'arr' => $data
        ];
        return $cli->lianjie();
    }
    //點選新增雜湊(進入房間)
    public function jingru()
    {
        $data['name']=input("name");
        $data['uid']=input("uid");
        $cli = new Client();
        $cli->data = [
            'data' => 'jingru',
            'arr' => $data
        ];
        return $cli->lianjie();
    }
    //本房間推送(出價格成功並推送)
    public function pushfan()
    {
        $data['fan']=input("fan");
        $cli = new Client();
        $cli->data = [
            'data' => $data['fan'],
            'msg' => "恭喜使用者111,喜當爹!!!!"
        ];
        return $cli->lianjie();
    }
    //時間結束並指定推送
    public function zhiding()
    {
        $data['fan']=input("fan");
        $cli = new Client();
        $cli->data = [
            'data' => $data['fan'],
            'msg' => "恭喜使用者111,喜當爹!!!!"
        ];
        return $cli->lianjie();
    }

}