套接字。io MVC节点。js发射到另一个房间

时间:2022-04-20 19:43:33

I have been looking into this Socket.io MVC node.js but I'm currently struggling.

我一直在看这个插座。io MVC节点。但是我现在很挣扎。

It says in the documentation:

文件中说:

Socket.io API's

套接字。io API

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只是套接字的包装机制。io,所有相同的API都可以使用套接字。MVC模块。请通过访问套接字查看所有可用API的列表。io Github页面,或http://socket。io(视您的版本而定)

My problem is that I cant find a way to emit socket MVC to a room.

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

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

Should work, but it doesn't. Any idea how I can accomplish this?

应该行得通,但行不通。你知道我该怎么做吗?

EDIT: userid is same as assigned here: import * as io from 'socket.io-client';

编辑:userid与这里分配的相同:从'socket.io-client'导入*作为io;

download

下载

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?");
          });

edit , in server.js

编辑在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???????");
    });

});

sockets.js

sockets.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!!!');
    });
};

2 个解决方案

#1


1  

Replace socketMVC.to by socketMVC.io.to.

取代socketMVC。通过socketMVC.io.to。

#2


1  

I've never used Socket.IO MVC, but after reading you post, I guess I will never use it at all. It doesn't seem like MVC, and it's purely not a wrapper at all. I've written a MVC framework (both HTTP and socket), I think it only counts as MVC when you put your code in a controller, not in the the io.on("connection") itself.

我从来没有使用套接字。IO MVC,但在阅读后,我想我将永远不会使用它。它看起来不像MVC,而且它根本不是一个包装器。我已经编写了一个MVC框架(HTTP和套接字),我认为当您将代码放在控制器中时,它才算MVC,而不是io.on(“connection”)本身。

#1


1  

Replace socketMVC.to by socketMVC.io.to.

取代socketMVC。通过socketMVC.io.to。

#2


1  

I've never used Socket.IO MVC, but after reading you post, I guess I will never use it at all. It doesn't seem like MVC, and it's purely not a wrapper at all. I've written a MVC framework (both HTTP and socket), I think it only counts as MVC when you put your code in a controller, not in the the io.on("connection") itself.

我从来没有使用套接字。IO MVC,但在阅读后,我想我将永远不会使用它。它看起来不像MVC,而且它根本不是一个包装器。我已经编写了一个MVC框架(HTTP和套接字),我认为当您将代码放在控制器中时,它才算MVC,而不是io.on(“connection”)本身。