javascript - Socket.io MVC node.js 发射到房间

标签 javascript node.js socket.io

我一直在调查这个Socket.io MVC node.js但我目前正在挣扎。

文档中说:

Socket.io API's

Since Socket.MVC is just a wrapping mechanism for Socket.io, all of the same API's can be used using the Socket.MVC module. Please see a list of all of the API's available by visiting the Socket.io Github page, or http://socket.io (depending on your version)

我的问题是我找不到将套接字 MVC 发送到房间的方法。

socketMVC.to(userid).emit('message', {message:2});

应该可以,但没有。知道如何实现吗?

编辑: userid 与此处分配的相同: import * as io from 'socket.io-client';

下载

oninit:
      this.socket = io(this.socketurl);


      var privateRoom = socket.request.session.passport.user;

        socket.join(privateRoom);


      this.socket.on('message', (data) => {
            //  this.messagesCounter = this.messagesCounter + 1;
              alert("OMG?");
          });

在 server.js 中编辑

io.on('connection',function(socket) {
    console.log('user connected');
    OnlineUsers.push(socket);
  //  socket.join('');
    console.log(socket.request.session.passport);
    console.log("user is connceted");




    socketMVC.init(io, socket, {
        debug: true,
        filePath: ['./src/routes/sockets.js']
    });



socket.on('disconnect', function(){
    console.log('user disconnected');
});

socket.on('add-message',function (message) {
    io.emit('message', {type:'new-message', text: message});
});

    socket.on('myevent', function(someData) {
        console.log("MYEVENT WORKS???????");
    });

});

套接字.js

var path = require('path');


module.exports = function (socket) {
   var privateRoom = socket.request.session.passport.user;

    socket.join(privateRoom);


    socket.on('testing', function() {
        console.log('GOT SOME SORT OF RESPONSE!!!');
    });
};

最佳答案

socketMVC.to替换为socketMVC.io.to

关于javascript - Socket.io MVC node.js 发射到房间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453683/

相关文章:

javascript - Socket.io 不适用于 android chrome 51

apache - 通过 Apache 服务页面访问 socket.io 服务器

node.js - 如何删除 socket.io 中的房间

javascript - 逐字符消失的占位符

node.js - NoSql 与远程服务器离线同步

javascript - 如何在单元测试中调用 $(document).ready(function() {})

node.js - RUN npm install 上的 Dockerfile 构建错误

Node.js HTTP 请求 : How to detect response body encoding?

javascript - 尝试让验证码与我的验证一起工作

javascript - 隐藏选择选项跨浏览器