@@ -128,7 +128,7 @@ func TestHandshake(t *testing.T) {
128128 http .Error (w , err .Error (), http .StatusBadRequest )
129129 return
130130 }
131- _ = ws .Serve (r .Context (), websocket .EchoHandler )
131+ _ = ws .Handle (r .Context (), websocket .EchoHandler )
132132 }))
133133 defer srv .Close ()
134134
@@ -229,7 +229,7 @@ func TestConnectionLimits(t *testing.T) {
229229 },
230230 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
231231 start := time .Now ()
232- err := ws .Serve (t .Context (), websocket .EchoHandler )
232+ err := ws .Handle (t .Context (), websocket .EchoHandler )
233233 elapsed := time .Since (start )
234234 assert .Equal (t , elapsed >= maxDuration , true , "not enough time passed" )
235235 assert .Error (t , err , os .ErrDeadlineExceeded )
@@ -374,7 +374,7 @@ func TestProtocolOkay(t *testing.T) {
374374
375375 // server just runs EchoHandler to reply to client
376376 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
377- assert .NilError (t , ws .Serve (t .Context (), websocket .EchoHandler ))
377+ assert .NilError (t , ws .Handle (t .Context (), websocket .EchoHandler ))
378378 },
379379 }.Run (t )
380380 })
@@ -459,7 +459,7 @@ func TestProtocolOkay(t *testing.T) {
459459
460460 // server just echoes messages from the client
461461 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
462- assert .NilError (t , ws .Serve (t .Context (), websocket .EchoHandler ))
462+ assert .NilError (t , ws .Handle (t .Context (), websocket .EchoHandler ))
463463 },
464464 }.Run (t )
465465 })
@@ -482,7 +482,7 @@ func TestProtocolOkay(t *testing.T) {
482482 },
483483 // server just echoes messages from the client
484484 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
485- assert .NilError (t , ws .Serve (t .Context (), websocket .EchoHandler ))
485+ assert .NilError (t , ws .Handle (t .Context (), websocket .EchoHandler ))
486486 },
487487 }.Run (t )
488488 })
@@ -606,7 +606,7 @@ func TestProtocolErrors(t *testing.T) {
606606 // protocol errors automatically
607607 serverOpts : newOpts (t ),
608608 serverTest : func (t testing.TB , ws * websocket.Websocket , conn net.Conn ) {
609- assert .Error (t , ws .Serve (t .Context (), websocket .EchoHandler ), tc .wantCloseReason )
609+ assert .Error (t , ws .Handle (t .Context (), websocket .EchoHandler ), tc .wantCloseReason )
610610 },
611611 }.Run (t )
612612 })
@@ -675,7 +675,7 @@ func TestCloseFrameValidation(t *testing.T) {
675675 // server just runs echo handler, which should process all
676676 // protocol errors automatically
677677 serverTest : func (t testing.TB , ws * websocket.Websocket , conn net.Conn ) {
678- assert .Error (t , ws .Serve (t .Context (), websocket .EchoHandler ), tc .wantErr )
678+ assert .Error (t , ws .Handle (t .Context (), websocket .EchoHandler ), tc .wantErr )
679679 },
680680 }.Run (t )
681681 })
@@ -916,7 +916,7 @@ func TestErrorHandling(t *testing.T) {
916916 }
917917 },
918918 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
919- err := ws .Serve (t .Context (), websocket .EchoHandler )
919+ err := ws .Handle (t .Context (), websocket .EchoHandler )
920920 assert .Error (t , err , writeErr )
921921 },
922922 }.Run (t )
@@ -941,7 +941,7 @@ func TestErrorHandling(t *testing.T) {
941941 // server tries to read message with the cancelable context, which
942942 // should be canceled while waiting for the continuation frame.
943943 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
944- assert .Error (t , ws .Serve (ctx , websocket .EchoHandler ), context .Canceled )
944+ assert .Error (t , ws .Handle (ctx , websocket .EchoHandler ), context .Canceled )
945945 },
946946 }.Run (t )
947947 })
@@ -1143,7 +1143,7 @@ func TestServeLoop(t *testing.T) {
11431143 // server runs Serve with a custom handler that returns an error
11441144 // when it receives a "fail" message
11451145 serverTest : func (t testing.TB , ws * websocket.Websocket , _ net.Conn ) {
1146- err := ws .Serve (t .Context (), func (ctx context.Context , msg * websocket.Message ) (* websocket.Message , error ) {
1146+ err := ws .Handle (t .Context (), func (ctx context.Context , msg * websocket.Message ) (* websocket.Message , error ) {
11471147 if bytes .Equal (msg .Payload , []byte ("fail" )) {
11481148 return nil , wantErr
11491149 }
@@ -1179,7 +1179,7 @@ func TestServeLoop(t *testing.T) {
11791179 }
11801180 },
11811181 serverTest : func (t testing.TB , ws * websocket.Websocket , conn net.Conn ) {
1182- err := ws .Serve (t .Context (), func (ctx context.Context , msg * websocket.Message ) (* websocket.Message , error ) {
1182+ err := ws .Handle (t .Context (), func (ctx context.Context , msg * websocket.Message ) (* websocket.Message , error ) {
11831183 return nil , appErr
11841184 })
11851185 // the error returned wraps both the application-level error
@@ -1566,7 +1566,7 @@ var (
15661566// ============================================================================
15671567// Examples
15681568// ============================================================================
1569- func ExampleServe () {
1569+ func ExampleHandle () {
15701570 handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
15711571 ws , err := websocket .Accept (w , r , websocket.Options {
15721572 ReadTimeout : 500 * time .Millisecond ,
@@ -1578,10 +1578,10 @@ func ExampleServe() {
15781578 http .Error (w , err .Error (), http .StatusBadRequest )
15791579 return
15801580 }
1581- if err := ws .Serve (r .Context (), websocket .EchoHandler ); err != nil {
1582- // an error returned by Serve is strictly informational; the
1581+ if err := ws .Handle (r .Context (), websocket .EchoHandler ); err != nil {
1582+ // an error returned by Handle is strictly informational; the
15831583 // connection will already be closed at this point.
1584- log .Printf ("error serving websocket connection: %s" , err )
1584+ log .Printf ("error handling websocket connection: %s" , err )
15851585 }
15861586 })
15871587 if err := http .ListenAndServe (":8080" , handler ); err != nil {
0 commit comments