function websocketClass(a) {
var b = this;
this.wsobj = !1,
this.wshost = "",
this.onopen = function () {},
this.onmessage = function () {},
this.onclose = function () {},
this.onerror = function () {},
this.reimfrom = "rockdemo",
this.adminid = "1",
this.sendname = "1",
this._init = function () {
if (a) for (var c in a) this[c] = a[c];
"undefined" == typeof WebSocket ? (WEB_SOCKET_SWF_LOCATION = "res/swf/WebSocketMain.swf", WEB_SOCKET_DEBUG = !0, $.getScript("res/js/swfobject.js", function () {
b._contect()
})) : this._contect()
},
this._contect = function () {
this.wsobj = new WebSocket(this.wshost),
this.wsobj.onopen = function (a) {
b._onopen(a)
},
this.wsobj.onmessage = function (a) {
b._onmessage(a)
},
this.wsobj.onclose = function (a) {
b._onclose(a)
},
this.wsobj.onerror = function (a) {
b._onerror(a)
}
},
this.connect = function () {
this._contect()
},
this._onopen = function (a) {
this.onopen(this, a),
this.send({
atype: "connect"
})
},
this._onmessage = function (a) {
var b = a.data;
this.onmessage(b, this)
},
this._onclose = function (a) {
this.onclose(this, a)
},
this._onerror = function (a) {
this.onerror(this, a)
},
this.send = function (a) {
var b = this.objecttostr(a);
return this.wsobj.send(b)
},
this.objecttostr = function (a) {
var b, d, c = "",
e = js.apply({
from: this.reimfrom,
adminid: this.adminid,
atype: "send",
sendname: this.sendname
}, a);
for (b in e) d = e[b],
c += ',"' + b + '":"' + d + '"';
return "" != c && (c = c.substr(1)),
"{" + c + "}"
},
this._init()
}
使用
websocketobj = new websocketClass({
adminid:adminid,
reimfrom:fromrecid,
wshost:fromwshost,
sendname:adminname,
onerror:function(o,ws){
connectbool=false;
js.msg('msg','无法连接服务器1<br><span id="lianmiao"></span><a href="javascript:;" onclick="return connectserver()">[重连]</a>',-1);
relianshotime(30);
},
onmessage:function(str){
connectbool=true;
var a=js.decode(str);
im.receivemesb(a);
},
onopen:function(){
connectbool=true;
js.msg('none');
im.initnotify();
},
onclose:function(){
if(otherlogin)return;
connectbool=false;
js.msg('msg','连接已经断开了<br><span id="lianmiao"></span><a href="javascript:;" onclick="return connectserver()">[重连]</a>',-1);
notifyobj.playerrsound();
relianshotime(30);
}
});