github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/golang.org/x/net/websocket/exampledial_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  	"fmt"
     9  	"log"
    10  
    11  	"golang.org/x/net/websocket"
    12  )
    13  
    14  // This example demonstrates a trivial client.
    15  func ExampleDial() {
    16  	origin := "http://localhost/"
    17  	url := "ws://localhost:12345/ws"
    18  	ws, err := websocket.Dial(url, "", origin)
    19  	if err != nil {
    20  		log.Fatal(err)
    21  	}
    22  	if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
    23  		log.Fatal(err)
    24  	}
    25  	var msg = make([]byte, 512)
    26  	var n int
    27  	if n, err = ws.Read(msg); err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	fmt.Printf("Received: %s.\n", msg[:n])
    31  }