我一直在调查这个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/