2015-02-01 23 views
10

socket.io v1.3.2 kullanılırken bir taşıma hatası alıyorum. Sadece socket.io için bir test olması gerekiyordu, bu yüzden kendimi bununla tanıştırabilirim.Socket.Io ile taşıma yoklama hatası

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' }); 
}); 

Ve bir dosya index.html:

I (socket.io docs doğrudan alınan) bir dosyayı app.js var

<!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> 

konsolunda, aşağıdaki hatayı alıyorum:

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 

Bu hatayı düzeltmeyi bilen var mı? this discussion'u buldum, ancak sorunu çözemedim.

cevap

21

Kodunuz ile socket.io sitesinin sitesindeki örnek arasındaki fark, socket.io örnek kodunun port 80'de dinlenmesi ve webSocket'ı bağlantı noktası 80'e bağlanmasıdır. 3000 numaralı bağlantı noktasını dinliyorsunuz, ancak bağlanmaya çalışıyorsunuz Bağlantı noktası 80, dolayısıyla bağlantı çalışmıyor. io.connect('http://localhost'); bağlantı noktası numarası belirtmez, bu nedenle bağlantı noktası 80 olan varsayılan http bağlantı noktasını kullanır. Ancak, websocket sunucunuz bağlantı noktası 80'i dinlemez, böylece bağlantı başarısız olur.

Bunu düzeltmek için en basit yolu bundan istemci kod satırını değiştirmektir:

var socket = io.connect('http://localhost'); 

Buna:

URL'yi dışarıda bırakın
var socket = io.connect(); 

, olacak, Varsayılan olarak, geçerli web sayfasından alan adını ve bağlantı noktasını kullanın (istediğiniz gibi).

1

varsayılan olarak, localhost bağlantı noktası 80'dir. Bağlantı noktasını bildirmeden istemcinin soketini localhost'a bağladınız. bu yüzden varsayılan olarak port 80'i verdiniz ve düğüm sunucunuz port 3000'i dinliyor. İstemcinizin sunucunuza bağlanabilmesi için, sunucunun hangi portu dinlediğini bildirmelisiniz. örnek: var socket = io.connect ('http://localhost:3000');