ws

package
v0.0.0-...-d0a0e58 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrExpired        = "Login expired, please login again."
	ErrForbidden      = "Forbidden."
	ErrInternalServer = "Internal server error."
	ErrUserNotFound   = "User doesn't exist."
	ErrMessageEmpty   = "Empty message."
)

Variables

This section is empty.

Functions

func CheckAuthority

func CheckAuthority(tk string, verify token.Verify, role ...string) (uid string, err error)

func ServeWs

func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request)

ServeWs handles websocket requests from the peer.

Types

type Channel

type Channel string
const (
	MessageError Channel = "message_error"
	// unsubscribe message
	Login     Channel = "login"
	Logout    Channel = "logout"
	ChatSend  Channel = "chat_send"
	ChatCheck Channel = "chat_check"
	// subscribe message
	Echo               Channel = "echo"
	ChatReceive        Channel = "chat_receive"
	ChatPartners       Channel = "chat_partners"
	ChatHistory        Channel = "chat_history"
	ChatPartnersStatus Channel = "chat_partners_status"
	ChatCheckedStatus  Channel = "chat_checked_status"
	ChatUncheckedList  Channel = "chat_unchecked_list"
	ChatUncheckedTotal Channel = "chat_unchecked_total"

	MaxWsConnsOneUser = 20 // max websocket connections one user
)

type ChannelHandler

type ChannelHandler struct {
	Handler Handler
	Auth    bool // Authorized to access
}

type ChannelSet

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

func NewChannelSet

func NewChannelSet(channels ...Channel) *ChannelSet

func (*ChannelSet) Add

func (s *ChannelSet) Add(channels ...Channel)

func (*ChannelSet) Clear

func (s *ChannelSet) Clear()

func (*ChannelSet) Contains

func (s *ChannelSet) Contains(channel Channel) bool

func (*ChannelSet) Map

func (s *ChannelSet) Map(f func(channel Channel))

func (*ChannelSet) Remove

func (s *ChannelSet) Remove(channels ...Channel)

func (*ChannelSet) Size

func (s *ChannelSet) Size() int

type CheckMessageReq

type CheckMessageReq struct {
	Partner string `json:"partner"`
}

CheckMessageReq is to check chat message request

type CheckedMessage

type CheckedMessage struct {
	Partner string `json:"partner"`
}

CheckedMessage is notify to client update checked status of partner's chat message

type Client

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

Client is a middleman between the websocket connection and the hub.

type ClientSet

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

func NewClientSet

func NewClientSet(clients ...*Client) *ClientSet

func (*ClientSet) Add

func (s *ClientSet) Add(clients ...*Client)

func (*ClientSet) Clear

func (s *ClientSet) Clear()

func (*ClientSet) Contains

func (s *ClientSet) Contains(client *Client) bool

func (*ClientSet) Map

func (s *ClientSet) Map(f func(cli *Client))

func (*ClientSet) Remove

func (s *ClientSet) Remove(clients ...*Client)

func (*ClientSet) Size

func (s *ClientSet) Size() int

type ErrorMessage

type ErrorMessage struct {
	Error  string `json:"error"`
	Source string `json:"source"`
}

type Handler

type Handler func(pkg *WrapPackageReq)

type HistoryMessageReq

type HistoryMessageReq struct {
	Partner string          `json:"partner" form:"partner" binding:"required"`
	Before  *types.UnixTime `json:"before,omitempty" form:"before"`
	Limit   int             `json:"limit" form:"limit" binding:"required,min=1,max=100" example:"20"`
}

HistoryMessageReq is history chat message request

type Hub

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

Hub maintains the set of active clients and broadcasts messages to the clients.

func NewHub

func NewHub(repo *repo.Manager) *Hub

func (*Hub) DeliverSystemMessage

func (h *Hub) DeliverSystemMessage(recipient string, content string) error

func (*Hub) RegisterHandler

func (h *Hub) RegisterHandler(channel Channel, handler Handler, auth bool)

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

type LoginReq

type LoginReq struct {
	Token string `json:"token"`
}

LoginReq is user login request

type LoginResp

type LoginResp struct {
	Msg string `json:"msg"`
}

LoginResp is user login response

type Message

type Message struct {
	Sender    string          `json:"sender,omitempty"`
	Recipient string          `json:"recipient"`
	Type      int8            `json:"type"` // 0:text message
	Content   string          `json:"content"`
	Timestamp *types.UnixTime `json:"timestamp,omitempty"`
	Checked   bool            `json:"checked"`
}

Message is chat message

type PackageReq

type PackageReq struct {
	Channel   Channel         `json:"channel"`
	Subscribe *bool           `json:"subscribe,omitempty"`
	Body      json.RawMessage `json:"body,omitempty"`
}

type PackageResp

type PackageResp struct {
	Channel Channel     `json:"channel"`
	Body    interface{} `json:"body,omitempty"`
}

type TotalUncheckedMessage

type TotalUncheckedMessage struct {
	Total int64 `json:"total"`
}

TotalUncheckedMessage is total unchecked chat messages

type WrapPackageReq

type WrapPackageReq struct {
	PackageReq
	Sender *Client
	Raw    []byte
}

Jump to

Keyboard shortcuts

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