gows

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

Gows

Gorilla websocket based simplified client implementation.

This project is a fork of GoWebsocket library.

Why Forked?

The project is dead, so we forked it and updated it.

API Reference

Install

go get github.com/Sock-Net/gows

Usage

For this test, get a demo websocket at: https://www.piesocket.com/websocket-tester and copy the url.

package main

import (
	"log"
	"time"

	"github.com/Sock-Net/gows"
)

var SOCKET *gows.Socket

func main() {
	SOCKET = gows.New("url", true)
	SOCKET.SetReconnect(time.Duration(5) * time.Second)

	SOCKET.OnConnected = func() {
		log.Println("Connected to server")

		// Send JSON response
		SOCKET.SendJSON(map[string]interface{}{
			"val": 5,
			"data": "test",
		})
	}

	SOCKET.OnConnectError = func(err error) {
		log.Println("Recieved connect error ", err)
	}

	SOCKET.OnTextMessage = func(message string) {
		log.Println("Recieved message " + message)
	}

	SOCKET.OnBinaryMessage = func(data []byte) {
		log.Println("Recieved binary data ", data)
	}

	SOCKET.OnPingReceived = func(data string) {
		log.Println("Recieved ping " + data)
	}

	SOCKET.OnPongReceived = func(data string) {
		log.Println("Recieved pong " + data)
	}

	SOCKET.OnDisconnected = func(err error) {
		log.Println("Disconnected from server")
	}

	SOCKET.ConnectAndWait()
	log.Println("Interrupt")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildProxy

func BuildProxy(Url string) func(*http.Request) (*url.URL, error)

Types

type ConnectionOptions

type ConnectionOptions struct {
	UseCompression bool
	UseSSL         bool
	Proxy          func(*http.Request) (*url.URL, error)
	Subprotocols   []string
}

Struct for connection options

type ReconnectOptions added in v0.2.0

type ReconnectOptions struct {
	Enabled bool
	Delay   time.Duration
}

Struct for reconnect options

type Socket

type Socket struct {
	IsConnected       bool
	Url               string
	Conn              *websocket.Conn
	WebsocketDialer   *websocket.Dialer
	ConnectionOptions *ConnectionOptions
	RequestHeader     http.Header
	Timeout           time.Duration

	ReconnectOptions *ReconnectOptions
	OnConnected      func()
	OnTextMessage    func(message string)
	OnBinaryMessage  func(data []byte)
	OnConnectError   func(err error)
	OnDisconnected   func(err error)
	OnPingReceived   func(data string)
	OnPongReceived   func(data string)
	// contains filtered or unexported fields
}

Socket struct

func New

func New(url string, ssl bool) *Socket

Create new websocket client

func (*Socket) Close

func (socket *Socket) Close() error

Close websocket

func (*Socket) Connect

func (socket *Socket) Connect() error

Connect websocket and setup handlers

func (*Socket) ConnectAndWait added in v0.2.0

func (socket *Socket) ConnectAndWait() error

Connect websocket, setup handlers and wait for interrupt

func (*Socket) SendBinary

func (socket *Socket) SendBinary(data []byte) error

Send binary to websocket

func (*Socket) SendJSON

func (socket *Socket) SendJSON(data interface{}) error

Send JSON to websocket

func (*Socket) SendText

func (socket *Socket) SendText(message string) error

Send text to websocket

func (*Socket) SetReconnect added in v0.2.0

func (socket *Socket) SetReconnect(delay time.Duration)

Jump to

Keyboard shortcuts

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