javascript - Pass session ID variable out if inline JS function to allow it to be used elsewhere -
this question has answer here:
- how return response asynchronous call? 21 answers
i creating single page web app using web sockets transport use session id verify user authenticated, have managed session id value server response cannot pass out of inline js function initiated message received web sockets. how do this?
sample of code
socket.onmessage = function(e) { if (debugmode) { console.log('notification : message received server "' + e.data + '."'); if (e.data.startswith("rid")) { message = e.data.split(','); } if (message[0].startswith('rid:')) { if (debugmode) { console.log('notification : rid message "' + message[0].split(':')[1] + '".'); } (var ismr = 0; ismr < activerequests.length; ismr++) { if (activerequests[ismr].startswith(message[0].split(':')[1])) { if (debugmode) { console.log('notification : message received server response request "' + message[0].split(':')[1] + '".'); if (activerequests[ismr].split(':')[1] === "authenticate") { if (message[1] === 'ok') { var sessionid = message[2].split(':')[1]; console.log('notification : session id token "' + sessionid + '".'); console.log('notification : agent authenticated'); } } $.colorbox({ html: '<h2 class="text-center">user authenticated</h2><p class="text-center">session token "' + sessionid + '".</p>' }) } } } } if (sessionid === null) { return sessionid = message[2].split(':')[1]; } }
without more of explanation of you're trying accomplish, i'm going guess you're trying set html of colorbox include session id. comment below i've updated example code better reflect problem. i'm closer.
i can give better explanation if can tell me logic behind retrieving session id e.data (the logic in code above unclear)? now, please see below.
var sessionid; socket.onmessage = function(e) { // if we're not in debugmode short circuit, have nothing if(!debugmode) return; var message = e.data.split(',') || []; // if split unsuccessful return, nothing can if(!message.length) return; var parts = message[0].split(':'); if(parts && parts.length) { (var = 0, request; request = activerequests[i]; i++) { if(parts[1] && !request.startswith(parts[1])) continue; if (request.split(':')[1] !== 'authenticate' || message[1] !== 'ok' || !message[0].startswith('rid:')) continue; sessionid = message[2].split(':')[1]; break; } } // don't understand logic? you're setting sessionid same value above. // need here @ all? if(!sessionid) { var parts = message[2].split(':'); if(!parts || !parts.length) return; // sessionid sessionid = parts[1]; } // finally, set colorbox $.colorbox({ html: '<h2 class="text-center">user authenticated</h2><p class="text-center">session token "' + sessionid + '".</p>' }); }
Comments
Post a Comment