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

Method Write

write.go:191–221  ·  view source on GitHub ↗

Write writes the given bytes to the WebSocket connection.

(p []byte)

Source from the content-addressed store, hash-verified

189
190// Write writes the given bytes to the WebSocket connection.
191func (mw *msgWriter) Write(p []byte) (_ int, err error) {
192 err = mw.writeMu.lock(mw.ctx)
193 if err != nil {
194 return 0, fmt.Errorf("failed to write: %w", err)
195 }
196 defer mw.writeMu.unlock()
197
198 if mw.closed {
199 return 0, errors.New("cannot use closed writer")
200 }
201
202 defer func() {
203 if err != nil {
204 err = fmt.Errorf("failed to write: %w", err)
205 }
206 }()
207
208 if mw.c.flate() {
209 // Only enables flate if the length crosses the
210 // threshold on the first frame
211 if mw.opcode != opContinuation && len(p) >= mw.c.flateThreshold {
212 mw.ensureFlate()
213 }
214 }
215
216 if mw.flate {
217 return mw.flateWriter.Write(p)
218 }
219
220 return mw.write(p)
221}
222
223func (mw *msgWriter) write(p []byte) (int, error) {
224 n, err := mw.c.writeFrame(mw.ctx, false, mw.flate, mw.opcode, p)

Callers

nothing calls this directly

Calls 6

ensureFlateMethod · 0.95
writeMethod · 0.95
lockMethod · 0.80
flateMethod · 0.80
unlockMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected