【问题标题】:socket with node js example带有节点 js 示例的套接字
【发布时间】:2011-03-30 06:41:39
【问题描述】:

我有问题。

客户端代码

 <html>
    <body onload="fun()">
    <script src="C:\cygwin\usr\local\lib\node\.npm\socket.io\0.6.16\package\support\socket.io-client\socket.io.js"></script> 
    <script> 

    function fun()
    {
     alert("hello")   
     var socket = new io.Socket('localhost',{'port':8090});

    socket.on('connect', function(){ 
      socket.send('hi!'); 
    }) 

     socket.on('connect', function(){ console.log('connected to server');socket.send('hi there, this is a test message'); })


    socket.on('message', function(data){ 
      alert(data);
    })
    socket.on('disconnect', function(){}) 
    }
    </script> 
    </body>
    </html>

服务器端代码:

var http = require('http'),  
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') 

server = http.createServer(function(req, res){ 
 // your normal server code 
 res.writeHead(200, {'Content-Type': 'text/html'}); 
 res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
  // new client is here! 
  client.on('message', function(){ console.log('message arrive'); }) 

  client.on('disconnect', function(){ console.log('connection closed');})

});

从 socket.io 找到这个例子。 当我运行服务器时,它给了我 Socket io 已准备就绪。接受连接 当我运行浏览器时它没有显示任何内容,并且在 firefox firebug 控制台上请帮助我解决这个问题。

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:

    您永远不会在客户端调用 socket.connect(),因此套接字永远不会尝试连接到服务器。检查以下代码:

    客户端->

    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script src="socket.io.js"></script> 
    <script> 
    
    function fun()
    {
        var socket = new io.Socket('localhost',{'port':8090});
    
        socket.connect();
    
        socket.on('connect', function(){
            console.log('connected');
            socket.send('hi!'); 
        });
    
    
        socket.on('message', function(data){ 
            console.log('message recived: ' + data);
        });
    
        socket.on('disconnect', function(){
            console.log('disconected');
        });
    }
    
    $(document).ready(function() {
        fun();
    });
    </script> 
    </head>
    <body>
    </body>
    </html>
    

    服务器端->

    var http = require('http'),  
        io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') 
    
    server = http.createServer(function(req, res){ 
        // your normal server code 
        res.writeHead(200, {'Content-Type': 'text/html'}); 
        res.end('<h1>Hello world</h1>'); 
    });
    server.listen(8090);
    
    // socket.io 
    var socket = io.listen(server); 
    socket.on('connection', function(client){ 
        // new client is here! 
        client.on('message', function(){ 
            console.log('message arrive');
            client.send('some message');
        });
    
        client.on('disconnect', function(){
            console.log('connection closed');
        });
    
    });
    

    【讨论】:

    • 感谢您的回复,但我面临同样的问题...该代码对您有用???我运行这样的代码:.....on cygwin runserver node server.js 然后运行 ​​html 文件这是正确的运行方式
    • 一个问题是客户端socket.io的正确路径,请检查firebug是否已加载脚本并调整/更改/move_the_file,直到您将其加载到浏览器&lt;script src="socket.io.js"&gt;&lt;/script&gt; 中。是的,它在这里像魅力一样工作,我什至更进一步,并在客户端和服务器上使用 setInterval 向后发送消息....
    • 感谢 poelinca 的工作....感谢您的帮助...您能告诉我有关节点 js 和套接字的任何教程 ....
    • 不客气,不,我不知道任何关于节点中套接字的教程,但谷歌提供了一些很好的例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    相关资源
    最近更新 更多