【问题标题】:NodeJS - Socket.IO Setup: served static content no handshake (Ubuntu on Rackspace Cloud Server)NodeJS - Socket.IO 设置:提供静态内容,无需握手(Rackspace 云服务器上的 Ubuntu)
【发布时间】:2012-09-29 05:47:45
【问题描述】:

我已经在 Rackspace 上安装了 Ubuntu node.js 和 Socket.IO

当我尝试一个简单的服务器应用程序并尝试使用客户端请求时,我只得到了“提供的静态内容”而不是握手。在调试的浏览器中,我可以看到“Hello S...”,在服务器端:

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

我不确定从哪里开始寻找问题(相同的脚本适用于本地开发)

为什么 node.js 只提供静态内容而不提供握手?

iptables 允许 8866 端口:# 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 

这是一个简单的服务器应用程序“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');
        });

});

这里是一个简单的客户端(SERVERDOMAIN替换为真实域):

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

【问题讨论】:

  • 如果您的页面出现错误,输出会在“调试 - 提供静态内容 /socket.io.js”后停止 - 您确定您的客户端代码正常工作吗?

标签: node.js socket.io rackspace-cloud


【解决方案1】:

在你的客户端代码中试试这个

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

这个问题主要是和不正确的url有关。

【讨论】:

  • 感谢您的建议,但没有帮助。
【解决方案2】:

我在 wifi 连接 上使用几个 Android(手机和平板电脑)设备以及桌面浏览器测试我的 socket.io 应用程序。根据 beNerd 的上述回答,我改变了我的

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

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

它成功了。

希望这个答案有助于用户通过 Wifi 连接测试他们的 NodeJS 和 Socket.IO 应用程序。

【讨论】:

    【解决方案3】:

    我已经安装了全新的服务器,现在一切正常。

    我使用过 Ubuntu 12.04 LTS (Precise Pangolin) 并使用 socket.io 安装 node.js

    对于客户端,我使用带有 Flash 文件的 socket.io.js 的本地副本。

    ;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2014-04-22
      • 2020-10-15
      • 2020-02-08
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多