sockjsclient

package module
v0.0.0-...-adfb8b4 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 16 Imported by: 0

README

sockjs-go-client

Optimized for STOMP Protocol...

Overview

sockjs-go-client is a client library optimized for use with the STOMP (Simple Text Oriented Messaging Protocol) over SockJS in Go applications. It provides a convenient and efficient way to establish WebSocket and XHR connections with a SockJS server, specifically tailored for use with the STOMP protocol.

Installation

go get -u github.com/eminaktas/sockjs-go-client

Usage

Here's a quick example demonstrating how to create and use a SockJS connection with STOMP protocol.

For the server, you can use the sockjs-stomp-go-server project. Refer to the example provided here for a server-side implementation.

package main

import (
 "log"
 "net/http"
 "time"

 sockjsclient "github.com/eminaktas/sockjs-go-client"
 "github.com/go-stomp/stomp/v3"
)

func main() {
 // Define SockJS server address
 serverAddress := "http://localhost:8085/connect"

 // Create a new SockJS client instance
 headers := make(http.Header)
 jar := new(http.CookieJar)
 sockJSClient, err := sockjsclient.NewClient(serverAddress, headers, *jar)
 if err != nil {
  log.Println("Error creating SockJS client:", err)
  return
 }

 log.Println("SockJS connection successful")

 // Define STOMP connection options
 stompHost := "default"
 stompSendTimeout := 1000 * time.Millisecond
 stompRecvTimeout := 1000 * time.Millisecond

 var stompOptions []func(*stomp.Conn) error = []func(*stomp.Conn) error{
  stomp.ConnOpt.Host(stompHost),                               // Set the host for the STOMP connection
  stomp.ConnOpt.HeartBeat(stompSendTimeout, stompRecvTimeout), // Configure heartbeats
 }

 // Connect to the STOMP server using SockJS as the underlying transport
 stompConnection, err := stomp.Connect(sockJSClient, stompOptions...)
 if err != nil {
  log.Println("Error creating STOMP connection:", err.Error())
  return
 }
 defer func() {
  log.Println("Disconnecting from STOMP server...")
  stompConnection.Disconnect()
 }()

 log.Println("STOMP connection successful")

 // Send a message to the specified destination
 sendDestination := "/echo/"
 messageBody := "an example message"
 log.Printf("Sending message to '%s': %s\n", sendDestination, messageBody)
 _ = stompConnection.Send(sendDestination, "text/plain", []byte(messageBody))

 // Subscribe to a destination on the STOMP server
 subscribeDestination := "/topic"
 subscription, err := stompConnection.Subscribe(subscribeDestination, stomp.AckAuto)
 if err != nil {
  log.Printf("Subscription failed: %v\n", err)
  return
 }

 // Wait for the response from the subscribed destination
 receivedMessage := <-subscription.C
 if receivedMessage.Err != nil {
  log.Printf("message recieve failed: %v\n", receivedMessage.Err)
  return
 }
 log.Printf("Received Message from '%s': %s\n", subscribeDestination, receivedMessage.Body)

 // Unsubscribe after message receieved.
 subscription.Unsubscribe()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Connection Connection

	WebSockets   bool
	Address      string
	ReadBufSize  int
	WriteBufSize int

	Reconnected    chan struct{}
	RequestHeaders http.Header
	Jar            http.CookieJar
}

func NewClient

func NewClient(address string, headers http.Header, jar http.CookieJar) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) ForceClose

func (c *Client) ForceClose()

func (*Client) Info

func (c *Client) Info() (*Info, error)

func (*Client) Read

func (c *Client) Read(p []byte) (int, error)

func (*Client) Write

func (c *Client) Write(p []byte) (int, error)

type Connection

type Connection interface {
	Read([]byte) (int, error)
	Write([]byte) (int, error)
	Close() error
	ForceClose()
}

type Info

type Info struct {
	WebSocket    bool     `json:"websocket"`
	CookieNeeded bool     `json:"cookie_needed"`
	Origins      []string `json:"origins"`
	Entropy      int      `json:"entropy"`
}

type WebSocket

type WebSocket struct {
	Address          string
	TransportAddress string
	ServerID         string
	SessionID        string
	Connection       *websocket.Conn
	Inbound          chan []byte
	Reconnected      chan struct{}
	RequestHeaders   http.Header
	Jar              http.CookieJar
}

WebSocket represents a SockJS WebSocket connection.

func NewWebSocket

func NewWebSocket(address string, headers http.Header, jar http.CookieJar) (*WebSocket, error)

NewWebSocket creates a new WebSocket instance.

func (*WebSocket) Close

func (w *WebSocket) Close() error

Close closes the WebSocket connection.

func (*WebSocket) ForceClose

func (w *WebSocket) ForceClose()

func (*WebSocket) Loop

func (w *WebSocket) Loop()

func (*WebSocket) Read

func (w *WebSocket) Read(v []byte) (int, error)

Read reads a message from the WebSocket and unquotes it.

func (*WebSocket) Write

func (w *WebSocket) Write(v []byte) (int, error)

Write writes a message to the WebSocket after formatting and escaping it.

type XHR

type XHR struct {
	Address          string
	TransportAddress string
	ServerID         string
	SessionID        string
	Inbound          chan []byte
	Done             chan bool
	// contains filtered or unexported fields
}

func NewXHR

func NewXHR(address string) (*XHR, error)

func (*XHR) Close

func (x *XHR) Close() error

func (*XHR) ForceClose

func (x *XHR) ForceClose()

No need for ForceClose.

func (*XHR) GetSessionState

func (x *XHR) GetSessionState() sockjs.SessionState

func (*XHR) Init

func (x *XHR) Init() error

func (*XHR) Read

func (x *XHR) Read(v []byte) (int, error)

func (*XHR) StartReading

func (x *XHR) StartReading()

func (*XHR) Write

func (x *XHR) Write(v []byte) (int, error)

Jump to

Keyboard shortcuts

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