socket.io MEAN堆栈(为什么我无法连接到事件?)

时间:2017-11-07 09:12:37

标签: node.js angular express socket.io

服务器端:

app.js

let express = require('express'),
    app = express(),
    http = require('http').Server(app);
    io = require('socket.io')(http);
http.listen(port, function () {
  ...
});
io.on('connection', (socket) =>{
    socket.emit('some_event',{user : 'some_name'})
});

routes / index.js

let router = express.Router();
.....
router.get('/api/telegram/activation-code',user.isLoggedIn(),telegramActivation);

客户端:

any_angular.component.ts

    import * as io from 'socket.io-client';
    ...
    ngOnInit(){
      let socket = io('/api/telegram/activation-code');
          socket.on('connection', (socket) =>{
            socket.on('some_event',(data) =>{
               console.log(data)
             })
          });
    }

我尝试从服务器端获取一些数据。但是当我移动到 / api / telegram / activation-code 路由时,我没有收到数据。谁可以向我解释我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

可能是您的背面和正面位于不同的命名空间中。在节点代码中尝试这个

io.of('/api/telegram/activation-code').on('connection...

https://socket.io/docs/rooms-and-namespaces/#custom-namespaces