使用WebSocket的Angular应用程序监听REST终端点。

huangapple go评论75阅读模式
英文:

Listening with WebSocket Angular app to a REST end point

问题

我已经实现了一个具有一些产品实体端点的Spring Boot REST服务器。

在客户端,也就是一个Angular应用中,我只想监听这些REST端点。我的意思是,与聊天应用程序相反,无需将消息/产品发送到服务器。因此,客户端应用程序仅监听变化。

我不确定是否应该修改我的Spring Boot REST服务器以添加WebSocket功能。

我看到很多关于Spring Boot和聊天应用程序的教程,但我不确定那是否适用于我这个简单的应用程序。

英文:

I already implemented a Spring Boot REST server with some endpoints for Products entity.

In the client-side, which is an Angular application, I want to only listen to the REST endpoints. What I mean is that in contrast to chat apps, there is no need to send messages/products to the server. So the client app only listens for changes.

I am not sure if I should change my Spring Boot REST server to add Websocket functionality or not.

I see a lot of tutorials about Spring boot and chat applications, but I am not sure if that's what I need for my simple application.

答案1

得分: 0

如果您将在服务器和客户端之间建立的通信通道是单向的,那么您必须使用服务器发送事件(Server Sent Events,SSE) [1]。简而言之,客户端将通过打开事件流请求来连接到SSE终端点。这里我附上一个使用curl连接的示例

curl --location -X GET 'localhost:8080/sse' \
 -H 'Content-Type: text/event-stream;charset=UTF-8' \
 -H 'Accept: text/event-stream;charset=UTF-8'

请注意,SSE终端点是**/sse**。还请注意这些头部信息。

[1] https://www.baeldung.com/spring-server-sent-events

英文:

If the communication channel that you will set up between your server and your client is unidirectional, then you must use server sent events [1]. In summary, the client will connect to the SSE endpoint by opening an event-streaming request. Here I attach an example on how to connect with curl

curl --location -X GET 'localhost:8080/sse' \
 -H 'Content-Type: text/event-stream;charset=UTF-8' \
 -H 'Accept: text/event-stream;charset=UTF-8'

Note the SSE endpoint is /sse. Also note the headers.

[1] https://www.baeldung.com/spring-server-sent-events

huangapple
  • 本文由 发表于 2020年8月17日 09:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63443388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定