一、绑定用户id实现一对一客服聊天
JS:
<script>
var fromid = {$fromid};
var toid = {$toid};
var ws = new WebSocket("ws://127.0.0.1:8282");
ws.onmessage = function(e){
var message = eval("("+e.data+")");
switch (message.type){
case "init":
var bild = '{"type":"bind","fromid":"'+fromid+'"}';
ws.send(bild);
return;
case "text":
$(".chat-content").append(' <div class="chat-text section-left flex"><span class="char-img" style="background-image: url(http://chat.com/static/newcj/img/123.jpg)"></span> <span class="text"><i class="icon icon-sanjiao4 t-32"></i>'+message.data+'</span> </div>');
return;
}
}
$(".send-btn").click(function(){
var text = $(".send-input").val();
var message = '{"data":"'+text+'","type":"say","fromid":"'+fromid+'","toid":"'+toid+'"}';
$(".chat-content").append('<div class="chat-text section-right flex"><span class="text"><i class="icon icon-sanjiao3 t-32"></i>'+text+'</span> <span class="char-img" style="background-image: url(http://chat.com/static/newcj/img/132.jpg)"></span> </div>');
ws.send(message);
$(".send-input").val("");
})
</script>
/**
* 当客户端连接时触发
* 如果业务不需此回调可以删除onConnect
*
* @param int $client_id 连接id
*/
public static function onConnect($client_id)
{
global $num;
// 向当前client_id发送数据
//Gateway::sendToClient($client_id, "Hello $client_id\r\n");
// 向所有人发送
// Gateway::sendToAll("$client_id login\r\n");
echo "connect".++$num.":".$client_id."\n";
Gateway::sendToClient($client_id,json_encode([
'type'=>'init',
'client_id'=>$client_id
]));
}
/**
* 当客户端发来消息时触发
* @param int $client_id 连接id
* @param mixed $message 具体消息
*/
public static function onMessage($client_id, $message)
{
$message_data = json_decode($message,true);
if(!$message_data){
return;
}
switch($message_data['type']){
case "bind":
$fromid = $message_data['fromid'];
Gateway::bindUid($client_id, $fromid);
return;
case "say":
$text = nl2br(htmlspecialchars($message_data['data']));
$fromid = $message_data['fromid'];
$toid = $message_data['toid'];
$date=[
'type'=>'text',
'data'=>$text,
'fromid'=>$fromid,
'toid'=>$toid,
'time'=>time()
];
Gateway::sendToUid($toid, json_encode($date));
// Gateway::sendToAll(json_encode($date));
return;
}
}
Test:
二、数据持久化与优化两个聊天对象的唯一性
public static function onMessage($client_id, $message)
{
$message_data = json_decode($message,true);
if(!$message_data){
return;
}
switch($message_data['type']){
case "bind":
$fromid = $message_data['fromid'];
Gateway::bindUid($client_id, $fromid);
return;
case "say":
$text = nl2br(htmlspecialchars($message_data['data']));
$fromid = $message_data['fromid'];
$toid = $message_data['toid'];
$date=[
'type'=>'text',
'data'=>$text,
'fromid'=>$fromid,
'toid'=>$toid,
'time'=>time()
];
if(Gateway::isUidOnline($toid)){
Gateway::sendToUid($toid, json_encode($date));
$date['isread']= 1;
}else{
$date['isread']=0;
}
$date['type']="save";
Gateway::sendToUid($fromid,json_encode($date));
return;
}
}
ws.onmessage = function(e){
var message = eval("("+e.data+")");
switch (message.type){
case "init":
var bild = '{"type":"bind","fromid":"'+fromid+'"}';
ws.send(bild);
return;
case "text":
//指定两个聊天对象聊天
if(toid==message.fromid) {
$(".chat-content").append(' <div class="chat-text section-left flex"> <span class="text"><i class="icon icon-sanjiao4 t-32"></i>' + message.data + '</span> </div>');
}
return;
case "save":
//数据持久化
save_message(message);
return;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。