的NodeJS - Socket.IO设置:提供静态内容没有握手(Ubuntu的Rackspac

2019-09-23 04:37发布

我已经安装在Rackspace公司与Ubuntu与Socket.IO的Node.js

当我试图一个简单的服务器应用程序,并尝试使用客户端请求我“服务的静态内容”改为只手抖动。 在调试,我可以看到浏览器的“Hello小号......”,并在服务器端:

# 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 

下面是一个简单的服务器应用程序“插座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>

Answer 1:

在您的客户端代码试试这个

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

这个问题主要是不正确的URL关联。



Answer 2:

我用一对夫妇的Android(手机和平板电脑)与WiFi连接在桌面浏览器沿着设备测试我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.

和它的工作。

希望答案是对用户有帮助的测试和的NodeJS应用Socket.IO与WiFi连接。



Answer 3:

我有一个全新的服务器安装结束了,现在一切工作正常。

我已经使用的Ubuntu 12.04 LTS(精确穿山甲),并安装有socket.io的node.js

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

;-)



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