ws

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 4 Imported by: 0

README

goWs

基于gorilla封装的并发安全的Golang Websocket组件

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// websocket连接已关闭
	ErrWsConnClose = errors.New("websocket connection closed")
	// websocket唯一连接id不能为空
	ErrIdEmpty = errors.New("id is null")
)

Functions

This section is empty.

Types

type Collection

type Collection interface {
	Set(id string, wsConn *websocket.Conn) error
	Get(id string) (wsConn *websocket.Conn, err error)
	GetGroup(groupName string) (wsConnList []*websocket.Conn, err error)
	GetAll() (wsConnList []*websocket.Conn, err error)
	Del(id string) error
}

客户端维护的websocket在线连接接口

type Conn

type Conn interface {
	Close() error
	Open(w http.ResponseWriter, r *http.Request) error
	Receive() (msg *WsMessage, err error)
	Write(msg *WsMessage) (err error)
}

type MsgTo

type MsgTo struct {
	ToType ToType
	To     string
}

定义了消息发送的对象

type ToType

type ToType string

定义了消息发送的对象类型

const (
	// 发送给所有在线连接
	ToAll ToType = "All"
	// 发送给群组在线连接
	ToGroup ToType = "Group"
	// 发送给指定连接
	ToConn ToType = "Conn"
)

描述了消息发送的对象

type WsConnection

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

WsConnection表示维护的一个websocket类型

func NewWsConnection

func NewWsConnection(id string, collect Collection) *WsConnection

新建一个WsConnection连接. id为唯一连接id; collect为实现Collection接口类

func (*WsConnection) Close

func (conn *WsConnection) Close() error

func (*WsConnection) Open

func (conn *WsConnection) Open(w http.ResponseWriter, r *http.Request) error

func (*WsConnection) Receive

func (conn *WsConnection) Receive() (msg *WsMessage, err error)

func (*WsConnection) Write

func (conn *WsConnection) Write(msg *WsMessage) (err error)

type WsMessage

type WsMessage struct {
	// 消息发送对象
	To MsgTo
	// The message types are defined in RFC 6455, section 11.8.
	MessageType int
	// 消息内容
	Data []byte
}

描述了一个消息实体

Jump to

Keyboard shortcuts

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