0

I am creating a single page web app using web sockets as the transport which will use a session ID to verify user is authenticated, I have managed to get session ID value from server response but cannot pass it out of inline JS function initiated by message received by web sockets. How do I do this?

Sample of code

socket.onmessage = function(e) {
if (debugMode) {
    console.log('NOTIFICATION : Message received from the server "' + e.data + '."');
    if (e.data.startsWith("RID")) {
        message = e.data.split(',');
    }
    if (message[0].startsWith('RID:')) {
        if (debugMode) {
            console.log('NOTIFICATION : RID for message is "' + message[0].split(':')[1] + '".');
        }
        for (var iSMR = 0; iSMR < activeRequests.length; iSMR++) {
            if (activeRequests[iSMR].startsWith(message[0].split(':')[1])) {
                if (debugMode) {
                    console.log('NOTIFICATION : Message received from server is a response to 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 successfully 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];
    }
}
KAD
  • 10,972
  • 4
  • 31
  • 73
  • What is the logic behind retrieving the session ID from `e.data?` – bflemi3 Nov 08 '16 at 13:46
  • The logic is e.data represents the message data sent over web socket connection by the server, it is in string format similar to JSON so is easily parsed to get values – Allan Macritchie Nov 08 '16 at 16:42
  • 1
    I understand that, but where are the values you need located in e.data. What do they look like? Why is the for loop needed? It looks like the sessionID is set from `message[2].split(':')[1]` no matter what. – bflemi3 Nov 09 '16 at 14:52

1 Answers1

0

Without more of an explanation of what you're trying to accomplish, I'm going to guess that you're trying to set the html of colorbox to include the session ID. From your comment below I've updated the example code to better reflect your problem. Hopefully I'm closer.

I can give you a better explanation if you can tell me what the logic is behind retrieving the session ID from e.data (the logic in the code above is unclear)? For now, please see below.

var sessionID;
socket.onmessage = function(e) {
    // if we're not in debugMode then short circuit, we have nothing to do
    if(!debugMode) return;

    var message = e.data.split(',') || [];

    // if the split was unsuccessful then just return, nothing we can do
    if(!message.length) return;

    var parts = message[0].split(':');
    if(parts && parts.length) {
        for (var i = 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;
        } 
    }

    // I don't understand this logic? You're just setting sessionID to the same value as above.
    // Does this need to be here at all?
    if(!sessionID) {
        var parts = message[2].split(':');
        if(!parts || !parts.length) return;

        // this is the sessionID 
        sessionID = parts[1];
    }

    // finally, set the colorbox
    $.colorbox({
        html: '<h2 class="text-center">User Authenticated</h2><p class="text-center">Session token "' + sessionID + '".</p>'
    });
}
bflemi3
  • 6,698
  • 20
  • 88
  • 155
  • Unfortunately not, the colorbox code is simplyto have an alert type message displayed in a consistant manner across browsers – Allan Macritchie Nov 08 '16 at 16:40
  • What I need to do is store the sessionID for reuse with other messages to the server over sockets – Allan Macritchie Nov 08 '16 at 16:41
  • I updated the code example to remove the session id out of the scope of the onmessage handler. Not sure in what scope you need the sessionID though. – bflemi3 Nov 09 '16 at 14:56
  • Issue resolved by refactoring whole code block, turns out my switch wasn't functioning correctly so was overwriting sessionID – Allan Macritchie Nov 15 '16 at 10:18