Once we run the program, the WebSocket server will start locally listening on port 8080.
Let’s understand the program we have written:
- 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.
- 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.
- Using var broadcast = make(chan Message), we created a channel where all the received messages are written.
- Next, we defined a HandleClients handler, which upon receiving the HTTP GET ...