demowsserver

package
v0.0.0-...-335aeae Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

This package contains the implementation of a simple websocket server with the following features:

  • subscribe/unsubcribe to a publication
  • publish heartbeats from server to client who subscribed the publciation on a regular basis
  • echo with optional user provided request ID. User can also request an error to be returned
  • interrupt/close client connections

This package contains the implementation of a simple websocket server with the following features:

  • subscribe/unsubcribe to a publication
  • publish heartbeats from server to client who subscribed the publciation on a regular basis
  • echo with optional user provided request ID. User can also request an error to be returned
  • interrupt/close client connections

This package contains the implementation of a simple websocket server with the following features:

  • subscribe/unsubcribe to a publication
  • publish heartbeats from server to client who subscribed the publciation on a regular basis
  • echo with optional user provided request ID. User can also request an error to be returned
  • interrupt/close client connections

Index

Constants

View Source
const (
	// Message type: error
	MSG_TYPE_ERROR = "error"
	// Message type: subscribe - request
	MSG_TYPE_SUBSCRIBE_REQUEST = "subscribe_request"
	// Message type: subscribe - response
	MSG_TYPE_SUBSCRIBE_RESPONSE = "subscribe_response"
	// Message type: unsubscribe - request
	MSG_TYPE_UNSUBSCRIBE_REQUEST = "unsubscribe_request"
	// Message type: unsubscribe - response
	MSG_TYPE_UNSUBSCRIBE_RESPONSE = "unsubscribe_response"
	// Message type: echo - request
	MSG_TYPE_ECHO_REQUEST = "echo_request"
	// Message type: echo - response
	MSG_TYPE_ECHO_RESPONSE = "echo_response"
	// Message type: heartbeat
	MSG_TYPE_HEARTBEAT = "heartbeat"
	// Topic for heartbeats
	TOPIC_HEARTBEAT = "heartbeat"
	// Response status OK
	RESPONSE_STATUS_OK = "OK"
	// Response status for an error
	RESPONSE_STATUS_ERROR = "ERROR"
	// Error code used when client has already subscribed to a topic
	ERROR_ALREADY_SUBSCRIBED = "ALREADY_SUBSCRIBED"
	// Error code used when client has not subscribed to a topic and asks to unsubscribe
	ERROR_NOT_SUBSCRIBED = "NOT_SUBSCRIBED"
	// Error code used when client ask echo to return an error
	ERROR_ECHO_ASKED_BY_CLIENT = "ASKED_BY_CLIENT"
	// Error code used when topic asked by client is not known
	ERROR_TOPIC_NOT_FOUND = "TOPIC_NOT_FOUND"
	// Error returned when a message could not be handled by the server (either unkown message type or message type is not there)
	ERROR_UNKNOWN_MESSAGE_TYPE = "UNKNOWN_MESSAGE_TYPE"
	// Error returned when a message could not be unmarshalled or is malformatted
	ERROR_BAD_REQUEST = "BAD_REQUEST"
)

Constants used in messages

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseMessage

type CloseMessage struct {
	// Close message reason code
	Code websocket.StatusCode
	// Close message reason
	Reason string
}

Data of a websocket close message

type DemoWebsocketServer

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

Structure for the websocket server

func NewDemoWebsocketServer

func NewDemoWebsocketServer(ctx context.Context, httpServer *http.Server, tracerProvider trace.TracerProvider, meterProvider metric.MeterProvider) (*DemoWebsocketServer, error)

Description

Factory which creates a new, non-started DemoWebsocketServer.

Inputs

  • ctx: Parent context to use as root context. All subcontextes will derive from this context and will be bound to this context lifecycle.
  • httpServer: The underlying HTTP Server to use. The provided HTTP Server handler will be overriden with this server handler. If nil is provided, a default HTTP server listening on localhost:8080 will be used.
  • tracerProvider: Tracer provider to use to get the tracer that will be used by the server. If nil, the global tracer provider will be used.
  • meterProvider: Meter provider to use to get the meter that will be used by the server. If nil, the meter tracer provider will be used.

Returns

A new, non-started DemoWebsocketServer or an error if any has occured.

