我不知道下一步该做什么。看起来我误解了一些东西,或者我可能没有学到一些关键的话题。
use std::sync::Arc;
use reqwest::{Error, Response}; // 0.11.4
use tokio::sync::mpsc::{self, Receiver, Sender}; // 1.9.0
pub struct Task {
pub id: u32,
pub url: String,
}
pub enum Message {
Failure(Task, Error),
Success(Task, Response),
}
struct State {
client: reqwest::Client,
res_tx: Sender<Message>,
res_rx: Receiver<Message>,
}
pub struct Proxy {
state: Arc<State>,
max_rps: u16,
max_pending: u16,
id: u32,
parent_tx: Sender<String>,
}
async fn send_msg<T>(tx: &Sender<T>, msg: T) {
match tx.send(msg).await {
Err(error) => {
eprintln!("{}", error)
}
_ => (),
};
} …Run Code Online (Sandbox Code Playgroud) 我遇到了这个问题中描述的问题:如何在另一个 Tokio 运行时中创建 Tokio 运行时,而不会收到错误“无法从运行时内启动运行时”?。
一些好的 Rust crate 没有异步执行器。我决定将所有此类库调用放在一个能够容忍此类操作的线程中。另一个线程应该能够使用 发送非闪烁消息tokio::channel。
我编写了一个演示站来测试实施选项。在每个运行时内部进行调用tokio::spawn是为了了解 tokio 运行时和处理程序中的更多细节 - 这是问题的一部分。
问题。
如果我还有什么误解,请纠正我。
有两个 tokio 运行时。每个都在自己的线程中启动。tokio::spawn在第一个运行时内部调用会first_runtime()生成任务。tokio::spawn内部调用会second_runtime()在第二个运行时生成任务。tokio::channel这两个任务之间有一个。如果通道tx.send(...).await缓冲区未满,即使接收线程被调用阻塞,调用也不会阻塞发送线程thread::sleep()。
我一切都做对了吗?这段代码的输出告诉我我是对的,但我需要确认我的推理。
use std::thread;
use std::time::Duration;
use tokio::sync::mpsc::{Sender, Receiver, channel}; // 1.12.0
#[tokio::main(worker_threads = 1)]
#[allow(unused_must_use)]
async fn first_runtime(tx: Sender<String>) {
thread::sleep(Duration::from_secs(1));
println!("first thread woke up");
tokio::spawn(async move {
for msg_id in 0..10 {
if let Err(e) = tx.send(format!("message {}", msg_id)).await …Run Code Online (Sandbox Code Playgroud) 目标是使用 Web 界面和 Spring 框架进行多房间聊天。看起来STOMP over SockJS是此类项目的最佳组合。STOMP 基于目的地的订阅解决了所有广播问题。但是,如果用户一次使用 100 个(例如)聊天,我需要在每次登录时从 Web 客户端发送 100 个订阅请求。
所以我正在寻找替代的单请求解决方案。让我来整理一下问题:
1) 有没有一种方法可以同时向多个订阅发出一个客户端 STOMP 请求?如果可以使用其他 JS 库发出这样的请求 - 那么我会很高兴尝试它。
2)有没有办法从Spring后端发起订阅?最好在服务器端为一个客户端注册多个目标消息查询 - 我可以为此使用特殊请求或在登录期间执行此操作。
3)关于这个问题还有其他建议吗?再说一遍:我很高兴尝试其他流行技术作为最后的手段。
下面的代码是最简单的 echo 服务。我只是测试这个协议和技术。
基本客户端代码:
window.onload = function () {
window.s = new SockJS("http://localhost:8080/portfolio");
window.s.onopen = function () {
window.stompClient = Stomp.over(window.s);
stompClient.connect('admin', 'admin', function(frame) {
console.log('Connected: ', frame);
stompClient.subscribe('/topic/echo', function(messageOutput) {
console.log(messageOutput.body);
})}, function(e){console.log("Fail My: ", e);})();
};
};
Run Code Online (Sandbox Code Playgroud)
踩踏配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void …Run Code Online (Sandbox Code Playgroud)