【问题标题】:socket issues with angularJS and NodeJsangularJS 和 NodeJs 的套接字问题
【发布时间】:2018-11-25 09:58:58
【问题描述】:

我需要帮助:我正在尝试向服务器端发送 emit socket 事件。你能帮我找出错误吗? 请考虑环境:客户端文件和服务器文件在同一台服务器上运行。

AngularJS:

<!-- Socket.io //bower_components/angular-socket-io/mock/socket-io.js-->                
var socket = io.connect("http://127.0.0.1:3000");      
socket.emit("test","{'xxx': 'yyy'}");

NodeJs:

//"socket.io": "^1.7.4",
var io = require('socket.io').listen(3000);
io.sockets.on('connection', function (socket) {    
      socket.on('test', function (data) {
            console.log("receiving data: " , data);   
      });
)}
running with : nodemon push.js 3000

提前致谢。 安德里亚

【问题讨论】:

    标签: angularjs node.js socket.io angular-socket-io


    【解决方案1】:

    您的代码中有错字:socket 而不是 sockets。此外,您没有按照 documentation 中的说明初始化 io

    var server = require('http').createServer();
    var io = require('socket.io')(server);
    io.on('connection', function (socket) {    
          socket.on('test', function (data) {
                console.log("receiving data: " , data);   
          });
    )}
    
    server.listen(3000)
    

    【讨论】:

    • 谢谢,但它也不起作用 io.socket.on('connection', function (socket) { ^ TypeError: Cannot read property 'on' of undefined
    • @Gelso77,我编辑了答案。它是 io.on 而不是 io.socket.on
    猜你喜欢
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2016-08-26
    • 1970-01-01
    • 2018-03-20
    • 2014-05-04
    • 2019-09-21
    相关资源
    最近更新 更多