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

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

问题描述

我想实现一个聊天系统。

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. 首先进入队列

  1. First be placed in queue

等待轮到

发出发布请求

等待大约5秒钟,等待服务器响应

Wait for around 5 secs for the response from server

如果响应在时间范围内到达,并且状态为OK,消息已发送,否则消息发送失败。

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!

推荐答案

听起来您正在描述-一系列

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

https://www.dartlang.org/guides/language/language-tour#handling-streams
https://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);

在该流上收听并上传消息:

Listen on that stream and upload the messages:

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

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

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