發(fā)送是成功的,就是不能正常即時(shí)收到消息
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連接請(qǐng)求已發(fā)送
chat.vue:1203 getMessage {code: 200, msg: '請(qǐng)求成功', 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: '請(qǐng)求成功', result: '更新私信閱讀狀態(tài)成功'}
chat.vue:1187 心跳發(fā)送成功
chat.vue:1304 img? 0
chat.vue:1308 mTxt 222
chat.vue:1331 res {code: 200, msg: '請(qǐng)求成功', 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 心跳發(fā)送成功
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連接請(qǐng)求已發(fā)送");
},
});
that.websocket.onOpen(function(res) {
that.websocket.send({
data: Nuser,
})
that.HeartBeat();
});
這里粘代碼或配置
00:53:06.365 心跳發(fā)送成功 at pages/chat/chat.vue:1187
后端:
// +----------------------------------------------------------------------
return [
// 擴(kuò)展自身需要的配置
'host' => '0.0.0.0', // 監(jiān)聽(tīng)地址
'port' => 10007, // 監(jiān)聽(tīng)端口
'root' => '', // WEB 根目錄 默認(rèn)會(huì)定位public目錄
'app_path' => '', // 應(yīng)用目錄 守護(hù)進(jìn)程模式必須設(shè)置(絕對(duì)路徑)
'file_monitor' => false, // 是否開(kāi)啟PHP文件更改監(jiān)控(調(diào)試模式下自動(dòng)開(kāi)啟)
'file_monitor_interval' => 2, // 文件監(jiān)控檢測(cè)時(shí)間間隔(秒)
'file_monitor_path' => [], // 文件監(jiān)控目錄 默認(rèn)監(jiān)控application和config目錄
// 支持workerman的所有配置參數(shù)
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => '',
return [
// 擴(kuò)展自身需要的配置
'protocol' => 'websocket', // 協(xié)議 支持 tcp udp unix http websocket text
'host' => '0.0.0.0', // 監(jiān)聽(tīng)地址
'port' => 10007, // 監(jiān)聽(tīng)端口
'socket' => '', // 完整監(jiān)聽(tīng)地址
'context' => [], // socket 上下文選項(xiàng)
'worker_class' => 'app/push/controller/Worker', // 自定義Workerman服務(wù)類(lèi)名 支持?jǐn)?shù)組定義多個(gè)服務(wù)
// 支持workerman的所有配置參數(shù)
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => '',
// 支持事件回調(diào)
// 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 啟動(dòng)命令
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";
}
## 接收消息,接收心跳消息 ,接收用戶(hù)id 信息等等
public function onMessage($connection,$data)
{
// {"form_uid":10952,"to_uid":10903,"sendTxt":"niakd"}
// $this->push();
// echo $data;
//fin1,fin2檢索是否為建立連接
$fin1 = 'oSO';//建立連接
$fin2 = 'uid';//建立連接的用戶(hù)
// $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檢索是否給其他用戶(hù)發(fā)送消息
$fin3 = 'to_uid';//接收用戶(hù)
$fin4 = 'from_uid';//發(fā)送者
// $fin4 = 'sendTxt';//發(fā)送文本
$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){
// }
}
//前端心跳檢測(cè)
$fin5 = 'SocketHeart';//前端心跳檢測(cè)專(zhuān)用
$p5 = strpos($data,$fin5);
if($p5){
$connection->send("SocketHeart");
echo "心跳接收成功";
}
//家族群聊
// $fin3 = 'to_uid';
// $fin4 = 'sendTxt';
$fin6 = 'topic_id';//接收群
$fin7 = 'from_uid';//發(fā)送用戶(hù)
// $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');
}
}