Socket.io客户端事件触发

Socket.io客户端事件触发,socket.io,Socket.io,我希望我的server.js在client.html提示时执行特定功能 两个苍蝇的部分如下: Server.js: io.sockets.on('connection', function (socket) { socket.on('key',function(data){ console.log('done'); //this statement is never executed }); }); 客户: <script> var socket = i

我希望我的server.js在client.html提示时执行特定功能

两个苍蝇的部分如下:

Server.js:

io.sockets.on('connection', function (socket) {

socket.on('key',function(data){
console.log('done'); //this statement is never executed

    });
  });
客户:

<script>
      var socket = io.connect();
     socket.on('connect', function () {
     socket.emit('key',{
     string:'string'
     });

    </script>

var socket=io.connect();
socket.on('connect',function(){
socket.emit('key'{
字符串:'string'
});

console.log('done');从未执行过..不知道为什么..请帮帮我..:)

您是否包含
socket
js文件


旧问题,但您的代码不起作用的原因是您缺少结束语:
})在客户端代码上

解决方案:

<script>
var socket = io.connect();
socket.on('connect', function () {
  socket.emit('key',{
     string:'string'
   });
});  //<--- Missing closing tag
</script>

var socket=io.connect();
socket.on('connect',function(){
socket.emit('key'{
字符串:'string'
});

}); //是的,我已经在
var socket=io.connect()中包含了js文件@ashleyjust
您需要指定url:
var socket=io.connect('http://localhost:3000');
从客户端删除
socket.on('connect')
,因为您没有发出任何名为
connect
的套接字。如果您查看文档,在('connect')上没有
:实际上该页面在客户端的('connect')上使用。此外,它被列为此处发出的事件之一:……然而,我的问题是,我根本没有在客户端接收到任何这些事件……您在浏览器控制台中是否收到任何错误?您是否阅读了任何教程?