admin管理员组

文章数量:1296509

I have two subdomains:

  • socket.mydomain - The Socket.IO server
  • app.mydomain - A web app that I'd like to connect to my web socket.

In the landing page for app.mydomain, I've linked in the Socket.IO client script, and successfully created an IO object, like so:

<script src=.io/socket.io.js></script>

<script type=text/javascript>
  const socket = io();
  socket.on('message', data => console.log(data));
</script>

However, rather than trying to connect to socket.mydomain, the client is trying to connect to app.mydomain. Because there's no socket at app.mydomain, it fails and keeps retrying.

Is there a way to connect my app at app.mydomain to my socket at socket.mydomain? Or is this impossible?

Update

Most all of the existing answers relating to this question now use outdated code, because Socket.IO has recently upgraded to 1.0 (1.4 in fact). However, even taking these code changes into account, it seems like Socket.IO doesn't allow what I'm trying to do.

When Socket.IO makes the initial connection to the server, it sends an XHR with the withCredentials settings set to true. But you can't have withCredentials set to true and allow CORS on the server. So it seems my question is actually, 'Is there a way around this problem?'

I have two subdomains:

  • socket.mydomain. - The Socket.IO server
  • app.mydomain. - A web app that I'd like to connect to my web socket.

In the landing page for app.mydomain., I've linked in the Socket.IO client script, and successfully created an IO object, like so:

<script src=https://socket.mydomain./socket.io/socket.io.js></script>

<script type=text/javascript>
  const socket = io();
  socket.on('message', data => console.log(data));
</script>

However, rather than trying to connect to socket.mydomain., the client is trying to connect to app.mydomain.. Because there's no socket at app.mydomain., it fails and keeps retrying.

Is there a way to connect my app at app.mydomain. to my socket at socket.mydomain.? Or is this impossible?

Update

Most all of the existing answers relating to this question now use outdated code, because Socket.IO has recently upgraded to 1.0 (1.4 in fact). However, even taking these code changes into account, it seems like Socket.IO doesn't allow what I'm trying to do.

When Socket.IO makes the initial connection to the server, it sends an XHR with the withCredentials settings set to true. But you can't have withCredentials set to true and allow CORS on the server. So it seems my question is actually, 'Is there a way around this problem?'

Share Improve this question edited Apr 13, 2016 at 17:08 dwhieb asked Apr 13, 2016 at 6:22 dwhiebdwhieb 1,83620 silver badges31 bronze badges 7
  • Possible duplicate of Cross Domain Socket.Io – Michał Perłakowski Commented Apr 13, 2016 at 6:23
  • Try to set the origins on the server-side: io.set('origins', 'domain or IP here'); where io is your own defined variable. – node_modules Commented Apr 13, 2016 at 6:23
  • Thanks @Gothdo - checking that out now. Will post here or delete my question if that works. – dwhieb Commented Apr 13, 2016 at 6:25
  • On the client side, I tried var socket = io.connect('socket.mydomain.'), and now I receive the following error: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'app.mydomain.' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute. – dwhieb Commented Apr 13, 2016 at 6:43
  • @Gothdo just posted an update. – dwhieb Commented Apr 13, 2016 at 17:09
 |  Show 2 more ments

1 Answer 1

Reset to default 9

To acplish what I wanted, several things needed to be configured correctly, and I needed to use the latest Socket.IO syntax:

  • CORS must be enabled on the socket.mydomain. server (Access-Control-Allow-Origin header set to *)

  • Acceptable transports had to be specified on the server (socket.mydomain.):

    const app = express();
    const server = http.createServer(app);
    const io = require('socket.io')(server, {
      transports: ['websocket', 'xhr-polling']
    });
    
  • The remote server and acceptable transports had to be specified on the client (app.mydomain.):

    const socket = io('socket.mydomain.', {
      transports: ['websocket', 'xhr-polling']
    });
    

本文标签: javascriptUsing SocketIO server and client on different subdomainsStack Overflow