Creating a WebSocket client
This subsection shows how to program a WebSocket client in Go. The client reads user data, sends it to the server, and reads the server response. The client
directory contains the implementation of the WebSocket client. The gorilla/websocket
package is going to help us develop the WebSocket client.
The code of ./client/client.go
is as follows:
package main
import (
"bufio"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/websocket"
)
var (
SERVER = ""
PATH = ""
TIMESWAIT = 0
TIMESWAITMAX = 5
in = bufio.NewReader(os.Stdin)
)
The in
variable is just a shortcut for bufio.NewReader(os.Stdin)
.
func getInput(input chan string) {
result, err := in.ReadString('\n')
if err != nil {
log.Println(err)
...