php是世界上最好的语言
对
没有毛病老哥、
你说的、
都对、
php是世界上最好的语言
<?php
/**
* 聊天主逻辑
* 主要是处理 onMessage onClose
*/
use \GatewayWorker\Lib\Gateway;
class Events
{
/**
* 客户端触发时
* @param int $client_id
* @param mixed $message
*/
public static function onConnect($client_id)
{
Gateway::joinGroup($client_id,1);
$count=Gateway::getClientCountByGroup(1);
$data=['type'=>'count','num'=>$count];
Gateway::sendToGroup(1,json_encode($data));
}
/**
* 有消息时
* @param int $client_id
* @param mixed $message
*/
public static function onMessage($client_id, $message)
{
$data=json_decode($message);
if($data->type == 'login')
{
$_SESSION['name']=$data->client_name;
//获取所有用户
$user_list=Gateway::getClientSessionsByGroup(1);
$user_arr=['type'=>'login','client_name'=>$data->client_name,'list'=>$user_list];
Gateway::sendToGroup(1,json_encode($user_arr));
}
//加好友
if($data->type=="add")
{
Gateway::sendToGroup(1,$message);
}
if($data->type=='text')
{
$data=json_decode($message,true);
$data['uname']=$_SESSION['name'];
$str=$data['data'];
$arr=array(
'呵呵'=>'../../../chat/public/images/phiz/hehe.gif',
'哈哈'=>'../../../chat/public/images/phiz/haha.gif',
'嘻嘻'=>"../../../chat/public/images/phiz/xixi.gif",
'可爱'=>"../../../chat/public/images/phiz/keai.gif",
'可怜'=>"../../../chat/public/images/phiz/kelian.gif",
'挖鼻屎'=>"../../../chat/public/images/phiz/wabisi.gif",
'吃惊'=>"../../../chat/public/images/phiz/chijing.gif",
'害羞'=>"../../../chat/public/images/phiz/haixiu.gif",
'挤眼'=>"../../../chat/public/images/phiz/jiyan.gif",
'闭嘴'=>"../../../chat/public/images/phiz/bizui.gif",
'鄙视'=>"../../../chat/public/images/phiz/bishi.gif",
'爱你'=>"../../../chat/public/images/phiz/aini.gif",
'泪'=>"../../../chat/public/images/phiz/lei.gif",
'偷笑'=>"../../../chat/public/images/phiz/touxiao.gif",
'亲亲'=>"../../../chat/public/images/phiz/qinqin.gif",
'生病'=>"../../../chat/public/images/phiz/shengbin.gif",
'太开心'=>"../../../chat/public/images/phiz/taikaixin.gif",
'懒得理你'=>"../../../chat/public/images/phiz/ldln.gif",
'右哼哼'=>"../../../chat/public/images/phiz/youhenhen.gif",
'左哼哼'=>"../../../chat/public/images/phiz/zuohenhen.gif",
'嘘'=>"../../../chat/public/images/phiz/xiu.gif",
'衰'=>"../../../chat/public/images/phiz/shuai.gif",
'委屈'=>"../../../chat/public/images/phiz/weiqu.gif",
'吐'=>"../../../chat/public/images/phiz/tu.gif",
'打哈欠'=>"../../../chat/public/images/phiz/dahaqian.gif",
'抱抱'=>"../../../chat/public/images/phiz/baobao.gif",
'怒'=>"../../../chat/public/images/phiz/nu.gif",
'疑问'=>"../../../chat/public/images/phiz/yiwen.gif",
'馋嘴'=>"../../../chat/public/images/phiz/canzui.gif",
'拜拜'=>"../../../chat/public/images/phiz/baibai.gif",
'思考'=>"../../../chat/public/images/phiz/sikao.gif",
'汗'=>"../../../chat/public/images/phiz/han.gif",
'困'=>"../../../chat/public/images/phiz/kun.gif",
'睡觉'=>"../../../chat/public/images/phiz/shuijiao.gif",
'钱'=>"../../../chat/public/images/phiz/qian.gif",
'失望'=>"../../../chat/public/images/phiz/shiwang.gif",
'酷'=>"../../../chat/public/images/phiz/ku.gif",
'花心'=>"../../../chat/public/images/phiz/huaxin.gif",
'哼'=>"../../../chat/public/images/phiz/heng.gif",
'鼓掌'=>"../../../chat/public/images/phiz/guzhang.gif",
'晕'=>"../../../chat/public/images/phiz/yun.gif",
'悲伤'=>"../../../chat/public/images/phiz/beishuang.gif",
'抓狂'=>"../../../chat/public/images/phiz/zuakuang.gif",
'黑线'=>"../../../chat/public/images/phiz/heixian.gif",
'阴险'=>"../../../chat/public/images/phiz/yinxian.gif",
'怒骂'=>"../../../chat/public/images/phiz/numa.gif",
'心'=>"../../../chat/public/images/phiz/xin.gif",
'伤心'=>"../../../chat/public/images/phiz/shuangxin.gif"
);
foreach($arr as $key=>$val){
$preg="/\[$key\]/";
$str=preg_replace($preg,"<img src='".$val."'>",$str);
// print_r($preg);die;
}
$data['data']=$str;
$msg=json_encode($data);
Gateway::sendToGroup(1,$msg);
}
if($data->type=='send_pic')
{
$uname=$_SESSION['name'];
$img=$data->content;
$msg=['type'=>'send_pic','uname'=>$uname,'img'=>$img];
// print_r($msg);
Gateway::sendToGroup(1,json_encode($msg));
}
}
/**
* 当客户端断开连接时
* @param integer $client_id 客户端id
*/
public static function onClose($client_id)
{
//获取所有用户
$user_list=Gateway::getClientSessionsByGroup(1);
Gateway::joinGroup($client_id,1);
$count=Gateway::getClientCountByGroup(1);
$data=['type'=>'count','num'=>$count];
Gateway::sendToGroup(1,json_encode($data));
$new_data=['type'=>'logout','client_name'=>$_SESSION['name'],'list'=>$user_list];
Gateway::sendToGroup(1,json_encode($new_data));
}
}