我觉得我已经很接近要开始工作了,但是我错过了最后一步。看来我完全在遵循文档,但是有人可以帮助我了解我所缺少的内容。这是我的@angular服务:
declare var require: any
const feathers = require('feathers/client');
const socketio = require('feathers-socketio/client');
const io = require('socket.io-client');
import { Injectable } from '@angular/core';
import { UserService } from './user.service';
@Injectable()
export class FeathersService {
socket = io.connect('http://localhost:3000/');
app = feathers().configure(socketio(this.socket));
status = this.app.service('realtime/user_status');
constructor(
public user: UserService,
) {
this.status.on('updated', ({status}) => {
console.log('your status was updated', status);
this.user.me.status = status;
});
}
updateStatus(status) {
const id = this.user.me._id;
this.status.update(id, {status});
}
}
然后这是我的路由器代码(子路由:/ realtime)
const feathers = require('feathers');
const realtime = feathers();
const socketio = require('feathers-socketio');
realtime.configure(socketio({ wsEngine: 'uws' }, (io) => {
io.on('connection', (socket) => {
socket.emit('news', { text: 'A client connected!' });
});
// Registering Socket.io middleware
io.use((socket, next) => {
// Exposing a request property to services and hooks
socket.feathers.referrer = socket.request.referrer;
next();
});
}));
const UserService = require('../controllers/user_service');
realtime.use('/user_status', new UserService());
module.exports = realtime;
最后是我的服务
const User = require('../models/user');
class UserService {
update(id, userProps, params, next) {
const userID = id;
User.findByIdAndUpdate({ _id: userID }, userProps, { 'new': true })
.then(user => {
console.log('userProps', userProps);
console.log('user >>>', user);
return Promise.resolve(user);
})
.catch(next);
}
}
module.exports = UserService;
最佳答案
您没有以服务方式退还Promise
const User = require('../models/user');
class UserService {
update(id, userProps, params) {
const userID = id;
return User.findByIdAndUpdate({ _id: userID }, userProps, { 'new': true })
.then(user => {
console.log('userProps', userProps);
console.log('user >>>', user);
return user;
});
}
}
module.exports = UserService;
应该做。
关于javascript - 带有Socket.io的FeathersJS监听事件是静默的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42593116/