websocket-client-go

command module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2025 License: MIT Imports: 3 Imported by: 0

README

websocket-client-go

websocket-client-go is a simple wrapper library for Go's x/net/websocket.

Installation

go get github.com/shinosaki/websocket-client-go

Usage

example.go

package main

import (
	"log"
	"time"

	"github.com/shinosaki/websocket-client-go/websocket"
)

const (
	WEBSOCKET_URL = "wss://echo.websocket.org"
	ATTEMPTS      = 3
	INTERVAL      = 2
)

func main() {
	ws := websocket.NewWebSocketClient(
		// onOpen
		func(ws *websocket.WebSocketClient) {
			log.Println("Connected")
		},

		// onClose
		func(ws *websocket.WebSocketClient, isReconnecting bool) {
			if isReconnecting {
				log.Println("Reconnecting...")
			} else {
				log.Println("Disconnected")
			}
		},

		// onMessage
		func(ws *websocket.WebSocketClient, payload []byte) {
			log.Println("Received message:", string(payload))
		},
	)

	// Connect to server
	if err := ws.Connect(WEBSOCKET_URL, ATTEMPTS, INTERVAL); err != nil {
		log.Println("Failed to connect:", err)
	}

	sendMessage := func() {
		data := map[string]string{"message": "Hello WebSocket"}
		if err := ws.SendJSON(data); err != nil {
			log.Println("Failed to send message:", err)
		}
	}

	// Send a message
	sendMessage()
	time.Sleep(2 * time.Second)

	// Reconnecting
	ws.Reconnect(WEBSOCKET_URL, ATTEMPTS, INTERVAL)
	sendMessage()
	time.Sleep(2 * time.Second)

	ws.Disconnect(false)

	log.Println("Done")
}

License

MIT

Author

shinosaki

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL