dnet

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: MIT Imports: 14 Imported by: 0

README

dnet

Simply take your websocket utilization to large scale level. SIMPLE, MINIMAL & POWERFUL!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthLink(link string) (userID, linkID string, status int)

AuthLink validates and authenticates verification link.

link: represents the link from the client generated by the Link () function. It returns user ID if succeeds.

status: indicates the results of the link validation. Where,

0 means link expired

1 stands for bad request i.e client side has sent the wrong link

2 means everything is fine! and with this status you can get the user ID.

func Connect

func Connect(w http.ResponseWriter, r *http.Request, allowedOrigin ...string)

Connect inits connection in the given endpoint

func GetIP added in v1.0.68

func GetIP(r *http.Request) (ip string, err error)

GetIP returns the IP address from the client request

func Init

func Init(options ...Options)

Init initializes dnet hub monitoring. You should pass your configurations in this function if you are using v1.0.208-beta and above. Otherwise you should stick with Config() function

It should be called only once

func IsCtxt added in v1.2.0

func IsCtxt(v any) bool

IsCtx tests the given value v if it's of Ctx type

func IsDnetContext added in v1.3.0

func IsDnetContext(v any) bool

IsDnetContext tests the given value v if it's a dnet context i.e dnet.EContext or dnet.Ctx

func IsEContext added in v1.2.0

func IsEContext(v any) bool

IsEContext tests the given value v if it's an external context - EContext

func LastSeen

func LastSeen(handler ActionHandler)

LastSeen is called when the authenticated user gets offline or logs out It's very useful for setting the last seen of the user connection

func NewLink(userID, linkID string, minutes ...time.Duration) (link string, err error)

NewLink generates a link to use for user verification.

ID is a Unique identifier stored in the link througth aes encryption.

minutes is a life time of the link in minutes i.e after that amount of time the link becomes expired. Default time is 60 minutes

func On

func On(action string, handlers ...ActionHandler)

On method takes ActionHandlers to be called when the given action fired by the dnet-client

func SendTicket

func SendTicket(r *http.Request, w http.ResponseWriter, ID string)

SendTicket sends an encrypted ticket to the user.

func Use

func Use(handlers ...ActionHandler)

Use adds root-level middlewares which will be called before any action is matched.

Types

type ActionHandler

type ActionHandler func(*Ctx)

ActionHandler is a function wich receives Ctx

type Ctx added in v1.0.71

type Ctx struct {

	//ID of the user owning the connection
	ID string

	// Rec is an id of the recipient
	Rec string

	// Authed  tells if the connection is authenticated or not
	Authed bool
	// IP address of the connection
	IP string
	// contains filtered or unexported fields
}

Ctx is a middleman between the websocket connection and the Hub. Ctx is stored in the dnet hub and hence it is an inside and persistent context.

func ToCtx added in v1.3.0

func ToCtx(v any) (ctx *Ctx, ok bool)

ToCtx converts the given value v to *dnet.Ctx.

ok is returned when the assertion succeed

func (*Ctx) All added in v1.0.71

func (c *Ctx) All(statusAndData ...interface{})

All sends to anyone connected to the websocket Dnet instance including the sender of the message

func (*Ctx) AuthTicket added in v1.0.71

func (c *Ctx) AuthTicket(infoText ...string) (ID string, ok bool)

AuthTicket authenticates the ticket and returns the user ID provided in the SendTicket().

ok is also returned to inform you if user authentication fails or not. If failed you, all you can do is simply "return" to finish your reponse cycle since AuthTicket helps you to respond to the client with the infoText you provide or the default infoText

Infotext is a message to send back to the client when user authentication fails. It defaults to "Please login to access this resource" if you do not provide it.

func (*Ctx) Bind added in v1.5.0

func (c *Ctx) Bind(v interface{}) (err error)

Bind extracts raw data, stores it in the passed pointer v and returns any raised error

func (*Ctx) Binder added in v1.0.71

func (c *Ctx) Binder(v interface{}, msg ...string) (ok bool)

Binder extracts raw data, stores it in the passed pointer v and implicitly sends 422 Unprocessable entities to the client when it's unable to decode data. You can provide a custom client error message by passing it as msg argument. In addition to sending a client friendly error to the client it also logs a raised error in console

It returns true on success and false on failure. You can use this value to stop the execution in the caller function by just using a return statement.

Use Bind(v interface{}) if you want to explicitly handle errors

func (*Ctx) Broadcast added in v1.0.71

func (c *Ctx) Broadcast(statusAndData ...interface{})

Broadcast sends data to all execept the sender

func (*Ctx) Clear added in v1.4.1

func (c *Ctx) Clear()

Clear empties context values. That is, deletes every value in the context values and resets the value store anew.

func (*Ctx) CreateRoom added in v1.0.71

func (c *Ctx) CreateRoom(roomID string, usersIDS ...string)

CreateRoom is for creating a new room.... if it finds a room exist it only adds the given the room

func (*Ctx) Del added in v1.4.1

func (c *Ctx) Del(key string)

Del deletes context value's field with a given keyc

func (*Ctx) Dispose added in v1.0.71

func (c *Ctx) Dispose()

Dispose discards the client connection without calling LastSeen for saving any lastSeen info for the clinet connection Useful for expired unauthorized client connections

func (*Ctx) Fire added in v1.0.71

func (c *Ctx) Fire(action string)

Fire sets which action to fire to the client. It's recommended to keep the action in path form to maintain maintain uniformity, If you do not set the action to fire, the action you listened for it will be fired backward to the client too.

