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?'
- 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');
whereio
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
1 Answer
Reset to default 9To 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
版权声明:本文标题:javascript - Using Socket.IO server and client on different subdomains - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741631901a2389414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论