feathersjs套接字使用事件

问题描述 投票:0回答:1

在feathersjs文档中,例如here,推荐的调用服务器的方法是发出一个事件。为什么不直接拨打应用程序?那么为什么要用:

socket.emit('find', 'messages', { status: 'read', user: 10 }, (error, data) => {
  console.log('Found all messages', data);
});

当你可以简单地做:

app.service('messages').find({ query: { status: 'read', user: 10 } }) 

这只是人们更喜欢事件符号还是有其他需要考虑的论点?

javascript sockets javascript-events feathersjs
1个回答
2
投票

您链接的文档页面解释了如何直接使用websocket - 例如,如果您连接Android应用程序或不想/不能在客户端上使用Feathers。

建议尽可能在客户端上使用Feathers,它会自动为您完成相同的操作。像这样的客户端代码:

const io = require('socket.io-client');
const feathers = require('@feathersjs/feathers');
const socketio = require('@feathersjs/socketio-client');

const socket = io('http://api.my-feathers-server.com');
const app = feathers().configure(socketio(socket));

app.service('messages').find({ query: { status: 'read', user: 10 } })
  .then(data => console.log('Found all messages', data));

完全相同的事情

const io = require('socket.io-client');
const socket = io('http://api.my-feathers-server.com');

socket.emit('find', 'messages', { status: 'read', user: 10 }, (error, data) => {
  console.log('Found all messages', data);
});

但是第一个你得到了优点(钩子,事件,承诺,身份验证)和熟悉的Feathers应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.