golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/websocket/examplehandler_test.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package websocket_test
     6  
     7  import (
     8  	"io"
     9  	"net/http"
    10  
    11  	"golang.org/x/net/websocket"
    12  )
    13  
    14  // Echo the data received on the WebSocket.
    15  func EchoServer(ws *websocket.Conn) {
    16  	io.Copy(ws, ws)
    17  }
    18  
    19  // This example demonstrates a trivial echo server.
    20  func ExampleHandler() {
    21  	http.Handle("/echo", websocket.Handler(EchoServer))
    22  	err := http.ListenAndServe(":12345", nil)
    23  	if err != nil {
    24  		panic("ListenAndServe: " + err.Error())
    25  	}
    26  }