Gorilla Websocket 中的 WriteMessage 和 ReadMessage 是如何工作的?
How do WriteMessage and ReadMessage in Gorilla Websocket work?
我正在使用 Gorilla Websocket 并且很好奇 WriteMessage 和 ReadMessage 函数是如何工作的。
WriteMessage函数是否同步向客户端发送字节数据?或者 ReadMessage 是否主动从服务器获取数据(根据文档,我们需要创建一个事件循环来调用 ReadMessage 函数)。
如果服务端一直调用WriteMessage,但是没有人读取消息(客户端通过事件循环调用ReadMessage函数),会发生什么情况,数据是丢失了,还是一直保留到下一个读取请求到来?谢谢。
您可以在此处找到该函数的源代码:https://github.com/gorilla/websocket/blob/c3dd95aea9779669bb3daafbd84ee0530c8ce1c1/conn.go#L751-L774
看起来这是 blocking/sync 方法。
根据跟踪,他们在这里创建作者:https://github.com/gorilla/websocket/blob/c3dd95aea9779669bb3daafbd84ee0530c8ce1c1/conn.go#L766
w, err := c.NextWriter(messageType)
然后他们在写 data
:
if _, err = w.Write(data); err != nil {
return err
}
这是阻塞的,因为他们正在该函数的最后一行关闭连接,所以此时必须完成写入。
这是 io.WriteCloser
接口返回到 w
变量的行为。
What happened if the server kept calling WriteMessage, but no one read the message (the client calls the ReadMessage function through event loop), is the data is lost, or is it kept until the next read request came? Thank You.
- 您应该设置 Write/Read 超时。
- 图书馆没有为您重复发送数据。您需要在您的应用程序中实现此逻辑。
- 如果服务器已启动并收到您的连接,(可能)它将读取您的消息(如果它在执行您的数据之前没有停止)。
- 如果您发送消息而服务器已死(未收到您的消息),您的数据将丢失。
附加参考:
w.Write
函数:https://github.com/gorilla/websocket/blob/c3dd95aea9779669bb3daafbd84ee0530c8ce1c1/conn.go#L650-L675
io.WriteCloser
接口描述:https://golang.org/pkg/io/#WriteCloser
- Gorila Websocket 超时:https://pkg.go.dev/github.com/gorilla/websocket#Conn.SetReadDeadline
- Gorila 的超时文档:https://pkg.go.dev/github.com/gorilla/websocket#Conn.SetReadDeadline
Does the WriteMessage function send the bytes data to the client synchronously?
WriteMessage 将数据写入底层网络连接。
操作系统网络连接维护数据缓冲区以传输到对等方。当对等方确认收到数据时,数据从缓冲区中删除。
将所有应用程序数据添加到缓冲区后,写入操作系统网络连接returns。写可以阻止在缓冲区中等待 space。
几乎总是这样,应用程序写入调用 returns 在对等方接收数据之前。成功调用 WriteMessage 确实意味着对等应用程序读取了数据。
Or Does the ReadMessage actively fetch the data from the server (according to the documentation, we need to create an event loop to call the ReadMessage function).
ReadMessage 调用在基础网络连接上读取。
操作系统缓冲了从对等方接收到的一些数据。
读取正在运行的网络连接块,直到缓冲区中有可用数据。
What happened if the server kept calling WriteMessage, but no one read the message.
WriteMessage 最终会阻塞等待操作系统传输缓冲区中的space。
使用 write deadline 来防止在死掉或卡住的对等点上永远阻塞。
is the data is lost, or is it kept until the next read request came?
数据保存在操作系统传输和接收缓冲区中。
当传输缓冲区已满时,应用程序写入 websocket 连接块。
仅当对等应用程序在对等应用程序读取数据之前终止时,数据才会丢失。
我正在使用 Gorilla Websocket 并且很好奇 WriteMessage 和 ReadMessage 函数是如何工作的。
WriteMessage函数是否同步向客户端发送字节数据?或者 ReadMessage 是否主动从服务器获取数据(根据文档,我们需要创建一个事件循环来调用 ReadMessage 函数)。
如果服务端一直调用WriteMessage,但是没有人读取消息(客户端通过事件循环调用ReadMessage函数),会发生什么情况,数据是丢失了,还是一直保留到下一个读取请求到来?谢谢。
您可以在此处找到该函数的源代码:https://github.com/gorilla/websocket/blob/c3dd95aea9779669bb3daafbd84ee0530c8ce1c1/conn.go#L751-L774
看起来这是 blocking/sync 方法。
根据跟踪,他们在这里创建作者:https://github.com/gorilla/websocket/blob/c3dd95aea9779669bb3daafbd84ee0530c8ce1c1/conn.go#L766
w, err := c.NextWriter(messageType)
然后他们在写 data
:
if _, err = w.Write(data); err != nil {
return err
}
这是阻塞的,因为他们正在该函数的最后一行关闭连接,所以此时必须完成写入。
这是 io.WriteCloser
接口返回到 w
变量的行为。
What happened if the server kept calling WriteMessage, but no one read the message (the client calls the ReadMessage function through event loop), is the data is lost, or is it kept until the next read request came? Thank You.
- 您应该设置 Write/Read 超时。
- 图书馆没有为您重复发送数据。您需要在您的应用程序中实现此逻辑。
- 如果服务器已启动并收到您的连接,(可能)它将读取您的消息(如果它在执行您的数据之前没有停止)。
- 如果您发送消息而服务器已死(未收到您的消息),您的数据将丢失。
附加参考:
w.Write
函数:https://github.com/gorilla/websocket/blob/c3dd95aea9779669bb3daafbd84ee0530c8ce1c1/conn.go#L650-L675io.WriteCloser
接口描述:https://golang.org/pkg/io/#WriteCloser- Gorila Websocket 超时:https://pkg.go.dev/github.com/gorilla/websocket#Conn.SetReadDeadline
- Gorila 的超时文档:https://pkg.go.dev/github.com/gorilla/websocket#Conn.SetReadDeadline
Does the WriteMessage function send the bytes data to the client synchronously?
WriteMessage 将数据写入底层网络连接。
操作系统网络连接维护数据缓冲区以传输到对等方。当对等方确认收到数据时,数据从缓冲区中删除。
将所有应用程序数据添加到缓冲区后,写入操作系统网络连接returns。写可以阻止在缓冲区中等待 space。
几乎总是这样,应用程序写入调用 returns 在对等方接收数据之前。成功调用 WriteMessage 确实意味着对等应用程序读取了数据。
Or Does the ReadMessage actively fetch the data from the server (according to the documentation, we need to create an event loop to call the ReadMessage function).
ReadMessage 调用在基础网络连接上读取。
操作系统缓冲了从对等方接收到的一些数据。
读取正在运行的网络连接块,直到缓冲区中有可用数据。
What happened if the server kept calling WriteMessage, but no one read the message.
WriteMessage 最终会阻塞等待操作系统传输缓冲区中的space。
使用 write deadline 来防止在死掉或卡住的对等点上永远阻塞。
is the data is lost, or is it kept until the next read request came?
数据保存在操作系统传输和接收缓冲区中。
当传输缓冲区已满时,应用程序写入 websocket 连接块。
仅当对等应用程序在对等应用程序读取数据之前终止时,数据才会丢失。