wsstomp

package module
v0.0.0-...-3f6f996 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: MIT Imports: 5 Imported by: 0

README

ws-stomp is a simple wrapper over a websocket connection that allows it to play nice with go-stomp/stomp.

Example usage:

package main

import (
	"context"
	"log"
	"time"

	wsstomp "github.com/SoMuchForSubtlety/ws-stomp"
	"github.com/go-stomp/stomp/v3"
)

func main() {
	// timeout if the connection isn't established after ten seconds
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	// connect to websocket
	conn, err := wsstomp.Connect(ctx, "wss://test.com/ws", nil)
	cancel()
	if err != nil {
		log.Printf("error during WS connect: %v", err)
		return
	}

	// init STOMP connection using the websocket connections
	stompConn, err := stomp.Connect(conn)
	if err != nil {
		conn.Close()
		log.Printf("error during STOMP connect: %v", err)
		return
	}
	defer func() {
		err = stompConn.Disconnect()
		if err != nil {
			log.Printf("error during STOMP disconnect: %v", err)
		}
	}()

	// send a message
	err = stompConn.Send("/queue/a", "text/plain", []byte("hello world!"))
	if err != nil {
		log.Println(err)
		return
	}
}

Documentation

Overview

Example (EstablishConnection)

ExampleEstablishConnection demonstrates how to use this library to talk STOMP over a websocket connection

// timeout if the connection isn't established after ten seconds
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
// connect to websocket
conn, err := wsstomp.Connect(ctx, "wss://test.com/ws", nil)
cancel()
if err != nil {
	log.Printf("error during WS connect: %v", err)
	return
}

// init STOMP connection using the websocket connections
stompConn, err := stomp.Connect(conn)
if err != nil {
	conn.Close()
	log.Printf("error during STOMP connect: %v", err)
	return
}
defer func() {
	err = stompConn.Disconnect()
	if err != nil {
		log.Printf("error during STOMP disconnect: %v", err)
	}
}()

// send a message
err = stompConn.Send("/queue/a", "text/plain", []byte("hello world!"))
if err != nil {
	log.Println(err)
	return
}
Output:

Index

Examples

Constants

View Source
const (
	NullByte     = 0x00
	LineFeedByte = 0x0a
)

Variables

This section is empty.

Functions

This section is empty.

Types

type WebsocketSTOMP

type WebsocketSTOMP struct {
	// contains filtered or unexported fields
}

func Connect

func Connect(ctx context.Context, url string, options *websocket.DialOptions) (*WebsocketSTOMP, error)

Establish a websocket connection with the provided URL. The context parameter will only be used for the connection handshake, and not for the full lifetime of the connection.

func (*WebsocketSTOMP) Close

func (w *WebsocketSTOMP) Close() error

func (*WebsocketSTOMP) Read

func (w *WebsocketSTOMP) Read(p []byte) (int, error)

Read messages from the websocket connection until the provided array is full. Any surplus data is preserved for the next Read call

func (*WebsocketSTOMP) Write

func (w *WebsocketSTOMP) Write(p []byte) (int, error)

Write to the websocket.

The written data is held back until a full STOMP frame has been written, then a WS message is sent.

Jump to

Keyboard shortcuts

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