Search Unity

Accessing session cookie with WebGL build

Discussion in 'Web' started by notprathap, Jun 14, 2015.

  1. notprathap

    notprathap

    Joined:
    Jan 18, 2013
    Posts:
    9
    Hi,

    We have a game running on the unity web player, embedded in our website. The user logs in to the website, and the game then uses the session cookie created to authenticate itself. This is achieved by the javascript injecting the session cookie into the native plugin, as specified by http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html

    Everything works great now, but now that NPAPI support is being deprecated, we want to switch over to using WebGL. The trouble is, the javascript generated by WebGL build does not have an interface to plug in the session cookie, and so we are unable to find a way to inject session cookies into WebGL builds. Has anyone else faced this issue? Thanks.
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
  3. notprathap

    notprathap

    Joined:
    Jan 18, 2013
    Posts:
    9
    Great, thank you.
     
  4. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    My javascript code to save and read cookies dont work on WebGL, anybody knows why? here is the code

    Code (JavaScript):
    1.     setCookie: function (cname, cvalue, exdays) {
    2.        var d = new Date();
    3.        d.setTime(d.getTime() + (exdays*24*60*60*1000));
    4.        var expires = "expires="+ d.toUTCString();
    5.        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    6.        console.log('set cookie='+document.cookie);
    7.     },
    8.  
    9.     getCookie: function (cname) {
    10.        var ret="";
    11.        var name = cname + "=";
    12.        var decodedCookie = decodeURIComponent(document.cookie);
    13.        console.log('get cookie='+decodedCookie);
    14.        var ca = decodedCookie.split(';');
    15.        for(var i = 0; i <ca.length; i++) {
    16.            var c = ca[i];
    17.            while (c.charAt(0) == ' ') {
    18.                c = c.substring(1);
    19.            }
    20.            if (c.indexOf(name) == 0) {
    21.                ret=c.substring(name.length, c.length);
    22.                break;
    23.            }
    24.        }
    25.        var buffer = _malloc(lengthBytesUTF8(ret) + 1);
    26.        writeStringToMemory(ret, buffer);
    27.        return buffer;
    28.     },
     
  5. Dustin_00

    Dustin_00

    Joined:
    Feb 26, 2013
    Posts:
    14
    Hello nsmith1024, your javascript cookie functions aren't working because you need to cast your function input strings like so:

    setCookie:
    ...
    document.cookie = Pointer_stringify(cname) + "=" + Pointer_stringify(cvalue) + expires + ";path=/";

    and getCookie:
    ...
    var name = Pointer_stringify(cname) + "=";
     
    tezza2k1 likes this.