MCPcopy
hub / github.com/coder/websocket / Close

Method Close

close.go:99–128  ·  view source on GitHub ↗

Close performs the WebSocket close handshake with the given status code and reason. It will write a WebSocket close frame with a timeout of 5s and then wait 5s for the peer to send a close frame. All data messages received from the peer during the close handshake will be discarded. The connection

(code StatusCode, reason string)

Source from the content-addressed store, hash-verified

97// Close will unblock all goroutines interacting with the connection once
98// complete.
99func (c *Conn) Close(code StatusCode, reason string) (err error) {
100 defer errd.Wrap(&err, "failed to close WebSocket")
101
102 if c.casClosing() {
103 err = c.waitGoroutines()
104 if err != nil {
105 return err
106 }
107 return net.ErrClosed
108 }
109 defer func() {
110 if errors.Is(err, net.ErrClosed) {
111 err = nil
112 }
113 }()
114
115 err = c.closeHandshake(code, reason)
116
117 err2 := c.close()
118 if err == nil && err2 != nil {
119 err = err2
120 }
121
122 err2 = c.waitGoroutines()
123 if err == nil && err2 != nil {
124 err = err2
125 }
126
127 return err
128}
129
130// CloseNow closes the WebSocket connection without attempting a close handshake.
131// Use when you do not want the overhead of the close handshake.

Callers 15

CloseReadMethod · 0.95
subscribeMethod · 0.95
TestConnFunction · 0.45
TestWasmFunction · 0.45
goDiscardLoopMethod · 0.45
BenchmarkConnFunction · 0.45
echoServerFunction · 0.45
assertCloseFunction · 0.45
TestConcurrentClosePingFunction · 0.45
TestConnClosePropagationFunction · 0.45
dialFunction · 0.45
closeMethod · 0.45

Calls 5

casClosingMethod · 0.95
waitGoroutinesMethod · 0.95
closeHandshakeMethod · 0.95
closeMethod · 0.95
WrapFunction · 0.92

Tested by 15

TestConnFunction · 0.36
TestWasmFunction · 0.36
goDiscardLoopMethod · 0.36
BenchmarkConnFunction · 0.36
echoServerFunction · 0.36
assertCloseFunction · 0.36
TestConcurrentClosePingFunction · 0.36
TestConnClosePropagationFunction · 0.36
ExampleAcceptFunction · 0.36