Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active September 6, 2022 17:27
Show Gist options
  • Save percybolmer/8c4c5cc2021f5e4fb11a60e7bd87b9db to your computer and use it in GitHub Desktop.
Save percybolmer/8c4c5cc2021f5e4fb11a60e7bd87b9db to your computer and use it in GitHub Desktop.
Websockets with remove/add clients
package main
import (
"log"
"net/http"
"sync"
"github.com/gorilla/websocket"
)
var (
/**
websocketUpgrader is used to upgrade incomming HTTP requests into a persitent websocket connection
*/
websocketUpgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
// Manager is used to hold references to all Clients Registered, and Broadcasting etc
type Manager struct {
clients ClientList
// Using a syncMutex here to be able to lcok state before editing clients
// Could also use Channels to block
sync.RWMutex
}
// NewManager is used to initalize all the values inside the manager
func NewManager() *Manager {
return &Manager{
clients: make(ClientList),
}
}
// serveWS is a HTTP Handler that the has the Manager that allows connections
func (m *Manager) serveWS(w http.ResponseWriter, r *http.Request) {
log.Println("New connection")
// Begin by upgrading the HTTP request
conn, err := websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
// Create New Client
client := NewClient(conn, m)
// Add the newly created client to the manager
m.addClient(client)
}
// addClient will add clients to our clientList
func (m *Manager) addClient(client *Client) {
// Lock so we can manipulate
m.Lock()
defer m.Unlock()
// Add Client
m.clients[client] = true
}
// removeClient will remove the client and clean up
func (m *Manager) removeClient(client *Client) {
m.Lock()
defer m.Unlock()
// Check if Client exists, then delete it
if _, ok := m.clients[client]; ok {
// close connection
client.connection.Close()
// remove
delete(m.clients, client)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment