发送正常,心跳也正常但是不能即时收到消息,

xycmgz

问题描述

发送是成功的,就是不能正常即时收到消息
Download the Vue Devtools extension for a better development experience:
https://github.com/vuejs/vue-devtools
chat.vue:505 getEmojiItem 10
chat.vue:514 emojiItem (10) [Array(21), Array(21), Array(21), Array(21), Array(21), Array(21), Array(21), Array(21), Array(21), Array(9), __ob__: Observer]
chat.vue:421 nowTime 1687192379
chat.vue:1099 {"uid":11119,"oSO":1}
chat.vue:1105 websocket连接请求已发送
chat.vue:1203 getMessage {code: 200, msg: '请求成功', result: {…}}
chat.vue:1220 startScroll
chat.vue:1222 startScroll {id: '', dataset: {…}, left: 10, right: 380, top: 51, …}
chat.vue:1230 viewHight2 2858.578125
chat.vue:449 onPageScroll {scrollTop: 2177}
chunk-vendors.js:63385 WebSocket connection to 'ws://localhost:8081/sockjs-node/607/05fikum2/websocket' failed: WebSocket is closed before the connection is established.
WebSocketTransport.close @ chunk-vendors.js:63385
SockJS._transportTimeout @ chunk-vendors.js:61231
setTimeout(异步)
SockJS._connect @ chunk-vendors.js:61210
SockJS._receiveInfo @ chunk-vendors.js:61190
g @ chunk-vendors.js:60456
EventEmitter.emit @ chunk-vendors.js:60476
(匿名) @ chunk-vendors.js:60952
g @ chunk-vendors.js:60456
EventEmitter.emit @ chunk-vendors.js:60476
(匿名) @ chunk-vendors.js:60761
g @ chunk-vendors.js:60456
EventEmitter.emit @ chunk-vendors.js:60476
xhr.onreadystatechange @ chunk-vendors.js:61985
XMLHttpRequest.send(异步)
AbstractXHRObject._start @ chunk-vendors.js:61993
(匿名) @ chunk-vendors.js:61882
setTimeout(异步)
AbstractXHRObject @ chunk-vendors.js:61881
XHRLocalObject @ chunk-vendors.js:63301
InfoAjax @ chunk-vendors.js:60743
InfoReceiver._getReceiver @ chunk-vendors.js:60921
InfoReceiver.doXhr @ chunk-vendors.js:60941
(匿名) @ chunk-vendors.js:60910
setTimeout(异步)
InfoReceiver @ chunk-vendors.js:60909
SockJS @ chunk-vendors.js:61118
SockJSClient @ chunk-vendors.js:20183
initSocket @ chunk-vendors.js:35550
(匿名) @ chunk-vendors.js:21209
FPRn @ chunk-vendors.js:21210
__webpack_require__ @ index.js:854
fn @ index.js:151
1 @ index.js:1030
__webpack_require__ @ index.js:854
checkDeferredModules @ index.js:46
(匿名) @ index.js:994
(匿名) @ index.js:997
chat.vue:681 getCid {data: {…}, statusCode: 200, header: {…}, errMsg: 'request:ok'}
chat.vue:1278 updateChatStatus {code: 200, msg: '请求成功', result: '更新私信阅读状态成功'}
chat.vue:1187 心跳发送成功
chat.vue:1304 img? 0
chat.vue:1308 mTxt 222
chat.vue:1331 res {code: 200, msg: '请求成功', result: {…}}
chat.vue:1367 sendScoket {from_uid: 11119, to_uid: 10610, post_id: 0, title: '', content: '222', …}
chat.vue:1372 toUser {"from_uid":11119,"to_uid":10610,"post_id":0,"title":"","content":"222","type":6,"chatType":0,"create_time":1687192276,"contentDuration":"","topic_id":0}
chat.vue:1240 newMsg {from_uid: 11119, to_uid: 10610, post_id: 0, title: '', content: '222', …}
chat.vue:1264 newMsg (51) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, __ob__: Observer]
chat.vue:1350 startScroll
chat.vue:1352 startScroll {id: '', dataset: {…}, left: 10, right: 380, top: -2126, …}
chat.vue:449 onPageScroll {scrollTop: 2230}
337chat.vue:1187 心跳发送成功

