Created
June 30, 2013 07:15
-
-
Save AtsushiM/5894206 to your computer and use it in GitHub Desktop.
Socket.ioでRequest-Responseっぽい何かを作ってみる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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