var BCCSDK = { socketClient: null, opts : { debug : 0, port : 2009, }, _event: { }, _reqCallBack: null, init : function(opts){ var _ret = new Object(); if(!opts.ip || !opts.port || !opts.extension){ _ret.code = 10001; _ret.message = '�������Ա���'; return _ret; }else{ for(var opt in opts){ this.opts[opt] = opts[opt]; } } _ret.code = 0; return _ret; }, on(_event, fn){ if(_event === 'connect'){ this._event.connect = fn; }else if(_event === 'disconnect'){ this._event.disconnect = fn; }else if(_event === 'connectError'){ this._event.connectError = fn; }else if(_event === 'statusChange'){ this._event.statusChange = fn; }else if(_event === 'missedCall'){ this._event.missedCall = fn; } }, connect(){ var url = 'http://' + this.opts.ip + ':' + this.opts.port + '/op?enterpriseCode=&token=&ts=&operatorId=' + this.opts.extension; console.log('ws connect', url) if(this.socketClient) { this.socketClient.connect(); }else{ this.socketClient = io.connect(url, {'transports':['websocket']}); var _this = this; this.socketClient.on('connect', function(){ if(_this._event.connect){ _this._event.connect() } //��ȡ��ʼ��״̬ _this.getStatus() }); this.socketClient.on('disconnect', function() { if(_this._event.disconnect){ _this._event.disconnect() } }); this.socketClient.on('connect_error', function(data){ if(_this._event.connectError){ _this._event.connectError() } }); //���ܵ���Ϣ this.socketClient.on('message', function(data) { var resp = JSON.parse(data) if(resp.type === 1 && _this._event.statusChange){ //status change _this._event.statusChange(resp.data) }else if(resp.type === 2 && _this._reqCallBack){ //response data _this._reqCallBack(resp.data); }else if(resp.type === 3 && _this._event.missedCall){ //missed call _this._event.missedCall(resp.data) }else { if(console) { console.log('other msg', resp) } } }); } }, disconnect(){ this.socketClient.disconnect(); }, getStatus(){ var _this = this; this._reqCallBack = function(resp){ if(_this._event.statusChange){ _this._event.statusChange(resp.data) } } this.callApi('get_status', {}); }, login(cb){ this._reqCallBack = cb; this.callApi('login', {number: this.opts.phoneNumber}); }, logout(cb){ this._reqCallBack = cb; this.callApi('logout', {}); }, showIdle(cb){ this._reqCallBack = cb; this.callApi('show_idle', {}); }, showBusy(cb){ this._reqCallBack = cb; this.callApi('show_busy', {}); }, call(number, websId, cb){ this._reqCallBack = cb; this.callApi('callout', {number: number, websId: websId}); }, hungup(cb){ this._reqCallBack = cb; this.callApi('hangup', {}); }, tansfer(phone, cb){ this._reqCallBack = cb; this.callApi('transfer_call', {phone: phone}); }, connu(phone, cb){ this._reqCallBack = cb; this.callApi('consultation', {phone: phone}); }, reslconnu(cb){ this._reqCallBack = cb; this.callApi('release_consultation', {}); }, listen(target, cb){ this._reqCallBack = cb; this.callApi('monitor_call', {targetOperatorId: target}); }, cutIn(target, cb){ this._reqCallBack = cb; this.callApi('insert_call', {targetOperatorId: target}); }, callApi(method, params){ // console.log('call api', method, params) if(this.socketClient){ params.operatorId = this.opts.extension params.method = method console.log(this.socketClient) this.socketClient.emit('message', JSON.stringify(params)); }else{ console.log('ws not connected') } } }