如何使用Golang中的websocket仅使用Mysql中的数据更新文本?
我只想更新文本,但不想更新整个页面,我不知道如何使用 websockets,也不理解网络上的示例。 我已经设法更新整个网格,但想法是带有相机的页面没有更新,只有文本
var socket=new WebSocket("ws://localhost:8083/ws")
setInterval(() => {
socket.send("updateJaula")
}, 2000);
<div class="datos generales">
<label id="lblNombreJaula" class="lbl-osd">NOMBRE:`+Object.values(jaula)[i].Nombre+`</label>
<br>
<label id="lblObjetivoJaula" class="lbl-osd">OBJETIVO:`+Object.values(jaula[i].Objetivo+`</label>
<br>
<label id="lblVisitaJaula" class="lbl-osd">VISITAS:`+Object.values(jaula)[i].Visita+`</label>
</div>
我真的不明白该怎么做,但我从 golang 加载了页面:
func HTTPAPIFullScreenMultiView(c *gin.Context) {
var createParams MultiViewOptions
SearchScreen(c.Param("uuid"))
err := c.ShouldBindJSON(&createParams)
if err != nil {
log.WithFields(logrus.Fields{
"module": "http_page",
"func": "HTTPAPIFullScreenMultiView",
"call": "BindJSON",
}).Errorln(err.Error())
}
log.WithFields(logrus.Fields{
"module": "http_page",
"func": "HTTPAPIFullScreenMultiView",
"call": "Options",
}).Debugln(createParams)
c.HTML(http.StatusOK, "fullscreen.html", gin.H{
"port": Storage.ServerHTTPPort(),
"screens": SearchScreen(c.Param("uuid")),
"streams": Storage.Streams,
"jaulas": ListarJaulas(),
"version": time.Now().String(),
"options": createParams,
"page": "fullscreen",
"query": c.Request.URL.Query(),
"uuid": c.Param("uuid"),
})
}
I want to update only the text but without updating the whole page, I don't know how to use websockets and I don't understand the examples on the web.
I have managed to update the entire grid but the idea is that the page with the cameras is not updated, only the text
var socket=new WebSocket("ws://localhost:8083/ws")
setInterval(() => {
socket.send("updateJaula")
}, 2000);
<div class="datos generales">
<label id="lblNombreJaula" class="lbl-osd">NOMBRE:`+Object.values(jaula)[i].Nombre+`</label>
<br>
<label id="lblObjetivoJaula" class="lbl-osd">OBJETIVO:`+Object.values(jaula[i].Objetivo+`</label>
<br>
<label id="lblVisitaJaula" class="lbl-osd">VISITAS:`+Object.values(jaula)[i].Visita+`</label>
</div>
I really don't understand what to do, but I load the page with this from golang:
func HTTPAPIFullScreenMultiView(c *gin.Context) {
var createParams MultiViewOptions
SearchScreen(c.Param("uuid"))
err := c.ShouldBindJSON(&createParams)
if err != nil {
log.WithFields(logrus.Fields{
"module": "http_page",
"func": "HTTPAPIFullScreenMultiView",
"call": "BindJSON",
}).Errorln(err.Error())
}
log.WithFields(logrus.Fields{
"module": "http_page",
"func": "HTTPAPIFullScreenMultiView",
"call": "Options",
}).Debugln(createParams)
c.HTML(http.StatusOK, "fullscreen.html", gin.H{
"port": Storage.ServerHTTPPort(),
"screens": SearchScreen(c.Param("uuid")),
"streams": Storage.Streams,
"jaulas": ListarJaulas(),
"version": time.Now().String(),
"options": createParams,
"page": "fullscreen",
"query": c.Request.URL.Query(),
"uuid": c.Param("uuid"),
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要执行多项操作才能
仅更新文本而不更新整个页面
。但在较高层面上,这些是您需要做的:首先,您已经拥有网络服务器(在
http
或https
协议下)来提供您的内容。接下来,您需要添加另一个名为websocket 服务器的服务器。只有这样,您才能使用 js 语句从 websocket 客户端(javascript)连接到 websocket 服务器(Go):
然后,您需要在 websocket 客户端上添加多个侦听器,以处理来自 websocket 服务器的传入消息/数据。
下一步,在后端,每当用户当前打开的数据发生一些更改时,后端必须将其推送到客户端,即您的前端,以便您可以相应地更新网格。
在 Go 中可用于 websocket 的一个库是 Gorilla Websocket。存储库中有一些示例。
另外,如果您需要额外的示例,我创建了一个简单的聊天应用程序,它利用带有 Go 的 websocket 和纯 JavaScript。与您需要的相比,它可能有所不同,但您也许可以从那里学到一些东西
https://github.com/novalagung/dasarpemrogramangolang-example/tree/master/chapter-D.3-golang-web-socket-chatting-app
There are several things you will need to do to achieve
update only the text but without updating the whole page
. but on high level, these are what you need to do:first of all, you already have the webserver (under
http
orhttps
protocol) up to serve your content.Next, you need to add another server called, websocket server. Only then you will be able to connect from websocket client (javascript) to the websocket server (Go), using js statement:
Then, you need to add several listener on the websocket client, to handle incoming message/data from the websocket server.
Next step, on the backend side, whenever there are some changes on data that are currently opened by the user, the backend must push it to the client, which is your front end, so you can update the grid accordingly.
One library that you can use for websocket in Go, is Gorilla Websocket. There are a few examples there within the repo.
Also if you need an additional example, I have created a simple chatting app that utilizes websocket with Go and plain javascript. It might be different compared to what you need but you might be able to learn something from there
https://github.com/novalagung/dasarpemrogramangolang-example/tree/master/chapter-D.3-golang-web-socket-chatting-app
又是我。我需要每 2 秒更新一次标签。做一个 MySQL 查询(我已经从 Golang 创建了它们),你认为它可以是这样的吗?
It's me again. I need the labels to be updated every 2 seconds. Doing a MySQL query (I already have them created from Golang), do you think it could be like this?