繁体   English   中英

节点js中的socket.io客户端

[英]socket.io client side in node js

难道我做错了什么? 当我运行node client.js 我收到此错误消息:

/web/ws/node_modules/socket.io/node_modules/engine.io/lib/server.js:358
  var listeners = server.listeners('request').slice(0);
                         ^
TypeError: Object http://localhost:8080 has no method 'listeners'
    at Server.attach (/web/ws/node_modules/socket.io/node_modules/engine.io/lib/server.js:358:26)
    at Function.attach (/web/ws/node_modules/socket.io/node_modules/engine.io/lib/engine.io.js:124:10)
    at Server.listen.Server.attach (/web/ws/node_modules/socket.io/lib/index.js:226:21)
    at new Server (/web/ws/node_modules/socket.io/lib/index.js:51:17)
    at Server (/web/ws/node_modules/socket.io/lib/index.js:39:41)
    at Object.<anonymous> (/web/ws/client.js:2:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

这是我的代码:

var io = require('socket.io');
var socket = io('ws://localhost:8080');
  socket.on('connect', function () {
    socket.send('hi');
    socket.on('message', function (msg) {
      // my msg
    });
  });

请提出一些建议。 我在哪里做错了?

这里的问题是您试图使用用于配置服务器的socket.io软件包。

相反,您需要使用socket.io-client软件包才能充当客户端。

您可以通过以下提供的链接找到用法示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM