tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/net/webclient/main.go (about)

     1  // This example uses TCP to send an HTTP request to retrieve a webpage.  The
     2  // HTTP request is hand-rolled to avoid the overhead of using http.Get() from
     3  // the "net/http" package.  See example/net/http-get for the full http.Get()
     4  // functionality.
     5  //
     6  // Example HTTP server:
     7  // ---------------------------------------------------------------------------
     8  // package main
     9  //
    10  // import "net/http"
    11  //
    12  // func main() {
    13  //        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    14  //                w.Write([]byte("hello"))
    15  //        })
    16  //        http.ListenAndServe(":8080", nil)
    17  // }
    18  // ---------------------------------------------------------------------------
    19  
    20  //go:build ninafw || wioterminal
    21  
    22  package main
    23  
    24  import (
    25  	"bufio"
    26  	"fmt"
    27  	"io"
    28  	"log"
    29  	"machine"
    30  	"net"
    31  	"strings"
    32  	"time"
    33  
    34  	"tinygo.org/x/drivers/netlink"
    35  	"tinygo.org/x/drivers/netlink/probe"
    36  )
    37  
    38  var (
    39  	ssid string
    40  	pass string
    41  	// HTTP server address to hit with a GET / request
    42  	address string = "10.0.0.100:8080"
    43  )
    44  
    45  var conn net.Conn
    46  
    47  // Wait for user to open serial console
    48  func waitSerial() {
    49  	for !machine.Serial.DTR() {
    50  		time.Sleep(100 * time.Millisecond)
    51  	}
    52  }
    53  
    54  func dialConnection() {
    55  	var err error
    56  
    57  	println("\r\n---------------\r\nDialing TCP connection")
    58  	conn, err = net.Dial("tcp", address)
    59  	for ; err != nil; conn, err = net.Dial("tcp", address) {
    60  		println("Connection failed:", err.Error())
    61  		time.Sleep(5 * time.Second)
    62  	}
    63  	println("Connected!\r")
    64  }
    65  
    66  func check(err error) {
    67  	if err != nil {
    68  		println("Hit an error:", err.Error())
    69  		panic("BYE")
    70  	}
    71  }
    72  
    73  func makeRequest() {
    74  	println("Sending HTTP request...")
    75  	w := bufio.NewWriter(conn)
    76  	fmt.Fprintln(w, "GET / HTTP/1.1")
    77  	fmt.Fprintln(w, "Host:", strings.Split(address, ":")[0])
    78  	fmt.Fprintln(w, "User-Agent: TinyGo")
    79  	fmt.Fprintln(w, "Connection: close")
    80  	fmt.Fprintln(w)
    81  	check(w.Flush())
    82  	println("Sent!\r\n\r")
    83  }
    84  
    85  func readResponse() {
    86  	r := bufio.NewReader(conn)
    87  	resp, err := io.ReadAll(r)
    88  	check(err)
    89  	println(string(resp))
    90  }
    91  
    92  func closeConnection() {
    93  	conn.Close()
    94  }
    95  
    96  func main() {
    97  	waitSerial()
    98  
    99  	link, _ := probe.Probe()
   100  
   101  	err := link.NetConnect(&netlink.ConnectParams{
   102  		Ssid:       ssid,
   103  		Passphrase: pass,
   104  	})
   105  	if err != nil {
   106  		log.Fatal(err)
   107  	}
   108  
   109  	for i := 0; ; i++ {
   110  		dialConnection()
   111  		makeRequest()
   112  		readResponse()
   113  		closeConnection()
   114  		println("--------", i, "--------\r\n")
   115  		time.Sleep(10 * time.Second)
   116  	}
   117  }