func (*Ctx) Get added in v1.0.71

func (c *Ctx) Get(key string) (val interface{}, err error)

Get gets data stored in the connection

func (*Ctx) Logout added in v1.0.71

func (c *Ctx) Logout()

Logout calls the LastSeen function to ensure user last seen data is saved before discarding the client connection

func (*Ctx) MarkAuthed added in v1.1.0

func (c *Ctx) MarkAuthed(ID string)

MarkAuthed marks this connection as authenticated. Hence, no ticket authentication required.

Use this when you have your own authentication mechanism or just for testing purposes.

func (*Ctx) Multicast added in v1.0.71

func (c *Ctx) Multicast(userIDs []string, statusAndData ...interface{})

Multicast sends to the given users IDs (useful for sharing something to multiple users

func (*Ctx) Next added in v1.0.71

func (c *Ctx) Next()

Next pushes the next middleware in the list

func (*Ctx) Refire added in v1.5.0

func (c *Ctx) Refire()

Refire resets action to the initial action before calling any Fire("/action") method. In fact, the action becomes the same as it was fired by the client.

func (*Ctx) RoomAll added in v1.0.71

func (c *Ctx) RoomAll(ID string, statusAndData ...interface{})

RoomAll sends to the members of the room. (useful for chat rooms.. and sending data to all people under the same role or cartegory )

func (*Ctx) RoomBroadcast added in v1.0.71

func (c *Ctx) RoomBroadcast(ID string, statusAndData ...interface{})

RoomBroadcast sends to all members of the registered room except the sender

func (*Ctx) Rooms added in v1.0.71

func (c *Ctx) Rooms(roomsIDs ...string)

Rooms assigns this connnection to the chatrooms it relates to

func (*Ctx) Send added in v1.0.71

func (c *Ctx) Send(ID string, statusAndData ...interface{})

Send sends to one client only

func (*Ctx) SendBack added in v1.0.71

func (c *Ctx) SendBack(statusAndData ...interface{})

SendBack sends back to the sender's sending connection only.

Use sendMe() if you want to send to all of the sender's connections including the sending connection.

func (*Ctx) SendMe added in v1.1.0

func (c *Ctx) SendMe(statusAndData ...interface{})

SendMe sends to all of the sender's open connections

func (*Ctx) Set added in v1.0.71

func (c *Ctx) Set(key string, val interface{})

Set stores value in the connection.

type DnetContext added in v1.4.0

type DnetContext interface {
	*Ctx | *EContext
}

type EContext added in v1.0.71

type EContext struct {

	//ID is  a user id to assocaite it with the user connection
	ID string
	// contains filtered or unexported fields
}

EContext is an external context which provides functionalities for sending data to the websocket's connections from a normal http connection. It interfaces the dnet hub and dnet sending functionalities to the normal http connection. EContext is not stored inside the dnet hub and hence it is called external and immidiate context.

func Context

func Context(action string, userID ...string) EContext

Context creates an external context for sending data to the websocket connections from a normal http connection.

func ToEContext added in v1.3.0

func ToEContext(v any) (ctx *EContext, ok bool)

ToEContext converts the given value v to *dnet.EContext.

ok is returned when the assertion succeed

func (*EContext) Broadcast added in v1.0.71

func (c *EContext) Broadcast(statusAndData ...interface{})

Broadcast sends data to alll dnet contexts in the hub

func (*EContext) Fire added in v1.0.71

func (c *EContext) Fire(action string)

Fire sets which action to fire to the client. It's recommended to keep the action in path form to maintain maintain uniformity, If you do not set the action to fire, the action you listened for it will be fired backward to the client too.

func (*EContext) Multicast added in v1.0.71

func (c *EContext) Multicast(userIDs []string, statusAndData ...interface{})

Multicast sends to the given users IDs (useful for sharing something to multiple users

func (*EContext) Refire added in v1.5.0

func (c *EContext) Refire()

Refire resets action to the initial action before calling any Fire("/action") method.

func (*EContext) Send added in v1.0.71

func (c *EContext) Send(ID string, statusAndData ...interface{})

Send sends to only one client of the specified ID

type Hub

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

Hub maintains and manages dnet contexts

func (*Hub) Run

func (h *Hub) Run()

Run method is for starting the Hub

type MainRouter

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

MainRouter routes websocket actions

func (*MainRouter) Route

func (r *MainRouter) Route(IncomingAction string, context *Ctx)

Route performs routing websocket actions based on the incoming action

type Map

type Map map[string]interface{}

Map models json data to be sent to the client by creating a map[string]interface{}

func Msg

func Msg(msg string) (simpleMsgResponse Map)

Msg models the msg to a simple msg:string map

type Options

type Options struct {
	TicketAge time.Duration
	MaxSize   int64
}

Options is used to take all the

type Subrouter added in v1.1.0

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

Subrouter is for grouping actions, and creating middleware subjected to the particular group

func Router

func Router(path string) Subrouter

Router creates a subrouter for grouping related actions.

func (Subrouter) On added in v1.1.0

func (r Subrouter) On(action string, handlers ...ActionHandler)

On method is adding Event handlders to the router by prefixing it with the Matcher path

func (*Subrouter) Router added in v1.1.0

func (r *Subrouter) Router(path string) Subrouter

func (*Subrouter) Use added in v1.1.0

func (r *Subrouter) Use(handlers ...ActionHandler)

Use adds middlewares to the subrouter which will be called before any other subrouter actions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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