0

I'm building some testing tool for SocketIO, that will allow a user to connect to any SocketIO back-end.

When i try connecting to a different domain, i get the CORS error, as expected.

Being that i do not control the server side in this case, the standard solution of adding headers is irrelevant.

Is there any way to overcome it on the front side, whether through SocketIO itself, or just plain JS?

This is the basic code i'm using to connect, when my React component mounts:

connect = (address) => {
    console.log('connecting');
    this.setState(()=>({
      connectionStatus:'connecting'
    }))

    if (socket) {
      socket.disconnect();
    }

    socket = window.socket = io(address);

      socket.on('connect', () => {
      console.log('connected!')
      this.setState({
        connectionStatus: 'connected',     
      })

    });
i.brod
  • 3,993
  • 11
  • 38
  • 74
  • can you show your code ? – Googlian Feb 26 '19 at 18:15
  • Added some code, though i aint sure how it would help :D – i.brod Feb 26 '19 at 18:17
  • Did you use headers ? – Googlian Feb 26 '19 at 18:19
  • 3
    If the server doesn't allow cross-origin requests, you won't be able to access it from another domain. It's the server that must allow this, it's not something you can configure on the client. That being said there are cases where you can't access a CORS-enabled server from a client for a number of reasons, hopefully if that's your problem someone can point you in the right direction. – nicholaswmin Feb 26 '19 at 18:21
  • So there is no way around it? – i.brod Feb 26 '19 at 18:24
  • Not that I'm aware of. If you find one then that would be a security flaw. – nicholaswmin Feb 26 '19 at 18:25
  • 1
    In many cases, you can make the request through a CORS proxy, which will inject the necessary response headers into the response the browser receives. See the *How to use a CORS proxy to get around “No Access-Control-Allow-Origin header” problems* section of the answer at https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141. – sideshowbarker Feb 28 '19 at 10:55

0 Answers0