2015-07-20 21 views
6

Yakın zamanda node.js ve socket.io öğelerini öğrenmeye başladım. Socket.io'nun sahip olduğu basit bir öğreticiyi takip ettim ve bilgisayarım üzerinde çalışırken iyi çalıştı. Ancak, istemci bölümünü test için bir sunucuya yüklemeye karar verdim ve sorunların başladığı yer burasıydı. Sohbet istemcisini bir web ana bilgisayarında çalıştırmak ve sunucumu bilgisayarımdan veya başka bir ana bilgisayardan çalıştırmak istiyorum. Temel olarak, sunucuyu yönlendiren bağlantı noktasını ve istemcinin bir web sayfasında çalışmasını planlıyorum. Limana portu açtım ve çalışıyor gibi görünüyor, ancak her seferinde web sayfasında hatayı alıyorum. CORS node.js ve socket.io ile kilitlendi

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). 

ancak ben bir yolunu olamaz, kendi projeye başlamadan önce bu soruna bir çözüm bulma umuduyla kodu ile karıştırmasını oldum. istemci kodu:

sunucu kodu için
<!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> 

Ben CORS izin kodu eklemek denedi ama gerçekten emin ne yapacağını: Ben bu ortası CORS kullanarak problemi çözmeyi başardık

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

Tam olarak ne '' myip' nedir? – Bergi

+0

Ah, ip adresime bağlandığım bağlantı noktasından bahsetmeyi unuttum, "myip" ile değiştirmeye karar verdim. – uncoded

+0

CORS, Express ve socket.io için örnek kodlar: http://enable-cors.org/server_expressjs.html ve http://stackoverflow.com/questions/24058157/socket-io-node-js-cross-origin- istek-bloke edildi. – jfriend00

cevap

İlgili konular