Categorygithub.com/albeebe/websocket
modulepackage
1.0.2
Repository: https://github.com/albeebe/websocket.git
Documentation: pkg.go.dev

# README

WebSocket Package

This package provides a wrapper around the gorilla/websocket package to simplify WebSocket client usage. It offers a more intuitive interface for managing WebSocket connections by leveraging channels for sending and receiving messages, as well as context for handling lifecycle and cancellation.

Features

  • Channel-based message handling for sending and receiving messages, providing a more intuitive and thread-safe interface
  • Graceful shutdown and resource cleanup through context-based cancellation

Installation

go get github.com/albeebe/websocket

Example Usage

package main

import (
   "fmt"
   "time"

   "github.com/albeebe/websocket"
)

func main() {
   // Create a new WebSocket client
   url := "wss://example.com/socket"
   client := websocket.NewWebSocketClient(url)

   // Monitor WebSocket client state changes and process incoming messages
   go func() {
      for {
         select {
         case <-client.Connected:
            fmt.Println("WebSocket client connected successfully!")

         case err := <-client.Disconnected:
            if err != nil {
               fmt.Println("WebSocket client disconnected with error:", err)
            } else {
               fmt.Println("WebSocket client disconnected")
            }
            return // Exit the loop and terminate the Goroutine after disconnection

         case msg := <-client.IncomingMessages:
            switch msg.Type {
            case websocket.TextMessage:
               fmt.Println("Received text message:", string(msg.Data))
            case websocket.BinaryMessage:
               fmt.Println("Received binary message of length:", len(msg.Data))
            case websocket.PingMessage:
               fmt.Println("Received ping message")
            case websocket.PongMessage:
               fmt.Println("Received pong message")
            }
         }
      }
   }()

   // Send a message to the server
   messageData := []byte("Hello WebSocket Server!")
   client.SendMessage(websocket.TextMessage, messageData)

   // Keep the client running to receive messages
   time.Sleep(10 * time.Second)

   // Close the WebSocket connection
   client.Close()
}

# Functions

NewWebSocketClient initializes a new WebSocket client and connects to the provided URL.

# Constants

Binary message (e.g., images, files).
Close connection request.
Ping to check connection.
Pong response to a ping.
Text message (e.g., JSON, plain text).

# Structs

Message represents a message sent or received over a WebSocket connection.
WebSocket manages the lifecycle and communication of a WebSocket connection.

# Type aliases

Define constants for different message types.