websocket

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

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

Go to latest
Published: Apr 11, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultIP   = "127.0.0.1"
	DefaultPort = 30117

	UniqIDLength = 32
)

Variables

View Source
var (
	ErrorContextCanceled       = fmt.Errorf("session canceled by context")
	ErrorConnectionInvalid     = fmt.Errorf("connection is invalid")
	ErrorResponseLengthInvalid = fmt.Errorf("response data length is invalid")
	ErrorAllConnectionInvalid  = fmt.Errorf("all connections are invalid")
)

Functions

func DestroyGlobalSessionPool

func DestroyGlobalSessionPool() error

Types

type ClientSessionPool

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

---------------------------------------------------- 用于客户端的session pool

func GetGlobalSessionPool

func GetGlobalSessionPool(ip string, port int32, url string, size int32, callback WebSocketFunc) *ClientSessionPool

用于初始化并返回全局客户端的session pool

func (*ClientSessionPool) Destroy

func (sp *ClientSessionPool) Destroy(err error) error

func (*ClientSessionPool) GetSession

func (sp *ClientSessionPool) GetSession() (*Session, error)

获取可用session

type Message

type Message struct {
	UniqID MessageID // 固定长度,用于识别命令
	// op           ws.OpCode  // 先用固定的 Binary
	Data         []byte
	WaitResponse bool // 发送成功后,是否还需要等待对方返回结果
	RetChan      chan *MessageResult
}

约束条件: 返回结果的 UniqID 需要保持不变,方便收到结果后,找到对应的chan

type MessageID

type MessageID string

固定长度UniqIDLength,用于识别命令

type MessageResult

type MessageResult struct {
	Err    error
	UniqID MessageID // 固定长度,用于识别命令
	Data   []byte
}

type Session

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

func NewClientSession

func NewClientSession(ip string, port int32, url string, callback WebSocketFunc) *Session

client端创建session,需要指定目标server的ip和端口

func NewServerSession

func NewServerSession(w http.ResponseWriter, r *restful.Request, callback WebSocketFunc) *Session

server端创建session,需要指定http处理函数

func (*Session) IsValid

func (s *Session) IsValid() bool

func (*Session) Send

func (s *Session) Send(data []byte, waitresponse bool) *MessageResult

session 内部将data封装为Message发送,并通过chan接收发送结果,Message的id需要内部生成 如果 waitresponse为true,则需要等待返回的结果

func (*Session) SendWithID

func (s *Session) SendWithID(id MessageID, data []byte, waitresponse bool) *MessageResult

session 内部将data封装为Message发送,并通过chan接收发送结果,这儿指定了id,无需自动生成 如果 waitresponse为true,则需要等待返回的结果

func (*Session) Size

func (s *Session) Size() int

返回当前session的任务数

type WebSocketFunc

type WebSocketFunc func(r *restful.Request, id MessageID, data []byte, s *Session) error

处理收到的消息,一般是流程是将data转成需要的格式,然后业务逻辑处理,处理完,再通过 Session发送回去

Jump to

Keyboard shortcuts

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