extraHeaders doesnt work in socket.io-client

8.4k Views Asked by At

Hi my code looks like this in nodejs, it works!

but when i try to use exactly same code in nwjs app, it doesn't! because as i sniff and see it thinks it runs under a web browser and it tries to use native functions of nwjs's chromium browser.

function connecttest(domain)
{
socket = require('socket.io-client')('https://ws.'+domain.replace('www.').replace('m.'),{
  forceNode:true,
  transports: ['websocket'],
  extraHeaders: {
      'Origin': 'https://www.'+domain.replace('www.').replace('m.')
  },
  transportOptions: {
    polling: {
      extraHeaders: {
        'Origin': 'https://www.'+domain.replace('www.').replace('m.')
      }
    }
  }
});
socket.on('connect',  function(socket){
    console.log("ok");
socket.emit('query', {"body":{"siteId":"9","source":1,"lang":"tr"},"action":"site.session","frontEndId":0,"token":null});
socket.on('response', function(data){
console.log(data);
});

});
}

fiddler sniff

as you see it can not set a right origin header

but

but it works under plain - nodejs script

1

There are 1 best solutions below

0
On

Perhaps it is your transports: ['websocket']. Look here, at the official documentation:

With extraHeaders

This only works if polling transport is enabled (which is the default). Custom headers will not be appended when using websocket as the transport. This happens because the WebSocket handshake does not honor custom headers. (For background see the WebSocket protocol RFC)