How it works...

Once we run the program, the WebSocket server will start locally listening on port 8080.

Let’s understand the program we have written:

  1. We used import ("log" "net/http" "github.com/gorilla/websocket") which is a preprocessor command that tells the Go compiler to include all files from the log, net/http, and github.com/gorilla/websocket packages.
  2. Using var clients = make(map[*websocket.Conn]bool), we created a map that represents the clients connected to a WebSocket server with KeyType as a WebSocket connection object and ValueType as Boolean.
  3. Using var broadcast = make(chan Message), we created a channel where all the received messages are written.
  4. Next, we defined a HandleClients handler, which upon receiving the HTTP GET ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.