簡體   English   中英

Socket.io MVC node.js 發射到房間

[英]Socket.io MVC node.js emit to room

我一直在研究這個Socket.io MVC node.js但我目前正在掙扎。

它在文檔中說:

Socket.io API

由於 Socket.MVC 只是 Socket.io 的包裝機制,所有相同的 API 都可以使用 Socket.MVC 模塊使用。 請訪問 Socket.io Github 頁面或http://socket.io (取決於您的版本)查看所有可用 API 的列表

我的問題是我找不到將套接字 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM