Creating a WebSocket client
This subsection shows how to program a WebSocket client in Go. The client reads user data that sends it to the server and reads the server response. The client
directory of https://github.com/mactsouk/ws contains the implementation of the WebSocket client—I find it more convenient to include both implementations in the same repository.
As with the WebSocket server, the gorilla/websocket
package is going to help us develop the WebSocket client.
We are going to see gorilla
in the next chapter when working with RESTful services.
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 = ""
var PATH = ""
var TIMESWAIT = 0
var TIMESWAITMAX = 5
var in = bufio.NewReader...