2012-09-29 13 views
5

Mam zainstalowany na Rackspace z Ubuntu node.js z Socket.IONodeJS - Definicja Socket.IO: podawane zawartości statycznej bez uzgadniania (Ubuntu na Rackspace Chmura Server)

Kiedy próbowałem prostą aplikację serwera i spróbować użyj żądania klienta Dostałem "obsługiwaną statyczną zawartość" tylko zamiast drżenia ręki. W przeglądarce w debug widzę „Witaj S ...” i po stronie serwera:

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

nie jestem pewien, gdzie zacząć szukać problemu (ten sam skrypt działa w rozwoju lokalnym)

Dlaczego plik node.js wyświetla tylko treść statyczną i nie obsługuje uzgadniania?

iptables pozwalają 8866 Port: # 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 

Oto prosta aplikacja serwera Gniazdo 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'); 
     }); 

}); 

Oto prosty klient (SERVERDOMAIN otrzymuje z prawdziwym domeny):

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

Jeśli na stronie występuje błąd, dane wyjściowe zatrzymują się po "debug - served static content /socket.io.js" - czy na pewno działa kod klienta? – supernova

Odpowiedz

3

w kodzie klienta spróbować tej

Ten problem jest związany głównie z niepoprawnym adresem URL.

+0

Dziękuję za poradę, ale to nie pomogło. – Trebla

+0

Doskonała odpowiedź @beNerd !! To działało dla mnie. Dzięki :) –

0

Skończyłem z całkowicie nową instalacją serwera i wszystko działa dobrze.

użyłem Ubuntu 12.04 LTS Precise Pangolin) (i zainstalować node.js z socket.io

dla klienta używam lokalną kopię socket.io.js z pliku Flash.

;-)

1

ja testował mój wniosek socket.io z kilkoma Android (telefon i tablet) urządzeń wraz z przeglądarek desktopowych na połączenia wifi. Na podstawie odpowiedź beNerd w powyższym zmieniłem

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

do

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

i to działało.

Mam nadzieję, że odpowiedź jest przydatna dla użytkowników testujących ich aplikacje NodeJS i Socket.IO za pomocą połączenia Wi-Fi.