How enkeyz/websocket works
Project Type and Purpose
• Type: A Go-language library implementing RFC-6455 (WebSocket protocol).
• Purpose: Provide API primitives (Dialer, Upgrader, Conn, etc.) to establish, read from, and write to WebSocket connections, with full support for masking, fragmentation, compression, and control frames.
File Structure
• Top‐level Go source files define core functionality:
– client.go, server.go: APIs for client and server side.
– conn.go / conn_*.go: Core Conn struct, read/write loops, and broadcast support.
– compression.go: permessage-deflate compression negotiation and implementation.
– mask.go / mask_safe.go: Masking/unmasking of client-to-server payloads.
– prepared.go: PreparedMessage support for efficient frame reuse.
– proxy.go / x_net_proxy.go: Optional proxy support.
– tls_handshake.go: TLS handshake wrappers.
– json.go, util.go, join.go: Helpers for JSON messaging and utility functions.
• examples/: sample applications (chat, echo, command, filewatch, Autobahn test client), showing how consumers embed the library.
README Insights
• Confirms compliance with Autobahn Test Suite (via examples/autobahn).
• Emphasizes stable API, full protocol coverage, and use cases (chat, echo, filewatch, etc.).
Main Components and Their Relationships
A. Application Layer
– User application code (examples/chat/main.go, examples/echo/server.go, etc.)
– Calls into the library’s public APIs: Dialer (client) or Upgrader (HTTP server).
B. API Layer
– Dialer: establishes outbound WebSocket connections over TCP/TLS.
– Upgrader: performs HTTP handshake to upgrade inbound HTTP requests to WebSockets.
C. Connection Layer (Conn abstraction)
– Conn struct: maintains the underlying net.Conn, read/write pumps, close/error handling, broadcasting.
– PreparedMessage: optional pre-encoded frames for perf.
D. Frame Processing Subsystem
– Frame Encoder/Decoder: splits and merges messages into frames, handles fragmentation.
– Masking Module: applies or removes client-side masking (mask.go).
– Compression Module: negotiates and applies permessage-deflate (compression.go).
E. Transport & Protocol Layer
– Underlying net.Conn (TCP).
– Optional TLS handshake (tls_handshake.go).
– Optional Proxy layer (proxy.go, x_net_proxy.go).
F. External Dependencies & Tests
– HTTP package (net/http) for server side.
– Autobahn Test Suite (examples/autobahn) for protocol compliance.
– CircleCI and Go modules (go.mod) for build/test automation.
• From Application → Dialer/Upgrader
• Dialer/Upgrader → Conn constructor
• Conn ↔ Frame Processing (bidirectional)
• Frame Processing → Masking & Compression (bidirectional)
• Conn → net.Conn (TCP/TLS)
• Tests/Examples harness hooking into API Layer
Architectural Patterns & Design Principles
• Layered design: clear separation of API, connection management, frame processing, and transport.
• Pluggable modules: masking, compression, and proxy code live in separate files for ease of maintenance and replacement.
• Concurrency via Go routines: read/write pumps inside Conn.
• Interface‐based extensibility: net.Conn and HTTP Handler.