NodeJS - Socket.IO Setup: served static content no handshake (Ubuntu on Rackspace Cloud Server)

安稳与你 提交于 2019-12-07 05:28:20

问题


I have installed on Rackspace with Ubuntu node.js with Socket.IO

When I tried a simple server app and try to use client request I got 'served static content' only instead of hand shake. In browser in debug I can see "Hello S..." and on server side:

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

I'm not sure where to start look for a problem (same script works in local development)

Why node.js serve only static content and doesn't handshake?

Iptables allow 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 

Here is a simple server app 'socket-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');
        });

});

Here is a simple client (SERVERDOMAIN is replaced with real domain):

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

回答1:


In your client code try this

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

This problem is mainly associated with incorrect url.




回答2:


I was testing my socket.io application with a couple of Android ( phone and tablet ) devices along with desktop browsers on a wifi connection. Based on beNerd's answer above I changed my

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

to

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

and it worked.

Hope the answer is helpful to users testing their NodeJS and Socket.IO application with a Wifi connection.




回答3:


I have end up with completely new server installation and everything is working fine now.

I have used Ubuntu 12.04 LTS (Precise Pangolin) and install node.js with socket.io

For the client I'm using local copy of socket.io.js with Flash file.

;-)



来源:https://stackoverflow.com/questions/12650299/nodejs-socket-io-setup-served-static-content-no-handshake-ubuntu-on-rackspac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!