Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – NodeJS – Socket.IO设置:提供静态内容无握手(Rackspace Cloud Server上的Ubuntu)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Rackspace上安装了带有Socket.IO的Ubuntu node.js

当我尝试使用简单的服务器应用程序并尝试使用客户端请求时,我只获得了“提供静态内容”而不是手抖动.在调试的浏览器中,我可以看到“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>

解决方法

在您的客户端代码中试试这个
var socket = io.connect('http://SERVERDOMAIN:8866');

此问题主要与错误的网址相关联.

大佬总结

以上是大佬教程为你收集整理的node.js – NodeJS – Socket.IO设置:提供静态内容无握手(Rackspace Cloud Server上的Ubuntu)全部内容,希望文章能够帮你解决node.js – NodeJS – Socket.IO设置:提供静态内容无握手(Rackspace Cloud Server上的Ubuntu)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。