gwiface

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	PingTime       int    // HeartBeat檢查的時間
	MaxConn        int    // 允許的最大連線數
	WorkerPoolSize uint32 // Working Pool的數量

	// 定義在gorilla/websocket/conn.go內
	// The message types are defined in RFC 6455, section 11.8.
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	//TextMessage = 1
	//
	//// BinaryMessage denotes a binary data message.
	//BinaryMessage = 2
	//
	//// CloseMessage denotes a close control message. The optional message
	//// payload contains a numeric code and text. Use the FormatCloseMessage
	//// function to format a close message payload.
	//CloseMessage = 8
	//
	//// PingMessage denotes a ping control message. The optional message payload
	//// is UTF-8 encoded text.
	//PingMessage = 9
	//
	//// PongMessage denotes a pong control message. The optional message payload
	//// is UTF-8 encoded text.
	//PongMessage = 10
	//一般使用應該用1或2就好
	MessageType int
}

type ConnManager

type ConnManager interface {
	Add(conn Connection)                  // 增加一個conn
	Remove(conn Connection)               // 移除conn
	Get(connID int64) (Connection, error) // 取得Conn使用ConnID
	Len() int                             // 取得總Conn數
	Search(Search)                        // 尋找Conn
	ClearConn()                           // 刪除並且Stop所有Conn
}

type Connection

type Connection interface {
	Start()                                  // 啟動Connection,讓此Connection開始工作
	Stop()                                   // 停止Connection,把此Connection結束
	Context() context.Context                // 回傳ctx,用在自己定義的go routine要把Connection變更狀態
	GetConnection() *websocket.Conn          // 得到內部的socket Conn
	GetConnID() int64                        // 取得ConnectionID
	RemoteAddr() net.Addr                    // 取得Client的IP
	SendMsg(msgID uint32, data []byte) error // 送Message給Client

	SetProperty(key string, value interface{})   //設定一個屬性
	GetProperty(key string) (interface{}, error) //取得一個属性
	RemoveProperty(key string)                   //移除属性

	SetPing()      // 當ping客戶端有回pong時,可以用setping設成有回應
	GetPing() bool // 檢查是否有在時間內取得心跳
	RemovePing()   // 把心跳flag設成false

	IsHeartbeatTimeout() //會一直自己循環的檢查心跳的function
}

* 連接的定義 client連上時會產生一個新的連接

type Message

type Message interface {
	GetMsgID() uint32
	GetData() []byte

	SetMsgID(uint32)
	SetData([]byte)
}

把Request的Message封裝到這個Message裡面用的

type MsgHandle

type MsgHandle interface {
	DoMsgHandler(request Request)          // 馬上用non-blocking的方式處理msg
	AddRouter(msgID uint32, router Router) // 增加一個msg的處理邏輯
	StartWorkerPool()                      // 啟動worker工作池
	SendMsgToTaskQueue(request Request)    // msg給TaskQueue,由worker進行處理
}

Msg管理器的封裝

type Packet

type Packet interface {
	Pack(msg Message) ([]byte, error)
	Unpack([]byte) (Message, error)
}

封包跟拆包封包的資料用的 可以實作這個來增加自己的加密

type Request

type Request interface {
	GetConnection() Connection
	GetData() []byte
	GetMsgID() uint32
}

Request 接口: 把client端的Conn跟Msg封裝在一起成為一個Request

主邏輯處理事處理Request,ws底層是讀Msg出來

type Router

type Router interface {
	PreHandle(request Request)
	Handle(request Request)
	PostHandle(request Request)
}

Router主邏輯,一個邏輯定義一個struct來處理

type Search func(Connection)

Connection的管理器 interface

type Server

type Server interface {
	Start(c *gin.Context)                  // 啟動Server
	Stop()                                 // 停止Server
	Serve(c *gin.Context)                  // 開始一個服務
	AddRouter(msgID uint32, router Router) // 路由功能:註冊處理Msg的方法

	GetConnMgr() ConnManager // 取得連接管理器,可以從中獲得連結

	SetOnConnStart(func(Connection)) // 設定Server的連接建立時Hook的function
	SetOnConnStop(func(Connection))  // 設定Server的連接中斷時的Hook

	CallOnConnStart(conn Connection) // Call上面SetOnConnStart設定的那個 Hook Function
	CallOnConnStop(conn Connection)  // Call上面SetOnConnStop設定的那個 Hook Function

	Packet() Packet
}

* 定義Server的一個interface

Jump to

Keyboard shortcuts

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