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

Function Echo

internal/test/wstest/echo.go:51–82  ·  view source on GitHub ↗

Echo writes a message and ensures the same is sent back on c.

(ctx context.Context, c *websocket.Conn, max int)

Source from the content-addressed store, hash-verified

49
50// Echo writes a message and ensures the same is sent back on c.
51func Echo(ctx context.Context, c *websocket.Conn, max int) error {
52 expType := websocket.MessageBinary
53 if xrand.Bool() {
54 expType = websocket.MessageText
55 }
56
57 msg := randMessage(expType, xrand.Int(max))
58
59 writeErr := xsync.Go(func() error {
60 return c.Write(ctx, expType, msg)
61 })
62
63 actType, act, err := c.Read(ctx)
64 if err != nil {
65 return err
66 }
67
68 err = <-writeErr
69 if err != nil {
70 return err
71 }
72
73 if expType != actType {
74 return fmt.Errorf("unexpected message typ (%v): %v", expType, actType)
75 }
76
77 if !bytes.Equal(msg, act) {
78 return fmt.Errorf("unexpected msg read: %#v", act)
79 }
80
81 return nil
82}
83
84func randMessage(typ websocket.MessageType, n int) []byte {
85 if typ == websocket.MessageBinary {

Callers 2

TestConnFunction · 0.92
TestWasmFunction · 0.92

Calls 6

BoolFunction · 0.92
IntFunction · 0.92
GoFunction · 0.92
randMessageFunction · 0.85
WriteMethod · 0.45
ReadMethod · 0.45

Tested by 2

TestConnFunction · 0.74
TestWasmFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…