程序代码或配置

        ChatSocket() {
            let that = this;
            let arr = {};
            arr["uid"] = this.userInfo.uid;
            arr["oSO"] = 1;
            let Nuser = JSON.stringify(arr);
            console.log(Nuser);

            that.websocket = uni.connectSocket({
                url: 'ws://121.196.210.2:10007',
                success(data) {
                    console.log("websocket连接请求已发送");
                },
            });
            that.websocket.onOpen(function(res) {
                that.websocket.send({
                    data: Nuser,
                })
                that.HeartBeat();
            });

这里粘代码或配置

00:53:06.365 心跳发送成功 at pages/chat/chat.vue:1187

后端:
// +----------------------------------------------------------------------
return [
// 扩展自身需要的配置
'host' => '0.0.0.0', // 监听地址
'port' => 10007, // 监听端口
'root' => '', // WEB 根目录 默认会定位public目录
'app_path' => '', // 应用目录 守护进程模式必须设置(绝对路径)
'file_monitor' => false, // 是否开启PHP文件更改监控(调试模式下自动开启)
'file_monitor_interval' => 2, // 文件监控检测时间间隔(秒)
'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录

// 支持workerman的所有配置参数
'name'                  => 'thinkphp',
'count'                 => 4,
'daemonize'             => false,
'pidFile'               => '',

return [
// 扩展自身需要的配置
'protocol'       => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host'           => '0.0.0.0', // 监听地址
'port'           => 10007, // 监听端口
'socket'         => '', // 完整监听地址
'context'        => [], // socket 上下文选项
'worker_class'   => 'app/push/controller/Worker', // 自定义Workerman服务类名 支持数组定义多个服务

// 支持workerman的所有配置参数
'name'           => 'thinkphp',
'count'          => 4,
'daemonize'      => false,
'pidFile'        => '',

// 支持事件回调
// onWorkerStart
'onWorkerStart'  => function ($worker) {

},
// onWorkerReload
'onWorkerReload' => function ($worker) {

},
// onConnect
'onConnect'      => function ($connection) {

},
// onMessage
'onMessage'      => function ($connection, $data) {
    $connection->send('receive success');
},
// onClose
'onClose'        => function ($connection) {

},
// onError
'onError'        => function ($connection, $code, $msg) {
    echo "error [ $code ] $msg\n";
},

];

<?php
namespace app\push\controller;
// use think\facade\Db;
// use think\worker\Server;
// use Workerman\Lib\Timer;

use think\facade\Db;
use think\worker\Server;
use Workerman\Lib\Timer;

// use JPush\Client as jgPush;//极光推送
define('HEARTBEAT_TIME', 20);// 心跳间隔55秒
// php think worker:server 启动命令
class Worker extends Server
{
protected $socket = 'websocket://0.0.0.0:10007';
// protected $processes = 4;
// $socket->count = 4;
// $this->Worker->count = 4;
// protected $count = 4;

public function onConnect($connection){
    $msg = '{"msg":"这是控制文件的onMessage"}';
    $connection->send($msg);
    // echo "open onConnect";
}

## 接收消息,接收心跳消息 ,接收用户id 信息等等
public function onMessage($connection,$data)
{
    // {"form_uid":10952,"to_uid":10903,"sendTxt":"niakd"}
    // $this->push();
    // echo $data;
    //fin1,fin2检索是否为建立连接
    $fin1 = 'oSO';//建立连接
    $fin2 = 'uid';//建立连接的用户
    // $fin3 = '{';
    $p1 = strpos($data,$fin1);
    $p2 = strpos($data,$fin2);
    // $p3 = strpos($data,$fin3);
    if($p1&&$p2){
        $arr = json_decode($data);
        $oSO = $arr->{'oSO'};
        $muid = $arr->{'uid'};
        echo "oSO".$oSO;
        echo "uid".$muid;
    }else{
        $oSO = 0;
    }

    if ($oSO==1) {
        $msg = '{"msg":"oSS==1"}';
        $connection->send($msg);
        $connection->id = $muid;
        $this->worker->uniConnections[$connection->id] = $connection;
        // $this->addAli($muid);
        // echo "open onConnect";
        // $this->push();
        // $worker->uniConnections = array();
        // $worker->uidConnections[$connection->id] = $connection;
        // echo "返回".$muid;

    }

    // else{
    //     $connection->send("失败");
    // }
    // echo $connection->id;

    //fin3,fin4检索是否给其他用户发送消息
    $fin3 = 'to_uid';//接收用户
    $fin4 = 'from_uid';//发送者
    // $fin4 = 'sendTxt';//发送文本
    $p3 = strpos($data,$fin3);
    $p4 = strpos($data,$fin4);
    if($p3&&$p4){
        $Sarr = json_decode($data);
        $toUid = $Sarr->{'to_uid'};
        $fromUid = $Sarr->{'from_uid'};
        $tid = $Sarr->{'topic_id'};
        echo $tid;
        if($tid == 0){
            $toSUid = strval($toUid);
            if(isset($this->worker->uniConnections[$toUid])){
                echo "id存在";
                $this->worker->uniConnections[$toUid]->send($data);
            }else{
                echo "id不存在";
            }
        }

        // $userInfo = Db::name("user")->where('uid',$fromUid)->find();
        // echo $toUid;
        // echo $toTxt;
        // $toUserC = 
        // echo $connection[$toUid]->id;
        // $connection[$toUid]->send($toTxt);

        // print_r($this->worker->uniConnections[$toUid]->id);

        // $toSUid = $toUid;

        // if(!$this->worker->uniConnections[$toUid]->id){

        // }

    }

    //前端心跳检测
    $fin5 = 'SocketHeart';//前端心跳检测专用
    $p5 = strpos($data,$fin5);
    if($p5){
        $connection->send("SocketHeart");
        echo "心跳接收成功";
    }

    //家族群聊
    // $fin3 = 'to_uid';
    // $fin4 = 'sendTxt';
    $fin6 = 'topic_id';//接收群
    $fin7 = 'from_uid';//发送用户
    // $fin8 = 'topic_id';//接收群
    $p6 = strpos($data,$fin6);
    $p7 = strpos($data,$fin7);
    if($p6&&$p7){
        echo "群聊消息";
        $Sarr = json_decode($data);
        $frUid = $Sarr->{'from_uid'};
        $toTid = $Sarr->{'topic_id'};
        // $toTxt = $Sarr->{'sendTxt'};
        if($toTid!=0){
            echo "群聊消息2";
            $toUid = Db::name("user_topic")->where("topic_id",$toTid)->field("uid")->select()->toArray();
            $count = count($toUid);
            // echo "count".$count;
            for($i=0;$i<$count;$i++){
                $b = current($toUid[$i]);
                if($frUid!=$b){
                    if(isset($this->worker->uniConnections[$b])){
                        echo "id存在";
                        $this->worker->uniConnections[$b]->send($data);
                    }else{
                        echo "id不存在";
                    }
                }

            }
        }
    }
}

public function onClose($connection){
    // echo('close');
}

}

372 0 0
0个回答

🔝