func (*DemoWebsocketServer) CloseClientConnections

func (srv *DemoWebsocketServer) CloseClientConnections(closeMessage CloseMessage)

Description

Close all client connections if server is started. Otherwise, it is a noop. A close message that includes the provided code and reason will be sent.

Inputs

  • closeMessage: Close message to send to clients before closing the connection on server side.

func (*DemoWebsocketServer) ServeHTTP

func (srv *DemoWebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

Description

Server handler which accepts incoming websocket connections.

func (*DemoWebsocketServer) Start

func (srv *DemoWebsocketServer) Start() error

Description

Start the websocket server that will accept incoming websocket connections.

func (*DemoWebsocketServer) Stop

func (srv *DemoWebsocketServer) Stop() error

Description

Gracefully shutdown the websocket server. The method exits either when the server has finished stopping or when a timeout or another error has occured.

Returns

Nil in case of success, an error otherwise.

type EchoRequest

type EchoRequest struct {
	Request
	// Message to be echoed by server
	Echo string `json:"echo"`
	// If true, the server will have to return an error instead of an echo
	Err bool `json:"err,omitempty"`
}

Echo request message

type EchoResponse

type EchoResponse struct {
	Response
	Data *EchoResponseData `json:"data,omitempty"`
}

EchoResponse message

type EchoResponseData

type EchoResponseData struct {
	// Echoed message
	Echo string `json:"echo"`
}

Data of a EchoResponse message

type ErrorMessage

type ErrorMessage struct {
	Message
	ErrorMessageData
}

Struct for a error message when no other response is applicable

type ErrorMessageData

type ErrorMessageData struct {
	// Error message
	Message string `json:"message"`
	// Optional error code
	Code string `json:"code,omitempty"`
	// User provided ID for its request
	ReqId string `json:"reqId,omitempty"`
}

Structure that contains data of an error returned by the websocket server

type Heartbeat

type Heartbeat struct {
	Message
	// Timestamp of the heartbeat publication
	Timestamp string `json:"timestamp"`
	// Connection uptime in seconds
	Uptime string `json:"uptime_seconds"`
}

Heartbeat publication

type Message

type Message struct {
	// Mandatory message type indicator
	MsgType string `json:"type"`
}

Base struct for a message exchanged between the websocket server & client

type Request

type Request struct {
	Message
	// User provided ID for the request. Used to match with response in a async. Request-Response pattern
	ReqId string `json:"reqId,omitempty"`
}

Base structure for a request message from client

type Response

type Response struct {
	Message
	// User provided ID for its request
	ReqId string `json:"reqId,omitempty"`
	// Status code -> OK or ERROR
	Status string `json:"status"`
	// Optional error -> must be there if status is ERROR
	Err *ErrorMessageData `json:"error,omitempty"`
	// Optional response data (can be absent even if status is OK)
	Data interface{} `json:"data,omitempty"`
}

Base structure for a response message from server

type SubscribeRequest

type SubscribeRequest struct {
	Request
	// Topic to subscribe to
	Topic string `json:"topic"`
}

Subscribe request message

type SubscribeResponse

type SubscribeResponse struct {
	Response
	// Optional SubscribeResponse data
	Data *SubscribeResponseData `json:"data,omitempty"`
}

Subscribe response message

type SubscribeResponseData

type SubscribeResponseData struct {
	// Topic client has subscribed to
	Topic string `json:"topic"`
}

Data of a SubscribeResponse

type UnsubscribeRequest

type UnsubscribeRequest struct {
	Request
	// Topic to unsubscribe to
	Topic string `json:"topic"`
}

Unsubscribe request message

type UnsubscribeResponse

type UnsubscribeResponse struct {
	Response
	// Optional UnsubscribeResponse data
	Data *UnsubscribeResponseData `json:"data,omitempty"`
}

Unsubscribe response message

type UnsubscribeResponseData

type UnsubscribeResponseData struct {
	// Topic client has unsubscribed to
	Topic string `json:"topic"`
}

Data of a UnsubscribeResponse

Jump to

Keyboard shortcuts

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