function InternalSocket (url) {
this.url = url;
this._socket = null;
this.handlers = {};
this._heartBeatTimer = null;
this._timeout = null;
this._first = 1;
this._retryTimes = 0;
this._retry = 1;
};
InternalSocket.prototype.setRetry = function(isRetry){
this._retry = isRetry;
};
InternalSocket.prototype.getRetry = function(){
return this._retry;
};
InternalSocket.prototype.connect = function () {
var self = this;
cc.vv.log(self.url);
self._socket = new WebSocket(self.url);
self._socket.binaryType = "arraybuffer"; //blob
self._socket.onopen = self.onopen.bind(this);
self._socket.onmessage = self.onmessage.bind(this);
self._socket.onerror = self.onerror.bind(this);
self._socket.onclose = self.onclose.bind(this);
};
InternalSocket.prototype.onopen = function (event) {
cc.vv.log("client connection establish.");
var self = this;
self.clearHBTimer();
self.heartBeat();
if (this._first) {
this._first = 0;
self._handle(cc.vv.SYS_MSG.CONNECT, {});
}
else {
self._handle(cc.vv.SYS_MSG.RECONNECT, {});
}
this._retryTimes = 0;
};
InternalSocket.prototype.onmessage = function (event) {
//cc.vv.log("recv data " + event.type);
var self = this;
var orig_buf = event.data;
if (cc.sys.isNative) {
self.onmessageNative(orig_buf);
}
else {
self.onmessageOther(orig_buf);
}
};
InternalSocket.prototype.onerror = function (event) {
var self = this;
cc.vv.log("websocket error . State : " + self.readyState());
cc.vv.log(JSON.stringify(event));
//if (event.type == "error") {};
self.clearHBTimer();
if (!!self._socket)
self._handle(cc.vv.SYS_MSG.ERROR, { readyState: self._socket.readyState });
};
InternalSocket.prototype.onclose = function (event) {
var self = this;
cc.vv.log("websocket close . State : " + self.readyState());
cc.vv.log(JSON.stringify(event));
self.clearHBTimer();
self._handle(cc.vv.SYS_MSG.CLOSE, {});
self._socket = null;
if (self._retry && self._retryTimes < cc.vv.MAX_RECONNECT_TIMES) {
self._retryTimes++;
setTimeout(function ()