飞镖中的未来队列 [英] Queue of Future in dart

查看:23
本文介绍了飞镖中的未来队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个聊天系统.

I want to implement a chat system.

我被困在用户非常快速地发送多个消息的地步.尽管所有消息都到达服务器,但以任何顺序.

I am stuck at the point where user sends multiple messgaes really fast. Although all the messages are reached to the server but in any order.

所以我想实现一个队列,每条消息都应该

So I thought of implementing a queue where each message shall

  1. 先入队

等待轮到它

轮到发帖请求

等待服务器响应大约 5 秒

Wait for around 5 secs for the response from server

如果响应在时间范围内到达并且状态正常,则消息发送,否则消息发送失败.

If the response arrives within time frame and the status is OK, message sent else message sending failed.

在第 5 点的任何情况下,消息都应出队,并给予下一条消息机会.

In any case of point 5, the message shall be dequeued and next message shall be given chance.

现在,主要问题是,每个聊天头或我们正在交谈的用户可能有多个队列.我将如何实施?我对飞镖和颤振真的很陌生.请帮忙.谢谢!

Now, the major problem is, there could be multiple queues for each chat head or the user we are talking to. How will I implement this? I am really new to dart and flutter. Please help. Thanks!

推荐答案

听起来你在描述一个 Stream - 一系列有序的异步事件.

It sounds like you are describing a Stream - a series of asynchronous events which are ordered.

https://www.dartlang.org/guides/language/语言之旅#handling-streamshttps://www.dartlang.org/guides/libraries/library-tour#stream

创建一个StreamController,并在消息进入时向其中添加消息:

Create a StreamController, and add messages to it as they come in:

var controller = StreamController<String>();
// whenever you have a message
controller.add(message);

收听该流并上传消息:

await for(var messsage in controller.messages) {
  await uploadMessage(message);
}

这篇关于飞镖中的未来队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