无锡小王子
无锡小王子
  • 发布:2020-10-21 09:11
  • 更新:2022-06-02 14:39
  • 阅读:1524

uniapp stomp 使用报错

分类:uni-app

TypeError: Cannot read property 'prototype' of undefined,这样的报错有没有人遇到过?

2020-10-21 09:11 负责人:无 分享
已邀请:
无锡小王子

无锡小王子 (作者)

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()

3***@qq.com

3***@qq.com - 哈哈大家好

楼主解决了吗

lisiurday

lisiurday

原因是 sockjs 内部使用了 json3 这个库,该库内部有这么一段代码:

var root = objectTypes[typeof window] && window || this  
...  
Object = context.Object || root.Object  
...  
objectProto = Object.prototype

微信真机环境在执行这段代码时,window 是 undefined 的,所以 root 值为 this 也即空对象,后续取到的 Object 是 undefined,再取prototype时就报错了。

我的临时解决方案就是不用 json3 ,也即把源码里所有 require('json3') 换成原生 JSON 对象,然后重新打包(顺便也把不用的transport 删掉了,减少了点体积)。

wuyi

wuyi

这个问题解决了吗

要回复问题请先登录注册