Nihat
Nihat

Reputation: 3115

how to add custom data with sails.io handshake query

I would like to send a custom query variable during initial connect by sails.io on client. I added

 io.sails.query = "token=fdbfdsjf"; 

after including the sails.io.js but I don't see it in the handshake data on server, which was:

query: 
   { __sails_io_sdk_version: '0.11.0',
     __sails_io_sdk_platform: 'browser',
     __sails_io_sdk_language: 'javascript',
     EIO: '3',
     transport: 'polling',
     t: '1432569655670-22' }

What is the proper way to add data/variable to handshake on client before sails.io connects to server?

Upvotes: 1

Views: 419

Answers (1)

Nihat
Nihat

Reputation: 3115

I intercepted the window.io() function call to inject my query parameter to the .query property of the options object (second param to .io())

<script> 
         (function () {
            var io;

            Object.defineProperty(window, 'io', {
               get: function get() {
                    return io;
               },
               set: function set(iofunc) {
                  io = function(){
                     var opts = arguments[1];
                     if (typeof opts.query === 'string') {
                        opts.query += '&token=<%=_csrf%>';
                     }
                     return iofunc.apply(this, arguments);
                  };
                }
            });
         })();
      </script>

      <!--SCRIPTS-->
      <script src="/js/dependencies/sails.io.js"></script>

Upvotes: 1

Related Questions