Tôi gặp phải lỗi vận chuyển khi sử dụng socket.io v1.3.2. Nó chỉ có nghĩa là một thử nghiệm cho socket.io vì vậy tôi có thể làm quen với nó.Lỗi bỏ phiếu khi vận chuyển với Socket.Io
Tôi có một tập tin app.js
(lấy trực tiếp từ tài liệu socket.io):
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(3000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
Và một file index.html
:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<h1>Socket Test</h1>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
});
</script>
</body>
</html>
Trong giao diện điều khiển, tôi nhận được lỗi sau:
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1422782880286-40
(index):1 XMLHttpRequest cannot load http://localhost/socket.io/?EIO=3&transport=polling&t=1422782880286-40. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.
socket.io.js:3715 engine.io-client:socket socket error {"type":"TransportError","description":0} +37ms
socket.io.js:1402 socket.io-client:manager connect_error +38ms
socket.io.js:1402 socket.io-client:manager reconnect attempt error +1ms
socket.io.js:1402 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
socket.io.js:3715 engine.io-client:socket socket close with reason: "transport error" +2ms
socket.io.js:3715 engine.io-client:polling transport not open - deferring close +1ms
socket.io.js:1402 socket.io-client:manager attempting reconnect +5s
socket.io.js:1402 socket.io-client:manager readyState closed +0ms
socket.io.js:1402 socket.io-client:manager opening http://localhost +0ms
socket.io.js:3715 engine.io-client:socket creating transport "polling" +5s
socket.io.js:3715 engine.io-client:polling polling +0ms
socket.io.js:3715 engine.io-client:polling-xhr xhr poll +0ms
socket.io.js:3715 engine.io-client:polling-xhr xhr open GET: http://localhost/socket.io/?EIO=3&transport=polling&t=1422782885332-41 +1ms
socket.io.js:3715 engine.io-client:polling-xhr xhr data null +0ms
socket.io.js:3715 engine.io-client:socket setting transport polling +1ms
socket.io.js:1402 socket.io-client:manager connect attempt will timeout after 20000 +3ms
Có ai biết cách sửa lỗi này không. Tôi đã tìm thấy this discussion, nhưng không thể giải quyết được vấn đề.