2012-09-29 12 views
5

Ho installato sul Rackspace con Ubuntu Node.JS con Socket.IONodeJS - Impostazione Socket.IO: serviti contenuti statici senza stretta di mano (Ubuntu su Rackspace Cloud Server)

Quando ho provato una semplice applicazione server e cercare di usa la richiesta del cliente Ho ricevuto solo "contenuto statico" invece di scuotere la mano. Nel browser di debug posso vedere "Ciao S ..." e sul lato server:

# node socket-server.js 
    info - socket.io started 
    debug - served static content /socket.io.js 
    debug - served static content /socket.io.js 

non sono sicuro da dove cominciare sguardo per un problema (lo stesso script funziona nello sviluppo locale)

Perché node.js pubblica solo contenuto statico e non ha l'handshake?

Iptables permettono 8866 porta: # iptables -L

Chain INPUT (policy ACCEPT) 
target  prot opt source    destination   
ACCEPT  all -- anywhere    anywhere    
ACCEPT  all -- anywhere    anywhere   state RELATED,ESTABLISHED 
ACCEPT  tcp -- anywhere    anywhere   tcp dpt:ssh 
ACCEPT  tcp -- anywhere    anywhere   tcp dpt:www 
ACCEPT  tcp -- anywhere    anywhere   tcp dpt:8866 
DROP  all -- anywhere    anywhere    

Chain FORWARD (policy ACCEPT) 
target  prot opt source    destination   

Chain OUTPUT (policy ACCEPT) 
target  prot opt source    destination   
ACCEPT  tcp -- anywhere    anywhere   tcp dpt:8866 

Ecco un semplice server app 'presa-server.js':

// Require HTTP module (to start server) and Socket.IO 
var http = require('http'), io = require('socket.io'); 

// Start the server at port 8866 
var server = http.createServer(function(req, res){ 

     // Send HTML headers and message 
     res.writeHead(200,{ 'Content-Type': 'text/html' }); 
     res.end('<h1>Hello Socket Lover!</h1>'); 
}); 
server.listen(8866); 

// Create a Socket.IO instance, passing it our server 
var socket = io.listen(server); 

// Add a connect listener 
socket.on('connection', function(client){ 

     // Create periodical which ends a message to the client every 5 seconds 
     var interval = setInterval(function() { 
       client.send('This is a message from the server! ' + new Date().getTime()); 
     },5000); 

     // Success! Now listen to messages to be received 
     client.on('message',function(event){ 
       console.log('Received message from client!',event); 
     }); 
     client.on('disconnect',function(){ 
       clearInterval(interval); 
       console.log('Server has disconnected'); 
     }); 

}); 

Ecco un semplice client (SERVERDOMAIN è sostituito con vero dominio):

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://SERVERDOMAIN:8866/socket.io/socket.io.js"></script> 
<script> 

    // Create SocketIO instance 
    var socket = io.connect('SERVERDOMAIN:8866'); 
    // Add a connect listener 
    socket.on('connect',function() { 
     log('<span style="color:green;">Client has connected to the server!</span>'); 
    }); 
    // Add a connect listener 
    socket.on('message',function(data) { 
     log('Received a message from the server: ' + data); 
    }); 
    // Add a disconnect listener 
    socket.on('disconnect',function() { 
     log('<span style="color:red;">The client has disconnected!</span>'); 
    }); 

    // Sends a message to the server via sockets 
    function sendMessageToServer(message) { 
     socket.send(message); 
     log('<span style="color:#888">Sending "' + message + '" to the server!</span>'); 
    } 

    // Outputs to console and list 
    function log(message) { 
     var li = document.createElement('li'); 
     li.innerHTML = message; 
     document.getElementById('message-list').appendChild(li); 
    } 

</script> 
</head> 
<body> 

<p>Messages will appear below (and in the console).</p><br /> 
<ul id="message-list"></ul> 
<ul style="margin:20px 0 0 20px;"> 
    <li>Type <code>socket.disconnect()</code> to disconnect</li> 
    <li>Type <code>socket.connect()</code> to reconnect</li> 
    <li>Type <code>sendMessageToServer('Your Message')</code> to send a message to the server</li> 
</ul> 

</body> 
</html> 
+0

se c'è un errore nella pagina l'output si interrompe dopo "contenuto statico debug -server /socket.io.js" - sei sicuro che il tuo codice clientide funzioni? – supernova

risposta

3

Nel codice cliente provare questo

0.123.
var socket = io.connect('http://SERVERDOMAIN:8866'); 

Questo problema è principalmente associato con l'URL corretto.

+0

Grazie per il consiglio ma non è stato d'aiuto. – Trebla

+0

Risposta eccellente @beNerd !! Questo ha funzionato per me. Grazie :) –

0

Mi sono ritrovato con un'installazione server completamente nuova e ora tutto funziona correttamente.

Ho usato Ubuntu 12.04 LTS Precise Pangolin() ed installare node.js con socket.io

Per il cliente che sto usando copia locale di socket.io.js con file Flash.

;-)

1

stavo testando la mia domanda socket.io con un paio di Android (telefono e tablet) dispositivi insieme con i browser desktop su una connessione wi-fi . Sulla base di risposta di beNerd sopra ho cambiato

io.connect('http://localhost:3000'); 

a

io.connect('http://192.168.5.3:3000'); // ip address of machine where the node.js app is running. 

e ha funzionato.

Spero che la risposta sia utile per gli utenti che testano le loro applicazioni NodeJS e Socket.IO con una connessione Wifi.

Problemi correlati