gpt4 book ai didi

javascript - 如何向 Web 界面通知变量的状态?

转载 作者:行者123 更新时间:2023-11-30 06:37:11 24 4
gpt4 key购买 nike

我正在将 Spring MVC 用于 Web 应用程序(使用 JQuery 作为前端)。我有一个服务,它的状态变量会根据某种方法的进度而变化。

代码如下所示:

@Service
public class PatchGenerationService {
private String status;

public String getStatus() {
return status;
}

public void generatePatch(){
status = STATUS_PREPARE;
//some code
status = STATUS_LOOKUP;
//and so on
status = STATUS_FINISHED;
}
}

有多个异步线程同时运行此服务,由不同的客户端创建。我希望能够通知每个客户端为他创建的线程的状态。

这是创建 PatchGenerationService 的服务:

@Async
public static void createPatchFromSelectedCommits(String branch, String tagName, List<Commit> commits) {
log.info("generating a new patch");
PatchGenerationService patchGenerationService = new PatchGenerationService();
patchGenerationService.generatePatch(/*attributes*/);
log.info("patch generation ended");
}

每次状态变量发生变化时,如何将状态变量的值信息发送给客户端?

最佳答案

您应该查看此文档:Using WebSocket to build an interactive web application

Using WebSocket to build an interactive web application

This guide walks you through the process of creating a "hello world" application that sends messages back and forth, between a browser and the server. WebSocket is a very thin, lightweight layer above TCP. It makes it very suitable to use "subprotocols" to embed messages. In this guide we’ll dive in and use STOMP messaging with Spring to create an interactive web application.

简单来说,你的后端会向你的前端发送一条消息,通知他状态的变化。

关于javascript - 如何向 Web 界面通知变量的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45016183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com