2015-07-20 18 views
6

Recentemente ho iniziato a imparare node.js e socket.io. Ho seguito un semplice tutorial che aveva socket.io e tutto ha funzionato bene durante l'esecuzione sul mio computer. Tuttavia, ho deciso di caricare la parte client su un server per il test ed è qui che sono iniziati i problemi. Vorrei eseguire il client di chat su un host web ed eseguire il server sul mio computer o su un altro host. Fondamentalmente, ho in programma il port forwarding del server e l'esecuzione del client su una pagina web. Ho aperto la porta per port forward e sembra funzionare, tuttavia sto ricevendo l'errore sulla pagina web ogni volta.CORS Bloccato con node.js e socket.io

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://24.151.51.34:3000/socket.io/?EIO=3&transport=polling&t=1437399007343-0. (Reason: CORS request failed). 

Sono stato nei guai con il codice nella speranza di trovare una soluzione a questo problema prima di iniziare il mio progetto, ma non riesco a trovare un modo. Il codice cliente è:

<!doctype html> 
<html> 
    <head> 
    <title>Socket.IO chat</title> 
    <style> 
     * { margin: 0; padding: 0; box-sizing: border-box; } 
     body { font: 13px Helvetica, Arial; } 
     form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } 
     form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } 
     form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } 
     #messages { list-style-type: none; margin: 0; padding: 0; } 
     #messages li { padding: 5px 10px; } 
     #messages li:nth-child(odd) { background: #eee; } 
    </style> 
    </head> 
    <body> 
    <ul id="messages"></ul> 
    <form action=""> 
     <input id="m" autocomplete="off" /><button>Send</button> 
    </form> 
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script> 
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script> 
    <script> 
     var socket = io('24.151.51.34:3000'); 
     $('form').submit(function(){ 
     socket.emit('chat message', $('#m').val()); 
     $('#m').val(''); 
     return false; 
     }); 
     socket.on('chat message', function(msg){ 
     $('#messages').append($('<li>').text(msg)); 
     }); 
    </script> 
    </body> 
</html> 

Per il codice del server ho provato ad aggiungere il codice per consentire CORS ma in realtà non so cosa fare:

var app = require('express')(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 

io.set('origins', 'http://browsercombat.com:80'); 

app.use(function(req, res, next) { 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
    res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS"); 
    res.header("Access-Control-Allow-Credentials", "true"); 
    next(); 
}); 

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

app.post('/', function(req, res, next) { 
    // Handle the post for this route 
}); 

io.on('connection', function(socket){ 
    socket.on('chat message', function(msg){ 
    io.emit('chat message', msg); 
    }); 
}); 

http.listen(3000, function(){ 
    console.log('listening on *:3000'); 
}); 
+0

Che cosa è esattamente 'myip'? – Bergi

+0

Ah, ho dimenticato di menzionare che ero port forwarding che si collegava al mio IP, ho deciso di sostituirlo con "myip". – uncoded

+0

Esempi di codice CORS per Express e socket.io: http://enable-cors.org/server_expressjs.html e http://stackoverflow.com/questions/24058157/socket-io-node-js-cross-origin- request-bloccata. – jfriend00

risposta