In this recipe, we will learn how to write a WebSocket server, which is a TCP application listening on port 8080 that allows connected clients to send messages to each other.
Creating your first WebSocket server
How to do it...
- Install the github.com/gorilla/websocket package using the go get command, as follows:
$ go get github.com/gorilla/websocket
- Create websocket-server.go where we will upgrade an HTTP request to WebSocket, read the JSON message from the client, and broadcast it to all of the connected clients, as follows:
package main
import
(
"log"
"net/http"
"github.com/gorilla/websocket"
)
var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan Message)
var upgrader...