刚接触GatewayWorker,请教大神,访客离线如何向客服端发送离线信息,好让客服端做对应数据清楚操作?
public static function onMessage($client_id, $message){
$message_data = json_decode($message,true);
if(!$message_data){
return;
}
switch($message_data['type']){
case "bind-user"; //访客绑定uid
$userid = $message_data['userid'];//访客id
$kefuid = $message_data['kefuid'];//客服id
//将访客userid与client_id绑定
Gateway::bindUid($client_id,$userid);
$text = "已连接";
$data = [
'type'=>"text",
'userid'=>$userid,
'chat'=>$text,
];
//提示访客已连接
Gateway::sendToUid($userid,json_encode($data));
$url = $message_data['url'];
$ref = $message_data['ref'];
$ip = $message_data['ip'];
$city = $message_data['city'];
$data = [
'type'=>"new-user-in", //新访客
'fromid'=>$fromid,
'ref'=>$ref,
'ip'=>$ip,
'city'=>$city,
'time'=> time()
];
//向客服发送新访客信息
Gateway::sendToUid($kefuid,json_encode($data));
return;
case "msg"; //转发消息
$text = nl2br(htmlspecialchars($message_data['data']));
$userid = $message_data['userid'];
$kefuid = $message_data['kefuid'];
$data = [
'type'=>"text",
'userid'=>$userid,
'kefuid'=>$kefuid,
'chat'=>$text,
'time'=> time()
];
//检测否在线后发送消息------------------
if(Gateway::isUidOnline($kefuid)){
Gateway::sendToUid($kfuid,json_encode($data));
}
else
{
$data = [
'type'=>"kefu-out",
'kefuid'=>$kefuid,
];
Gateway::sendToUid($userid,json_encode($data));
}
return;
}// switch结束
}//onMessage结束
public static function onClose($client_id){
$data = [
'type'=>"userid-out",
'kefuid'=>$kefuid,
];
Gateway::sendToUid($kefuid,json_encode($data));
//请问访客离线如何把userid发送给kefuid,让客服端根据userid删除对应userid的访客轨迹数据?
}//onClose结束
Gateway::sendToUid($kefuid,json_encode($data));即可
public static function onClose($client_id){
$data = [
'type'=>"userid-out",
'kefuid'=>$kefuid,
];
Gateway::sendToUid($kefuid,json_encode($data));
我再试试,昨天一直发送不出去,激活不了userid-out 事件
应该是自己的业务逻辑bug,需要自己定位。
已解决,需要在新访客绑定$uid的同时设置session,然后再onCloser触发的时候直接从session取出保存数组。
。。。。。。
Gateway::bindUid($client_id,$fromid);
//保存session数组
Gateway::setSession($client_id, array('user'=>$user,'kefu'=>$kefu));
public static function onClose($client_id){
$kefu = $_SESSION['kefu'];
$data = [
'type'=>"userid-out",
'user'=>$_SESSION['user'],
'kefu'=>$_SESSION['kefu']
];
Gateway::sendToUid($kefu,json_encode($data));
}