Skip to content

Instantly share code, notes, and snippets.

@AtsushiM
Created June 30, 2013 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtsushiM/5894206 to your computer and use it in GitHub Desktop.
Save AtsushiM/5894206 to your computer and use it in GitHub Desktop.
Socket.ioでRequest-Responseっぽい何かを作ってみる。
// SocketRequest
var SocketRequest =
C['SocketRequest'] = classExtendObserver({
'init': function(config) {
config = copyObject(config);
var that = this;
that._request_id = SocketRequest._id++;
that._responseFunc = function(id, vars) {
if (that._request_id === id) {
that['stop']();
that['fire']('complete', vars);
}
};
that._ev_res = config['responseEvent'] || SocketRequest['responseEvent'];
that._ev_req = config['requestEvent'] || SocketRequest['requestEvent'];
that._socket = config['socket'] || SocketRequest['socket'] || (SocketRequest['socket'] = io['connect']());
delete config['socket'];
config['responseEvent'] = that._ev_res;
that._socket['on'](that._ev_res, that._responseFunc);
that['_super']();
bindOnProp(that, config);
that._config = config;
ifManualStart(that, config);
},
'start': function() {
var that = this;
fire_start(that);
that._socket['emit'](that._ev_req, that._request_id, that._config);
},
'stop': function() {
var that = this;
that['fire']('stop');
that._socket['disconnect'](that._ev_res, that._responseFunc);
}
});
SocketRequest._id = 0;
/* SocketRequest['socket'] = NULL; */
SocketRequest['responseEvent'] = 'CIRSocket-Response';
SocketRequest['requestEvent'] = 'CIRSocket-Request';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment