xwebsocket

package
v0.0.0-...-6ffc9e3 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TryWebsocketUpgrade

func TryWebsocketUpgrade(w http.ResponseWriter, r *http.Request, wshandler WebsocketHandler, httphandler http.Handler)

Types

type WebsocketHandler

type WebsocketHandler interface {
	OnConnected(ws *WebsocketSession) error
	OnHandleTextMessage(ws *WebsocketSession, data []byte) error
	OnHandleBinaryMessage(ws *WebsocketSession, data []byte) error
	OnClosed(ws *WebsocketSession) error
}

type WebsocketSession

type WebsocketSession struct {
	*websocket.Conn
	// contains filtered or unexported fields
}

panic: concurrent write to websocket connection https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency websocket 并发读,或者并发写,会出现安全的问题,所以需要加locker

func WebsocketDial

func WebsocketDial(urlstr string, handler WebsocketHandler) (*WebsocketSession, error)

func WebsocketUpgrade

func WebsocketUpgrade(w http.ResponseWriter, r *http.Request, wshandler WebsocketHandler) (*WebsocketSession, error)

func (*WebsocketSession) Close

func (s *WebsocketSession) Close() error

close 函数是直接关闭底层的socket链接,如果消息存在未读取和未发送的情况,就会丢失,属于暴力关闭链接的方式

func (*WebsocketSession) GetHandshakeHeaders

func (s *WebsocketSession) GetHandshakeHeaders() http.Header

func (*WebsocketSession) GetUri

func (s *WebsocketSession) GetUri() string

func (*WebsocketSession) ID

func (s *WebsocketSession) ID() string

func (*WebsocketSession) ShutDown

func (s *WebsocketSession) ShutDown(ctx context.Context) error

该函数主动关闭会话,在关闭链接之前,会先发送一个closemessage消息给对端,然后才关闭链接

func (*WebsocketSession) WriteBinaryMessage

func (s *WebsocketSession) WriteBinaryMessage(data []byte) error

func (*WebsocketSession) WriteTextMessage

func (s *WebsocketSession) WriteTextMessage(data []byte) error

Jump to

Keyboard shortcuts

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