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

Function write

wsjson/wsjson.go:52–68  ·  view source on GitHub ↗
(ctx context.Context, c *websocket.Conn, v any)

Source from the content-addressed store, hash-verified

50}
51
52func write(ctx context.Context, c *websocket.Conn, v any) (err error) {
53 defer errd.Wrap(&err, "failed to write JSON message")
54
55 // json.Marshal cannot reuse buffers between calls as it has to return
56 // a copy of the byte slice but Encoder does as it directly writes to w.
57 err = json.NewEncoder(util.WriterFunc(func(p []byte) (int, error) {
58 err := c.Write(ctx, websocket.MessageText, p)
59 if err != nil {
60 return 0, err
61 }
62 return len(p), nil
63 })).Encode(v)
64 if err != nil {
65 return fmt.Errorf("failed to marshal JSON: %w", err)
66 }
67 return nil
68}

Callers 1

WriteFunction · 0.85

Calls 3

WrapFunction · 0.92
WriterFuncFuncType · 0.92
WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…