exampledial_test.go 681 B

12345678910111213141516171819202122232425262728293031
  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. package websocket_test
  5. import (
  6. "fmt"
  7. "log"
  8. "golang.org/x/net/websocket"
  9. )
  10. // This example demonstrates a trivial client.
  11. func ExampleDial() {
  12. origin := "http://localhost/"
  13. url := "ws://localhost:12345/ws"
  14. ws, err := websocket.Dial(url, "", origin)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
  19. log.Fatal(err)
  20. }
  21. var msg = make([]byte, 512)
  22. var n int
  23. if n, err = ws.Read(msg); err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Printf("Received: %s.\n", msg[:n])
  27. }