| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\api\imsocket;
- use GatewayWorker\Lib\DbConnection;
- use think\facade\Db;
- use think\worker\Server;
- use GatewayWorker\Lib\Gateway;
- class imsocket extends Server
- {
- protected $socket = 'websocket://0.0.0.0:2345';
- protected $uidConnnections = array();
- public function __construct()
- {
- parent::__construct();
- }
- public function onConnect($connection){
- $msg=['type'=>'conn','msg'=>'连接服务器成功'];
- $connection->send(json_encode($msg));
- }
- public function onMessage($connection,$data)
- {
- $arr = json_decode($data);
- //绑定用户
- if($arr->type == "bind" && !isset($connection->uid)){
- $connection->uid = $arr->uid;
- //保存uid到connection的映射,实现针对特定uid的推送
- $this->worker->uidConnections[$connection->uid]=$connection;
- if($arr->school_id){
- //如果是学校的话,返回一个老师的id给他。
- $teacherList=Db::table('yoshop_user_info')
- ->alias('i')
- ->join('yoshop_user u','i.user_id = u.user_id')
- ->where('u.user_type','=',3)
- ->where('u.is_delete','=',0)
- ->where('i.school_id','=',$arr->school_id)
- ->where('i.status','=',1)
- ->select();
- $tempList=[];
- foreach($teacherList as $list){
- $temp['uid']= $list['user_id'];
- $temp['uname']= $list['real_name'];
- $temp['utype']= $list['user_type'];
- if(isset($this->worker->uidConnections[$list['user_id']])){
- //是否在线,在线加权重
- $temp['w']=1;
- }else{
- $temp['w']=0;
- }
- $tempList[]=$temp;
- }
- //按权重随机返回
- $weight = 0;
- $tempdata = array ();
- foreach ($tempList as $one) {
- $weight += $one['w'];
- for ($i = 0; $i < $one['w']; $i++) {
- $tempdata[] = $one;
- }
- }
- $use = rand(0, $weight -1);
- $one = $tempdata[$use];
- $msg=['type'=>'bind','fansinfo'=>['uid'=>$one['uid'],'uname'=>$one['uname']]];
- $connection->send(json_encode($msg));
- }
- }
- //聊天
- if($arr->type=="text"){
- $updata=[
- 'from_user_id'=>$arr->from_user_id,
- 'to_user_id'=>$arr->to_user_id,
- 'message'=>$arr->msg,
- 'school_id'=>$arr->school_id,
- 'speciality_id'=>$arr->speciality_id,
- 'sendtime'=>$arr->sendtime,
- 'chat_key'=>$arr->chat_key
- ];
- if(isset($this->worker->uidConnections[$arr->to_user_id])){
- $conn= $this->worker->uidConnections[$arr->to_user_id];
- $updata['is_push']=0;
- $conn->send($data);
- }else{
- // //先返回给发送者错误信息
- // $conn2= $this->worker->uidConnections[$arr->from_user_id];
- // $msg=['type'=>'error','msg'=>'对方不在线'];
- // $conn2->send(json_encode($msg));
- $updata['is_push']=1;
- }
- //保存数据
- Db::name('imchat')->insert($updata);
- }
- //获取聊天记录
- if($arr->type=="loadHistory"){
- $tfkey=$arr->tfkey;
- $lastMessageTimeStamp=$arr->lastMessageTimeStamp;
- $from_user_id=$arr->from_user_id;
- $conn2= $this->worker->uidConnections[$from_user_id];
- $msgList=Db::name('imchat')->where('chat_key','=',$tfkey)
- ->where('sendtime','<',$lastMessageTimeStamp)
- ->field('id,from_user_id,to_user_id,message,sendtime')
- ->order('sendtime','desc')
- ->limit(2)
- ->select();
- if($msgList){
- $msg=['type'=>'loadHistory','mgList'=>$msgList];
- }else{
- $msg=['type'=>'loadHistory','mgList'=>''];
- }
- $conn2->send(json_encode($msg));
- }
- }
- public function onClose($connection){
- unset($this->worker->uidConnections[$connection->uid]);
- }
- }
|