在类

时间:2017-06-19 02:49:09

标签: javascript websocket

我在另一个方法中调用类方法时遇到问题。

在我指定的构造函数中

this.self = this 

在使用notify()方法调用时修复了它,但是当我尝试调用

this.self.convo(id, data2.from); 

当我收到消息时,在GoodStuff内部,我得到一个TypeError。

我也遇到了麻烦但放弃了,但这是一个相关的问题。

count(){
    console.log(this.users);
    console.log(this.subbed,"total users");
}
  

5575257,     5163800]   未定义的'总用户'

它表示未定义的子变量

  

TypeError:无法读取属性' convo'未定义的

class Socket {
        constructor(id) {
            this.self = this;
            this.subbed = 0;
            this.id = id;
            this.users = [];
            this.ws = new WebSocket('ws://echo.websocket.org');
            this.ws.on('message', this.theGoodStuff);
            console.log('readystate',this.ws.readyState);
            console.log("created socket with user", id);
        }
        send(msg){
            let soc = this.ws;
            if(soc.readyState){
                console.log("socket open, sending..");
                this.ws.send(msg);
            } else {
                console.log("socket not open, waiting..");
                soc.on('open', function(){
                    console.log("socket now open, sending..");
                    soc.send(msg);
                });
            }
        }
        convo(to, from) {
            this.ws.send('{"event":"pusher:subscribe","data":{"channel":"inbox_convo_' + to + '-' + from + '"}}');
        }
        notify(user) {
            this.users.push(user);
            this.self.send('{"event":"pusher:subscribe","data":{"channel":"user_notifications_' + user +'"}}');
        }

        theGoodStuff(json, flags) {
            try {
                var data = JSON.parse(json)
                ,   data2 = JSON.parse(data.data);
                if( data.channel ) {
                    var user = /inbox-(.*)/g.exec(data.channel) ? /inbox-(.*)/g.exec(data.channel)[1] : /user_notifications_(.*)/g.exec(data.channel)[1];
                }
                var id = parseInt(user);
                switch( data.event ){
                    case "pusher_internal:subscription_succeeded":
                        this.subbed++;
                        break;
                    case "pusher:connection_established":
                        break;
                    case "notification":
                        if( data2.notify_type ){
                            switch( data2.notify_type ){
                                case "notify_inbox":
                                // New Convo
                                    this.self.convo(id, data2.from);
                                    this.self.convo(data2.from, id);
                                    break;
                                default:
                                    break;
                            }
                        } 
                        break;
                    default:
                        console.log(data);
                        console.log(data2);
                        break;
                }
            } catch (e) {
                switch ( e.constructor ) {
                    case SyntaxError:
                        break;
                    default:
                        console.log(e);
                }
            }
        }

    }

    module.exports = Socket;

谢谢,非常感谢任何帮助!

0 个答案:

没有答案