![](/img/trans.png)
[英]video chat application on node.js with webRTC( socket.io) works fine on localhost but gives error on network
[英]socket.io and node.js gives error on localhost
我有一個在192.168.1.22上工作的開發服務器,我想在上面運行node.js,但它給出了一些錯誤。 我只想測試一些情況,甚至無法運行它。 在我運行它之后,我將獲取我的onclick按鈕,並從node.js發送該帖子到我的php文件。 並將數據從php返回到node.js,將返回的數據返回到網站。
我的js:
var http = require('http'),
io = require('socket.io'),
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Sample server created with NodeJS.</h1>');
res.end();
});
server.listen(8001);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
client.send('Hello client');
client.on('message', function(){
client.send((new Date()).getTime());
})
});
我的html:
<script type="text/javascript" src="http://cdn.socket.io/stable/socket.io.js"></script>
<script type="text/javascript">
var socket = new io.Socket(null, {port: 8001});
socket.connect();
socket.on('message', function(message){
document.getElementById('divTime').innerHTML = message;
});
function GetServerTime() {
socket.send('');
}
</script>
錯誤:
WebSocket connection to 'ws://192.168.1.22:8001/socket.io/websocket' failed: Connection closed before receiving a handshake response socket.io.js:378
XMLHttpRequest cannot load http://192.168.1.22:8001/socket.io/xhr-polling//1401170092339. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.22:8000' is therefore not allowed access. (index):1
WebSocket connection to 'ws://192.168.1.22:8001/socket.io/websocket' failed: Connection closed before receiving a handshake response socket.io.js:378
XMLHttpRequest cannot load http://192.168.1.22:8001/socket.io/xhr-polling//1401170102339. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.22:8000' is therefore not allowed access. (index):1
WebSocket connection to 'ws://192.168.1.22:8001/socket.io/websocket' failed: Connection closed before receiving a handshake response
從該錯誤來看,您似乎正在嘗試從另一個端口連接到該服務器:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.22:8000' is therefore not allowed access. (index):1
您上面粘貼的服務器代碼(標記為“ my js”)看起來不錯。 一旦安裝了socket.io
程序包,就可以在本地運行相同的代碼。 因此,問題可能不在於啟動服務器代碼,而是在於啟動后連接到服務器代碼? 如果是這樣,您上面的錯誤就指向一個同源策略問題。 如果您從節點應用程序中托管HTML代碼,並且該代碼運行在同一端口上,則不會出現此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.