Socket.io Client doesn't (re)emit without page reload
See original GitHub issueTL;DR; I can connect to the node.js server and retrieve my data when I click on <button>Get Data</button>
. But nothing is emitted if I click on that button again.
I’m not sure if this is a bug, but using socket.io 1.0.4 . . .
… on document ready, my client connects to the node socket.io server to get the client .js file:
/*** Attempt to get the socket.io.js file from the node listener. ***/
var svrPort = 8072
var scriptPath = "https://"+location.host+":"+svrPort+'/socket.io/socket.io.js';
jQuery.getScript( scriptPath, function( data, textStatus, jqxhr ) {
if (jqxhr.status == 200) {
listenerUp = true;
console.debug( "socket.io.js file received." );
}
});
The page has a <button id='RS_Refresh'>Get Data</button>
that has an event trigger defined at:
jQuery('#RS_Refresh').click(function(){
get_AllRS(true);
});
The get_ALLRS()
function does indeed establish a connection to the node server, and on connect, emits a request for the data to be displayed on the web page, which works perfectly:
/* Some code omitted for brevity */
function get_AllRS(showSpinner) {
var getAllRS_sock = io.connect(svr,options);
var toListener =
{
'cType' : 'get_AllRS',
'DEBUG' : RS_DEBUG,
'clientID' : clientID,
'userID' : userID,
};
/* On second attempt, this gets executed, but no request is actually
emitted - no traffic seen in Firebug's 'Net' tab */
if (getAllRS_sock.connected) {
console.log('emitting. . .');
getAllRS_sock.emit('get_AllRS',toListener) ;
}
/*** When connected, send get_AllRS ***/
getAllRS_sock.on('connect', function getAllRS_sock_on_connect() {
console.log('In getAllRS_sock_on_connect()');
/*** Let's talk to the server ***/
getAllRS_sock.emit('get_AllRS',toListener) ;
});
/*** Mission Accomplished ***/
getAllRS_sock.on('got_AllRS', function getAllRS_sock_on_got_AllRS(incoming) {
console.log('In getAllRS_sock_on_got_AllRS()');
console.debug('=== got_AllRS incoming ======================');
console.debug(incoming)
console.debug('=============================================');
if (typeof(incoming) !== 'undefined'){
populateRSFields(incoming);
setRSAlert('Fields Retrieved successfully','flash_success');
}
else{
revertValues();
setRSAlert('Fields were NOT retrieved','flash_error');
}
/* Should I disconnect from the node server?? Doesn't work either way */
// getAllRS_sock.emit('forceDisconnect',{}) ;
// getAllRS_sock.destroy();
});
}
However, if I click on the Get Data button again, nothing is emitted, according to the ‘Net’ tab in Firebug and node.js doesn’t hear/see the second request in DEBUG=* mode, … until I refresh the entire web page.
What gives?
Should I emit to the server to disconnect the client (i.e. .emit('forceDisconnect',{}
)? (The problem seems to persist either way, unless I’m doing it wrong.)
Is my approach incorrect? Or maybe you could show me an example of how you made this work?
Issue Analytics
- State:
- Created 9 years ago
- Comments:12 (1 by maintainers)
@Haideralee please make sure the listener is properly initialized in your
Ctrl 1
.Closing, as the initial issue should be fixed by now.
I am facing the same issue but in my case the problem in
on
section, emit is working fine but the listen is not working as expected. Ex:the confusing thing is on running in
Ctrl 2
but not listen inCtrl 1