1. package main
    2. import (
    3. "fmt"
    4. "log"
    5. "net/http"
    6. "code.google.com/p/go.net/websocket"
    7. )
    8. func main() {
    9. http.Handle("/", websocket.Handler(Echo))
    10. if err := http.ListenAndServe(":1234", nil); err != nil {
    11. log.Fatal("ListenAndServer: ", err)
    12. }
    13. }
    14. func Echo(ws *websocket.Conn) {
    15. var err error
    16. for {
    17. var reply string
    18. if err = websocket.Message.Receive(ws, &reply); err != nil {
    19. fmt.Println("Can’t receive")
    20. break
    21. }
    22. fmt.Println("Received back from client: ", reply)
    23. msg := "Received: " + reply
    24. fmt.Println("Sending to client" + msg)
    25. if err = websocket.Message.Send(ws, msg); err != nil {
    26. fmt.Println("Can’t send")
    27. break
    28. }
    29. }
    30. }