mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-13 00:11:22 +08:00
PRF
@gxlct008
This commit is contained in:
parent
77059dc9aa
commit
a3cce7c436
@ -1,6 +1,6 @@
|
|||||||
[#]: collector: (lujun9972)
|
[#]: collector: (lujun9972)
|
||||||
[#]: translator: (gxlct008)
|
[#]: translator: (gxlct008)
|
||||||
[#]: reviewer: ( )
|
[#]: reviewer: (wxy)
|
||||||
[#]: publisher: ( )
|
[#]: publisher: ( )
|
||||||
[#]: url: ( )
|
[#]: url: ( )
|
||||||
[#]: subject: (Building a Messenger App: Realtime Messages)
|
[#]: subject: (Building a Messenger App: Realtime Messages)
|
||||||
@ -10,6 +10,8 @@
|
|||||||
构建一个即时消息应用(五):实时消息
|
构建一个即时消息应用(五):实时消息
|
||||||
======
|
======
|
||||||
|
|
||||||
|
![](https://img.linux.net.cn/data/attachment/album/202010/05/091113edbuavorm89looja.jpg)
|
||||||
|
|
||||||
本文是该系列的第五篇。
|
本文是该系列的第五篇。
|
||||||
|
|
||||||
* [第一篇: 模式][1]
|
* [第一篇: 模式][1]
|
||||||
@ -17,8 +19,7 @@
|
|||||||
* [第三篇: 对话][3]
|
* [第三篇: 对话][3]
|
||||||
* [第四篇: 消息][4]
|
* [第四篇: 消息][4]
|
||||||
|
|
||||||
|
对于实时消息,我们将使用 <ruby>[服务器发送事件][5]<rt>Server-Sent Events</rt></ruby>。这是一个打开的连接,我们可以在其中传输数据流。我们会有个端点,用户会在其中订阅发送给他的所有消息。
|
||||||
对于实时消息,我们将使用 [服务器发送事件 (Server-Sent Events)][5]。这是一个开放的连接,我们可以在其中传输数据。我们将拥有用户订阅发送给他的所有消息的和端点。
|
|
||||||
|
|
||||||
### 消息户端
|
### 消息户端
|
||||||
|
|
||||||
@ -69,7 +70,7 @@ func broadcastMessage(message Message) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
该函数查询收件人 ID (其他参与者 ID),并将消息发送给所有客户端。
|
该函数查询接收者 ID(其他参与者 ID),并将消息发送给所有客户端。
|
||||||
|
|
||||||
### 订阅消息
|
### 订阅消息
|
||||||
|
|
||||||
@ -126,8 +127,7 @@ func subscribeToMessages(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
首先,它检查请求头是否正确,并检查服务器是否支持流式传输。我们创建一个消息通道,用它来构建一个客户端,并将其存储在客户端 map 中。每当创建新消息时,它都会进入这个通道,因此我们可以通过 `for-select` 循环从中读取。
|
首先,它检查请求头是否正确,并检查服务器是否支持流式传输。我们创建一个消息通道,用它来构建一个客户端,并将其存储在客户端映射中。每当创建新消息时,它都会进入这个通道,因此我们可以通过 `for-select` 循环从中读取。
|
||||||
|
|
||||||
|
|
||||||
<ruby>服务器发送事件<rt>Server-Sent Events</rt></ruby>使用以下格式发送数据:
|
<ruby>服务器发送事件<rt>Server-Sent Events</rt></ruby>使用以下格式发送数据:
|
||||||
|
|
||||||
@ -141,17 +141,17 @@ data: some data here\n\n
|
|||||||
data: {"foo":"bar"}\n\n
|
data: {"foo":"bar"}\n\n
|
||||||
```
|
```
|
||||||
|
|
||||||
我们使用 `fmt.Fprintf()` 以这种格式写入响应写入器,并在循环的每次迭代中刷新数据。
|
我们使用 `fmt.Fprintf()` 以这种格式写入响应<ruby>写入器<rt>writter</rt></ruby>,并在循环的每次迭代中刷新数据。
|
||||||
|
|
||||||
这个循环会一直运行,直到使用请求上下文关闭连接为止。我们延迟了通道的关闭和客户端的删除,因此,当循环结束时,频道将被关闭,客户端将不会收到更多的消息。
|
这个循环会一直运行,直到使用请求上下文关闭连接为止。我们延迟了通道的关闭和客户端的删除,因此,当循环结束时,通道将被关闭,客户端不会收到更多的消息。
|
||||||
|
|
||||||
注意,<ruby>服务器发送事件<rt>Server-Sent Events</rt></ruby> (EventSource) 的 JavaScript API 不支持设置自定义头部😒,所以我们不能设置 `Authorization: Bearer <token>`。这就是为什么 `guard()` 中间件也会从 URL 查询字符串中读取令牌的原因。
|
注意,<ruby>服务器发送事件<rt>Server-Sent Events</rt></ruby>(EventSource)的 JavaScript API 不支持设置自定义请求头😒,所以我们不能设置 `Authorization: Bearer <token>`。这就是为什么 `guard()` 中间件也会从 URL 查询字符串中读取令牌的原因。
|
||||||
|
|
||||||
* * *
|
* * *
|
||||||
|
|
||||||
实时消息部分到此结束。我想说的是,这就是后端的全部内容。但是为了编写前端代码,我将再增加一个登录端点。一个仅用于开发的登录。
|
实时消息部分到此结束。我想说的是,这就是后端的全部内容。但是为了编写前端代码,我将再增加一个登录端点:一个仅用于开发的登录。
|
||||||
|
|
||||||
[Souce Code][7]
|
- [源代码][7]
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -159,17 +159,17 @@ via: https://nicolasparada.netlify.com/posts/go-messenger-realtime-messages/
|
|||||||
|
|
||||||
作者:[Nicolás Parada][a]
|
作者:[Nicolás Parada][a]
|
||||||
选题:[lujun9972][b]
|
选题:[lujun9972][b]
|
||||||
译者:[译者ID](https://github.com/gxlct008)
|
译者:[gxlct008](https://github.com/gxlct008)
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
校对:[wxy](https://github.com/wxy)
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
[a]: https://nicolasparada.netlify.com/
|
[a]: https://nicolasparada.netlify.com/
|
||||||
[b]: https://github.com/lujun9972
|
[b]: https://github.com/lujun9972
|
||||||
[1]: https://nicolasparada.netlify.com/posts/go-messenger-schema/
|
[1]: https://linux.cn/article-11396-1.html
|
||||||
[2]: https://nicolasparada.netlify.com/posts/go-messenger-oauth/
|
[2]: https://linux.cn/article-11510-1.html
|
||||||
[3]: https://nicolasparada.netlify.com/posts/go-messenger-conversations/
|
[3]: https://linux.cn/article-12056-1.html
|
||||||
[4]: https://nicolasparada.netlify.com/posts/go-messenger-messages/
|
[4]: https://linux.cn/article-12680-1.html
|
||||||
[5]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
|
[5]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
|
||||||
[6]: https://developer.mozilla.org/en-US/docs/Web/API/EventSource
|
[6]: https://developer.mozilla.org/en-US/docs/Web/API/EventSource
|
||||||
[7]: https://github.com/nicolasparada/go-messenger-demo
|
[7]: https://github.com/nicolasparada/go-messenger-demo
|
||||||
|
Loading…
Reference in New Issue
Block a user