Decode continuation frame in websocket

How do you decode a continuation frame in websocket? Can someone please give me a useful insights into this? Decoding a continuation frame as text frame results in error.

I'm sending a large text-string to the server and I can manage to decode only the first incoming text frame and fails after that.

Here is a simple function in nodejs that handles the text frame decoding -

function decodeWS(data)
{
    var dl = data[1] & 127;
    var ifm = 2;
    if (dl == 126) 
    {
        ifm = 4;
    } else if (dl == 127) 
    {
        ifm = 10;
    }
    var i = ifm + 4;
    var masks = data.slice(ifm,i);
    var index = 0;
    var output = "";
    var l=data.length;
    while (i < l) 
    {
        output += String.fromCharCode(data[i++] ^ masks[index++ % 4]);
    }
    return output;
}