// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html const Socket = require('sock'); //const Base64 = require('../utils/base64'); cc.Class({ extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, url: "" }, ctor(){ this.sock = null; }, setCallback(cb){ this.cb = cb; }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start () { }, // update (dt) {}, setHost(url){ this.url = url; }, connect(){ if(this.sock){ this.sock = null; } this.sock = new Socket(this, this.url); }, send(msg){ this.sock && this.sock.send(msg); }, disconnect(){ this.sock && this.sock.close(); }, onOpen(url){ this.cb && this.cb.onConnect(); }, onClose(url, errevent){ this.cb && this.cb.onClose(errevent); }, onError(url, errevent){ this.cb && this.cb.onError(errevent); }, onMessage(msg){ //var bs = Base64.arrayBufferToBase64(msg); //console.log(bs); this.cb && this.cb.onRecv(msg); } });