qx.Class.define(
"myapp.api.WebSocket"
,{
extend : qx.core.Object,
events :{
"connect"
:
"qx.event.type.Event"
,
"connecting"
:
"qx.event.type.Data"
,
"connect_failed"
:
"qx.event.type.Event"
,
"message"
:
"qx.event.type.Data"
,
"close"
:
"qx.event.type.Data"
,
"disconnect"
:
"qx.event.type.Event"
,
"reconnect"
:
"qx.event.type.Data"
,
"reconnecting"
:
"qx.event.type.Data"
,
"reconnect_failed"
:
"qx.event.type.Event"
,
"error"
:
"qx.event.type.Data"
},
properties:{
url:{
nullable:
false
,
init:
"http://"
+window.location.hostname+
"/"
,
check:
"String"
},
port:{
nullable:
false
,
init: 8080,
check:
"Number"
},
namespace:{
nullable:
true
,
init:
""
,
check:
"String"
},
socket:{
nullable:
true
,
init:
null
,
check:
"Object"
},
reconnect:{
nullable:
true
,
init:
true
,
check:
"Boolean"
},
connectTimeout:{
nullable:
true
,
init: 10000,
check:
"Number"
},
reconnectionDelay:{
nullable:
false
,
init: 500,
check:
"Number"
},
maxReconnectionAttemps:{
nullable:
false
,
init: 1000,
check:
"Number"
}
},
construct:
function
(namespace){
this
.base(arguments);
if
(namespace !==
null
){
this
.setNamespace(namespace);
}
this
.__name = [];
},
members:{
__name :
null
,
connect:
function
(){
if
(
this
.getSocket() !=
null
){
this
.getSocket().removeAllListeners();
this
.getSocket().disconnect();
}
this
.setSocket(io.connect(
this
.getUrl()+
this
.getNamespace(), {
'port'
:
this
.getPort(),
'reconnect'
:
this
.getReconnect(),
'connect timeout'
:
this
.getConnectTimeout(),
'reconnection delay'
:
this
.getReconnectionDelay(),
'max reconnection attempts'
:
this
.getMaxReconnectionAttemps(),
'force new connection'
:
true
}));
this
.on(
"connect"
,
function
(){
this
.fireEvent(
"connect"
); },
this
);
this
.on(
"connecting"
,
function
(e){
this
.fireDataEvent(
"connecting"
, e); },
this
);
this
.on(
"connect_failed"
,
function
(){
this
.fireEvent(
"connect_failed"
); },
this
);
this
.on(
"message"
,
function
(e){
this
.fireDataEvent(
"message"
, e); },
this
);
this
.on(
"close"
,
function
(e){
this
.fireDataEvent(
"close"
, e); },
this
);
this
.on(
"disconnect"
,
function
(){
this
.fireEvent(
"disconnect"
); },
this
);
this
.on(
"reconnect"
,
function
(e){
this
.fireDataEvent(
"reconnect"
, e); },
this
);
this
.on(
"reconnecting"
,
function
(e){
this
.fireDataEvent(
"reconnecting"
, e); },
this
);
this
.on(
"reconnect_failed"
,
function
(){
this
.fireEvent(
"reconnect_failed"
); },
this
);
this
.on(
"error"
,
function
(e){
this
.fireDataEvent(
"error"
, e); },
this
);
},
emit:
function
(name, jsonObject){
this
.getSocket().emit(name, jsonObject);
},
on:
function
(name, fn, that){
this
.__name.push(name);
if
(
typeof
(that) !==
"undefined"
&& that !==
null
){
this
.getSocket().on(name, qx.lang.Function.bind(fn, that));
}
else
{
this
.getSocket().on(name, fn);
}
}
},
destruct :
function
(){
if
(
this
.getSocket() !=
null
){
if
(
this
.__name !==
null
&&
this
.__name.length >= 1){
for
(
var
i=0; i<
this
.__name.length; ++i){
this
.getSocket().removeAllListeners(
this
.__name[i]);
}
}
this
.__name =
null
;
this
.removeAllBindings();
try
{
this
.getSocket().socket.disconnect();
}
catch
(e) {}
try
{
this
.getSocket().disconnect();
}
catch
(e) {}
this
.getSocket().removeAllListeners(
"connect"
);
this
.getSocket().removeAllListeners(
"connecting"
);
this
.getSocket().removeAllListeners(
"connect_failed"
);
this
.getSocket().removeAllListeners(
"message"
);
this
.getSocket().removeAllListeners(
"close"
);
this
.getSocket().removeAllListeners(
"disconnect"
);
this
.getSocket().removeAllListeners(
"reconnect"
);
this
.getSocket().removeAllListeners(
"reconnecting"
);
this
.getSocket().removeAllListeners(
"reconnect_failed"
);
this
.getSocket().removeAllListeners(
"error"
);
}
}
});