Documentation
¶
Index ¶
- func TryWebsocketUpgrade(w http.ResponseWriter, r *http.Request, wshandler WebsocketHandler, ...)
- type WebsocketHandler
- type WebsocketSession
- func (s *WebsocketSession) Close() error
- func (s *WebsocketSession) GetHandshakeHeaders() http.Header
- func (s *WebsocketSession) GetUri() string
- func (s *WebsocketSession) ID() string
- func (s *WebsocketSession) ShutDown(ctx context.Context) error
- func (s *WebsocketSession) WriteBinaryMessage(data []byte) error
- func (s *WebsocketSession) WriteTextMessage(data []byte) error
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 ¶
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
Click to show internal directories.
Click to hide internal directories.