WebSocket - 关闭握手大猩猩
WebSocket - Closing Handshake Gorilla
来自 WebSocket RFC 的片段:
To Start the WebSocket Closing Handshake with a status code (Section 7.4) /code/ and an optional close reason (Section 7.1.6) /reason/, an endpoint MUST send a Close control frame, as described in Section 5.5.1, whose status code is set to /code/ and whose close reason is set to /reason/. Once an endpoint has both sent and received a Close control frame, that endpoint SHOULD Close the WebSocket Connection as defined in Section 7.1.1.
我正在尝试使用带有以下代码的 Gorilla WebSocket 包进行关闭握手:
服务器:
// Create upgrader function
conn, err := upgrader.Upgrade(w, r, nil)
// If there is an error stop everything.
if err != nil {
fmt.Println(err)
return
}
for {
// Read Messages
_, _, err := conn.ReadMessage()
// Client is programmed to send a close frame immediately...
// When reading close frame resend close frame with same
// reason and code
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1000, "woops"))
fmt.Println(err)
break
}
客户:
d := &websocket.Dialer{}
conn, _, err := d.Dial("ws://localhost:8080", nil)
if err != nil {
fmt.Println(err)
return
}
go func() {
for {
// Read Messages
_, _, err := conn.ReadMessage()
if c, k := err.(*websocket.CloseError); k {
if(c.Code == 1000) {
// Never entering since c.Code == 1005
fmt.Println(err)
break
}
}
}
}()
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1000, "woops"))
for {}
服务器正在按预期读取关闭帧并输出以下内容:
websocket: close 1000 (normal): woops
但是客户端一发送关闭消息就好像停止阅读一样。 ReadMessage
继续 return 错误 1005。我做错了什么?
服务器用 code:
响应关闭帧
c.WriteControl(CloseMessage, []byte{}, time.Now().Add(writeWait))
这被客户端转换为关闭代码 1005(未收到状态)。
客户端应用程序看不到服务器写入的 1000 oops 关闭帧,因为 websocket 连接在收到第一个关闭帧后停止从网络读取。
当 ReadMessage 返回错误时,客户端应用程序应退出循环。无需检查特定的关闭代码。
for {
// Read Messages
_, _, err := conn.ReadMessage()
if err != nil {
break
}
}
与问题中的问题无关,服务器应用程序应该在发送关闭帧后close websocket 连接。
也与问题中的问题无关,使用select {}
而不是for {}
来阻塞主goroutine。前者只是简单地阻塞了 goroutine。后者使用 CPU 时间旋转。
来自 WebSocket RFC 的片段:
To Start the WebSocket Closing Handshake with a status code (Section 7.4) /code/ and an optional close reason (Section 7.1.6) /reason/, an endpoint MUST send a Close control frame, as described in Section 5.5.1, whose status code is set to /code/ and whose close reason is set to /reason/. Once an endpoint has both sent and received a Close control frame, that endpoint SHOULD Close the WebSocket Connection as defined in Section 7.1.1.
我正在尝试使用带有以下代码的 Gorilla WebSocket 包进行关闭握手:
服务器:
// Create upgrader function
conn, err := upgrader.Upgrade(w, r, nil)
// If there is an error stop everything.
if err != nil {
fmt.Println(err)
return
}
for {
// Read Messages
_, _, err := conn.ReadMessage()
// Client is programmed to send a close frame immediately...
// When reading close frame resend close frame with same
// reason and code
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1000, "woops"))
fmt.Println(err)
break
}
客户:
d := &websocket.Dialer{}
conn, _, err := d.Dial("ws://localhost:8080", nil)
if err != nil {
fmt.Println(err)
return
}
go func() {
for {
// Read Messages
_, _, err := conn.ReadMessage()
if c, k := err.(*websocket.CloseError); k {
if(c.Code == 1000) {
// Never entering since c.Code == 1005
fmt.Println(err)
break
}
}
}
}()
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(1000, "woops"))
for {}
服务器正在按预期读取关闭帧并输出以下内容:
websocket: close 1000 (normal): woops
但是客户端一发送关闭消息就好像停止阅读一样。 ReadMessage
继续 return 错误 1005。我做错了什么?
服务器用 code:
响应关闭帧 c.WriteControl(CloseMessage, []byte{}, time.Now().Add(writeWait))
这被客户端转换为关闭代码 1005(未收到状态)。
客户端应用程序看不到服务器写入的 1000 oops 关闭帧,因为 websocket 连接在收到第一个关闭帧后停止从网络读取。
当 ReadMessage 返回错误时,客户端应用程序应退出循环。无需检查特定的关闭代码。
for {
// Read Messages
_, _, err := conn.ReadMessage()
if err != nil {
break
}
}
与问题中的问题无关,服务器应用程序应该在发送关闭帧后close websocket 连接。
也与问题中的问题无关,使用select {}
而不是for {}
来阻塞主goroutine。前者只是简单地阻塞了 goroutine。后者使用 CPU 时间旋转。