import Vue from 'vue';
import store from '@/store';
import { mapState } from 'vuex';
import Socket from "@/common/sockjs.min.js";
import { Stomp } from "@/common/stomp.js" ;
export const connentURL= "";
var stompClient = null;
var wsCreateHandler="";
class mystomp{
connent(){
var wss = new Socket(connentURL);
stompClient = Stomp.over(wss);
stompClient.heartbeat.outgoing = 3000;
stompClient.heartbeat.incoming=3000;
stompClient.connect();
var userId = uni.getStorageSync('userid');
stompClient.connect({userId: userId,paltform: "app"}, function (val) {
//1./exchange/<exchangeName>单聊交换机临时队列监听
stompClient.subscribe("/queue/privateChat." + userId, function (MessageItem) {
console.info(MessageItem,'收到的消息');
//定时器
//#ifdef APP-PLUS || APP-VUE || APP-NVUE
plus.runtime.setBadgeNumber(1);
//#endif
store.commit("setMsgStatus",MessageItem.data);
//确认保持链接
store.commit("setIsSocket", false);
store.commit("setCurrentMsg",MessageItem.data);
store.commit("setCurrentMsgchat",MessageItem.data);
//更新会话
store.commit("setBoom",MessageItem.data);
//#ifdef APP-PLUS
let system = MessageItem.data.msg.userinfo.name ? MessageItem.data.msg.userinfo.name + '给您发了消息。' : '您有一条新消息';
plus.push.createMessage(system, "", "");
//#endif
//处理完成返回 不完成不返回
data.ack();
},{ack:'client'});
//消息发送后回调确认消息发送成功
stompClient.subscribe("/exchange/chat.sendACK/" + userId, function (res) {
console.log("code:" + res.body)
//code返回,发送成功
});
//发送获取
stompClient.send("/app/getOflineMsg", {}, userId);
//消息发送成功确认回调
}, function (error) {
let that =this;
//如果在执行则清除任务再执行
wsCreateHandler && clearTimeout(wsCreateHandler);
wsCreateHandler = setTimeout(function () {
console.log("重连中...");
this.$mystomp.connentc();
// console.log("连接完成");
}, 3000); //3秒一次重连
console.log("连接完成");
}
)
}
//发送给某人
sendToUser(data) {
stompClient.send("/app/sendToUser", {},data);
}
close() {
if (stompClient != null) {
stompClient.disconnect();
}
}
}
export default new mystomp()
4 个回复
无锡小王子 (作者)
import Vue from 'vue';
import store from '@/store';
import { mapState } from 'vuex';
import Socket from "@/common/sockjs.min.js";
import { Stomp } from "@/common/stomp.js" ;
export const connentURL= "";
var stompClient = null;
var wsCreateHandler="";
class mystomp{
connent(){
var wss = new Socket(connentURL);
stompClient = Stomp.over(wss);
stompClient.heartbeat.outgoing = 3000;
stompClient.heartbeat.incoming=3000;
stompClient.connect();
var userId = uni.getStorageSync('userid');
stompClient.connect({userId: userId,paltform: "app"}, function (val) {
//1./exchange/<exchangeName>单聊交换机临时队列监听
stompClient.subscribe("/queue/privateChat." + userId, function (MessageItem) {
console.info(MessageItem,'收到的消息');
//定时器
//#ifdef APP-PLUS || APP-VUE || APP-NVUE
plus.runtime.setBadgeNumber(1);
//#endif
store.commit("setMsgStatus",MessageItem.data);
//确认保持链接
store.commit("setIsSocket", false);
}
export default new mystomp()
3***@qq.com - 哈哈大家好
楼主解决了吗
无锡小王子 (作者)
我换腾讯聊天啦
2021-08-09 17:02
lisiurday
原因是 sockjs 内部使用了 json3 这个库,该库内部有这么一段代码:
微信真机环境在执行这段代码时,window 是 undefined 的,所以 root 值为 this 也即空对象,后续取到的 Object 是 undefined,再取prototype时就报错了。
我的临时解决方案就是不用 json3 ,也即把源码里所有 require('json3') 换成原生 JSON 对象,然后重新打包(顺便也把不用的transport 删掉了,减少了点体积)。
无锡小王子 (作者)
空了 我也试试看
2021-08-09 17:02
wuyi
这个问题解决了吗