NodeJS-socket.io getting “Access is Denied” Exception

纵饮孤独 提交于 2020-01-03 05:52:47

问题


I have a node (0.6.11)/socket.io(0.9.0) application that runs well in FF but IE8 throws JS exceptions:

Access is denied

in socket.io.js (line 2561):

req.open(method || 'GET', this.prepareUrl() + query, true);

a few lines before that, req is defined as

req = io.util.request(this.socket.isXDomain())

This suggests it is a cross domain issue, but I'm doing it locally all the way. Plus FF has no issues.

What could be the cause?

.

Here's the source code:

SERVER:

var app = require('express').createServer()
  , io = require('socket.io').listen(app);

app.listen(1337);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

CLIENT:

<html>
    <head>
    </head>
    <body>        
        <div id='contents'> 
        </div>
        <script src="http://localhost:1337/socket.io/socket.io.js"></script>
        <script>
          var socket = io.connect('http://127.0.0.1:1337');
          socket.on('news', function (data) {
            console.log(data);
            socket.emit('my other event', { my: 'data' });
          });
        </script>
    </body>
</html>

I read about setting the secure flag to true and that makes the exception go away but then it siliently fails and does nothing. In FF and IE.


回答1:


sorry nobody bothers to answer you, but the issue is that you are doing CORS, (cross-origin-resource-sharing), meaning your socket.io server is running on a different port from your webserver (i assume port 80, but you don't explicitly say it)

the IE8 and IE9 have very limited CORS support. i don't know a solution for IE8 support, but that's your problem. more details can be found here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx



来源:https://stackoverflow.com/questions/9594913/nodejs-socket-io-getting-access-is-denied-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!