Documentation ¶
Index ¶
- Constants
- Variables
- type Channel
- type ChunkHandler
- type ChunkStatus
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(code int, data interface{}, err error)
- func (c *Context) AbortWithRespond(result interface{})
- func (c *Context) Bind(dest interface{}) error
- func (c *Context) ClientIP() string
- func (c *Context) Copy() *Context
- func (c *Context) Emit(event string, result interface{})
- func (c *Context) EmitOthers(event string, result interface{})
- func (c *Context) Error(err error) *Context
- func (c *Context) Get(key string) (v interface{}, ok bool)
- func (c *Context) GetBool(key string) (v bool)
- func (c *Context) GetDuration(key string) (v time.Duration)
- func (c *Context) GetFile(name ...string) (*File, error)
- func (c *Context) GetFiles(name ...string) ([]*File, error)
- func (c *Context) GetFloat64(key string) (v float64)
- func (c *Context) GetInt(key string) (v int)
- func (c *Context) GetMapString(key string) (v map[string]string)
- func (c *Context) GetString(key string) (v string)
- func (c *Context) GetStringMap(key string) (v map[string]interface{})
- func (c *Context) GetStringSlice(key string) (v []string)
- func (c *Context) GetTime(key string) (v time.Time)
- func (c *Context) IsAborted() bool
- func (c *Context) MustBind(dest interface{}) error
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) MustGetFile(name ...string) *File
- func (c *Context) MustGetFiles(name ...string) []*File
- func (c *Context) Next()
- func (c *Context) Param(indexes ...int) *Param
- func (c *Context) Respond(result interface{})
- func (c *Context) RespondWithError(code int, data interface{}, err error)
- func (c *Context) Set(key string, value interface{})
- func (c *Context) Subscribe(event string, channel string) *Context
- func (c *Context) Unsubscribe(event string, channel string) *Context
- type Engine
- func (e *Engine) Close() error
- func (e *Engine) Emit(event string, channel string, result interface{}) error
- func (e *Engine) EmitFilter(event string, channel string, payload interface{}, filter func(*Session) bool) error
- func (e *Engine) EmitMultiple(event string, channel string, result interface{}, sessions []*Session) error
- func (e *Engine) Event(name string)
- func (e *Engine) HandleChunk(handler ChunkHandler) *Engine
- func (e *Engine) HandleSubscribe(handler SubscribeHandler) *Engine
- func (e *Engine) Len() int
- func (e *Engine) NoMethod(handler ...HandlerFunc) *Engine
- func (e *Engine) Register(method string, handler ...HandlerFunc) *Method
- func (e *Engine) Run(port ...string)
- func (e *Engine) Use(handlers ...HandlerFunc) *Engine
- type EngineOption
- type Error
- type ErrorType
- type Event
- type File
- type H
- type HandlerFunc
- type Method
- type MethodOption
- type Param
- func (p *Param) Get() (v interface{})
- func (p *Param) GetBool() (v bool)
- func (p *Param) GetDuration() (v time.Duration)
- func (p *Param) GetFloat64() (v float64)
- func (p *Param) GetInt() (v int)
- func (p *Param) GetMapString() (v map[string]string)
- func (p *Param) GetString() (v string)
- func (p *Param) GetStringMap() (v map[string]interface{})
- func (p *Param) GetStringSlice() (v []string)
- func (p *Param) GetTime() (v time.Time)
- func (p *Param) Len() int
- type RateLimitConfig
- type RawFile
- type Request
- type RequestLimitConfig
- type Response
- type ResponseError
- type Session
- func (s *Session) Copy() *Session
- func (s *Session) Disconnect() error
- func (s *Session) Get(name string) (v interface{}, ok bool)
- func (s *Session) GetBool(key string) (v bool)
- func (s *Session) GetDuration(key string) (v time.Duration)
- func (s *Session) GetFloat64(key string) (v float64)
- func (s *Session) GetInt(key string) (v int)
- func (s *Session) GetMapString(key string) (v map[string]string)
- func (s *Session) GetString(key string) (v string)
- func (s *Session) GetStringMap(key string) (v map[string]interface{})
- func (s *Session) GetStringSlice(key string) (v []string)
- func (s *Session) GetTime(key string) (v time.Time)
- func (s *Session) Set(key string, value interface{})
- type SubscribeHandler
Constants ¶
const ( // B 為 Byte。 B = 1 // KB 為 Kilobyte。 KB = 1024 * B // MB 為 Megabyte。 MB = 1024 * KB // GB 為 Gigabyte。 GB = 1024 * MB // TB 為 Terabyte。 TB = 1024 * GB )
const ( // StatusError 表示有內部錯誤發生。 StatusError = -1000 // StatusFull 表示此請求無法被接受,因為額度已滿。例如:使用者加入了一個已滿的聊天室、好友清單已滿。 StatusFull = -1001 // StatusExists 表示請求的事物已經存在,例如:重複的使用者名稱、電子郵件地址。 StatusExists = -1002 // StatusInvalid 表示此請求格式不正確。 StatusInvalid = -1003 // StatusNotFound 表示找不到請求的資源。 StatusNotFound = -1004 // StatusNotAuthorized 表示使用者需要登入才能進行此請求。 StatusNotAuthorized = -1005 // StatusNoPermission 表示使用者已登入但沒有相關權限執行此請求。 StatusNoPermission = -1006 // StatusUnimplemented 表示此功能尚未實作完成。 StatusUnimplemented = -1007 // StatusTooManyRequests 表示使用者近期發送太多請求,需稍後再試。 StatusTooManyRequests = -1008 // StatusResourceExhausted 表示使用者可用的額度已耗盡。 StatusResourceExhausted = -1009 // StatusBusy 表示伺服器正繁忙無法進行執行請求。 StatusBusy = -1010 // StatusFileRetry 表示檔案區塊發生錯誤,需要重新上傳相同區塊。 StatusFileRetry = -1011 // StatusFileEmpty 表示上傳的檔案、區塊是空的。 StatusFileEmpty = -1012 // StatusFileTooLarge 表示檔案過大無法上傳。 StatusFileTooLarge = -1013 // StatusTimeout 表示這個請求費時過久導致逾期而被終止。 StatusTimeout = -1014 )
Variables ¶
var ( // ErrEventNotFound 表示欲發送的事件沒有被初始化或任何客戶端監聽而無法找到因此發送失敗。 ErrEventNotFound = errors.New("mego: the event doesn't exist") // ErrChannelNotFound 表示欲發送的事件存在,但目標頻道沒有被初始化或任何客戶端監聽而無法找到因此發送失敗。 ErrChannelNotFound = errors.New("mego: the channel doesn't exist") // ErrFileNotFound 表示欲取得的檔案並不存在,可能是客戶端上傳不完整。 ErrFileNotFound = errors.New("mego: the file was not found") // ErrKeyNotFound 表示欲從鍵值組中取得的鍵名並不存在。 ErrKeyNotFound = errors.New("mego: the key was not found") // ErrPanicRecovered 表示 Panic 發生了但已回復正常。 ErrPanicRecovered = errors.New("mego: panic recovered") )
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct { // Name 是這個頻道的名稱。 Name string // Sessions 是監聽此頻道的階段 ID 切片。 Sessions []*Session // Event 是這個頻道的父事件。 Event *Event }
Channel 呈現了事件中的一個頻道與其監聽者。
type ChunkHandler ¶
type ChunkHandler func(ctx *Context, raw *RawFile, dest *File) ChunkStatus
ChunkHandler 是區塊處理函式。
type ChunkStatus ¶
type ChunkStatus int
ChunkStatus 是區塊處理結果狀態碼,用以讓 Mego 得知下一步該如何執行。
const ( // ChunkNext 表示本次處理成功,請求下一個檔案區塊。 ChunkNext ChunkStatus = iota // ChunkDone 表示所有區塊皆處理完畢,結束檔案處理。 ChunkDone // ChunkAbort 表示不打算處理本檔案了,結束此檔案的處理手續並停止上傳。 ChunkAbort )
type Context ¶
type Context struct { // Keys 存放透過 `Set` 儲存的鍵值組,僅限於單次請求。 Keys map[string]interface{} // Errors 存放開發者自訂的錯誤,可用在中介軟體或處理函式中。 Errors errorMsgs // Session 是產生此連線的客戶端階段建構體。 Session *Session // ID 是本次請求的工作編號,用以讓客戶端呼叫相對應的處理函式。 ID int // Request 是這個 WebSocket 的 HTTP 請求建構體。 Request *http.Request // Method 是目前正執行的方法建構體。 Method *Method // contains filtered or unexported fields }
Context 是單次請求中的上下文建構體。
func (*Context) AbortWithError ¶
AbortWithError 結束此次請求,避免繼續執行。並以指定的狀態碼、錯誤資料與訊息回應特定的客戶端並表示錯誤發生。
func (*Context) AbortWithRespond ¶
func (c *Context) AbortWithRespond(result interface{})
AbortWithRespond 終止此次請求,避免繼續執行。並以資料回應特定的客戶端。
func (*Context) EmitOthers ¶
EmitOthers 會將帶有指定資料的事件向自己以外的所有人廣播。
func (*Context) GetDuration ¶
GetDuration 能夠以時間長度取得指定的參數。
func (*Context) GetFloat64 ¶
GetFloat64 能夠以浮點數取得指定的參數。
func (*Context) GetMapString ¶
GetMapString 能夠以 `map[string]string` 取得指定的參數。
func (*Context) GetStringMap ¶
GetStringMap 能夠以 `map[string]interface{}` 取得指定的參數。
func (*Context) GetStringSlice ¶
GetStringSlice 能夠以字串切片取得指定的參數。
func (*Context) MustGetFile ¶
MustGetFile 會回傳一個指定檔案欄位中的檔案,並且在沒有該檔案的時候呼叫 `panic` 終止此請求。
func (*Context) MustGetFiles ¶
MustGetFiles 會取得指定檔案欄位中的全部檔案,並回傳一個檔案切片供開發者遍歷。沒有該檔案欄位的時候會呼叫 `panic` 終止此請求。
func (*Context) Respond ¶
func (c *Context) Respond(result interface{})
Respond 會以指定的狀態碼、資料回應特定的客戶端。
func (*Context) RespondWithError ¶
RespondWithError 會以指定的狀態碼、錯誤資料與訊息回應特定的客戶端並表示錯誤發生。
type Engine ¶
type Engine struct { // Sessions 儲存了正在連線的所有階段。 Sessions map[string]*Session // Events 儲存了所有可用的事件與其監聽的客戶端資料。 Events map[string]*Event // Methods 是所有可用的方法切片。 Methods map[string]*Method // Option 是這個引擎的設置。 Option *EngineOption // contains filtered or unexported fields }
Engine 是 Mego 最主要的引擎結構體。
func (*Engine) EmitFilter ¶
func (e *Engine) EmitFilter(event string, channel string, payload interface{}, filter func(*Session) bool) error
EmitFilter 會以過濾函式來決定要將帶有指定資料的事件廣播給誰。 如果過濾函式回傳 `true` 則表示該客戶端會接收到該事件。
func (*Engine) EmitMultiple ¶
func (e *Engine) EmitMultiple(event string, channel string, result interface{}, sessions []*Session) error
EmitMultiple 會將指定事件與資料向指定的客戶端切片進行廣播。
func (*Engine) HandleChunk ¶
func (e *Engine) HandleChunk(handler ChunkHandler) *Engine
HandleChunk 會更改預設的區塊處理函式,開發者可以傳入一個回呼函式並接收區塊內容。 回傳 `ChunkStatus` 來告訴 Mego 區塊的處理狀態如何。
func (*Engine) HandleSubscribe ¶
func (e *Engine) HandleSubscribe(handler SubscribeHandler) *Engine
HandleSubscribe 會更改預設的事件訂閱檢查函式,開發者可傳入一個回呼函式並接收客戶端欲訂閱的事件與頻道和相關資料。 回傳一個 `false` 即表示客戶端的資格不符,將不納入訂閱清單中。該客戶端將無法接收到指定的事件。
func (*Engine) NoMethod ¶
func (e *Engine) NoMethod(handler ...HandlerFunc) *Engine
NoMethod 會在客戶端呼叫不存在方法時被執行。
type EngineOption ¶
type EngineOption struct { // MaxSize 是這個方法允許接收的最大位元組(Bytes)。 MaxSize int // MaxChunkSize 是這個方法允許的區塊最大位元組(Bytes)。 MaxChunkSize int // MaxFileSize 是這個方法允許的檔案最大位元組(Bytes),會在每次接收區塊時結算總計大小, // 如果超過此大小則停止接收檔案。 MaxFileSize int // MaxSessions 是引擎能容忍的最大階段連線數量。 MaxSessions int // CheckInterval 是每隔幾秒進行一次階段是否仍存在的連線檢查, // 此為輕量檢查而非發送回應至客戶端。 CheckInterval int }
EngineOption 是引擎的選項設置。
type Error ¶
type Error struct { // Err 是實際的錯誤資料。 Err error // Meta 是這個錯誤的中繼資料,可傳遞至其他函式讀取運用。 Meta interface{} // Type 是這個錯誤的種類,預設為 `ErrorTypePrivate`。 Type ErrorType }
Error 呈現了一個路由中發生的錯誤。
func (*Error) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface.
type Event ¶
type Event struct { // Name 是這個事件的名稱。 Name string // Channels 是這個事件的所有頻道。 Channels map[string]*Channel // contains filtered or unexported fields }
Event 呈現了單一個事件。
type File ¶
type File struct { // Name 為此檔案的原生名稱。 Name string // Size 是這個檔案的總位元組大小。 Size int // Extension 是這個檔案的副檔名。 Extension string // Path 為此檔案上傳後的本地路徑。 Path string // Keys 為此檔案的鍵值組,可供開發者存放自訂資料。 Keys map[string]interface{} }
File 呈現了一個接收的檔案資料。
type HandlerFunc ¶
type HandlerFunc func(*Context)
HandlerFunc 是方法處理函式的型態別名。
func LoggerWithWriter ¶
func LoggerWithWriter(out io.Writer) HandlerFunc
LoggerWithWriter 可以傳入自訂的 `io.Writer` 決定紀錄的輸出位置(如:`Stdout` 或檔案)。
func RateLimit ¶
func RateLimit(conf RateLimitConfig) HandlerFunc
RateLimit 會回傳一個流量限制的中介軟體,可以限制單個階段在限時時間內能夠發出多少個請求。
func Recovery ¶
func Recovery() HandlerFunc
Recovery 會回傳一個回復中介軟體, 這能在請求發生 `panic` 錯誤時自動回復並顯示相關錯誤訊息供除錯,而不會導致整個伺服器強制關閉。
func RecoveryWithWriter ¶
func RecoveryWithWriter(out io.Writer) HandlerFunc
RecoveryWithWriter 可以傳入自訂的 `io.Writer` 決定回復紀錄的輸出位置(如:`Stdout` 或檔案)。
func RequestLimit ¶
func RequestLimit(conf RequestLimitConfig) HandlerFunc
RequestLimit 會回傳一個能夠限制單個方法最大同時請求數的中介軟體。
type Method ¶
type Method struct { // Name 是此方法的名稱。 Name string // Handlers 是中介軟體與處理函式。 Handlers []HandlerFunc // Option 是此方法的選項。 Option *MethodOption // ChunkHandler 是本方法的區塊處理回呼函式。 ChunkHandler ChunkHandler }
Method 呈現了一個方法。
type MethodOption ¶
type MethodOption struct { // MaxSize 是這個方法允許接收的最大位元組(Bytes)。此選項會覆蓋引擎設定。 MaxSize int // MaxChunkSize 是這個方法允許的區塊最大位元組(Bytes)。此選項會覆蓋引擎設定。 MaxChunkSize int // MaxFileSize 是這個方法允許的檔案最大位元組(Bytes),會在每次接收區塊時結算總計大小, // 如果超過此大小則停止接收檔案。此選項會覆蓋引擎設定。 MaxFileSize int }
MethodOption 是一個方法的選項。
type Param ¶
type Param struct {
// contains filtered or unexported fields
}
Param 代表著單一個參數。
func (*Param) GetDuration ¶
GetDuration 能夠以時間長度取得指定的參數。
func (*Param) GetMapString ¶
GetMapString 能夠以 `map[string]string` 取得指定的參數。
func (*Param) GetStringMap ¶
GetStringMap 能夠以 `map[string]interface{}` 取得指定的參數。
func (*Param) GetStringSlice ¶
GetStringSlice 能夠以字串切片取得指定的參數。
type RateLimitConfig ¶
type RateLimitConfig struct { // Period 是計時的週期。 Period int // Limit 表示單個階段在指定週期內僅能發送相同請求的數量。 Limit int }
RateLimitConfig 是用在流量限制的選項建構體。
type RawFile ¶
type RawFile struct { // Binary 是檔案的二進制。 Binary []byte `codec:"b" msgpack:"b"` // ID 是由客戶端替此檔案所產生的順序編號,用於區塊組合。 ID int `codec:"i" msgpack:"i"` // Parts 呈現區塊的分塊進度。索引 0 表示總共區塊數,索引 1 則是本區塊編號。 // 如果這個切片是空的表示此為實體檔案而非區塊。 Parts []int `codec:"p" msgpack:"p"` // Name 是檔案的原始名稱。 Name string `codec:"n" msgpack:"n"` }
RawFile 是尚未轉化成為可供開發者使用之前的生檔案資料內容。
type Request ¶
type Request struct { // Method 是欲呼叫的方法名稱。 Method string `codec:"m" msgpack:"m"` // Files 是此請求所包含的檔案欄位與其內容。 Files map[string][]*RawFile `codec:"f" msgpack:"f"` // Params 是資料或參數。 Params interface{} `codec:"p" msgpack:"p"` // ID 為本次請求編號,若無則為單次通知廣播不需回應。 ID int `codec:"i" msgpack:"i"` }
Request 呈現了一個客戶端所傳送過來的請求內容。
type RequestLimitConfig ¶
type RequestLimitConfig struct { // Limit 是指定方法最大的同時連線數。 Limit int }
RequestLimitConfig 是總請求限制的選項建構體。
type Response ¶
type Response struct { // Event 是欲呼叫的客戶端事件名稱。 Event string `codec:"v" msgpack:"v"` // Result 是正常回應時的資料酬載。 Result interface{} `codec:"r" msgpack:"r"` // Error 是錯誤回應時的資料酬載,與 Result 兩者擇其一,不會同時使用。 Error ResponseError `codec:"e" msgpack:"e"` // ID 是當時發送此請求的編號,用以讓客戶端比對是哪個請求所造成的回應。 ID int `codec:"i" msgpack:"i"` }
Response 呈現了 Mego 將會回應給客戶端的內容。
type ResponseError ¶
type ResponseError struct { // Code 是錯誤代號。 Code int `codec:"c" msgpack:"c"` // Message 是人類可讀的簡略錯誤訊息。 Message string `codec:"m" msgpack:"m"` // Data 是本次錯誤的詳細資料。 Data interface{} `codec:"d" msgpack:"d"` }
ResponseError 是回應錯誤資料建構體。
type Session ¶
type Session struct { // Keys 包含了發送此請求的客戶端初始連線資料,此資料由客戶端連線時自訂。可用以取得用戶身份和相關資料。 // 此欄位亦能存放伺服端設置的鍵值組。 Keys map[string]interface{} // ID 是此客戶端初始化時由伺服端所建立的不重複隨機名稱,供辨識匿名身份用。 ID string // contains filtered or unexported fields }
Session 是接收請求時的關聯內容,其包含了指向到特定客戶端的函式。
func (*Session) GetDuration ¶
GetDuration 能夠以時間長度取得指定的參數。
func (*Session) GetFloat64 ¶
GetFloat64 能夠以浮點數取得指定的參數。
func (*Session) GetMapString ¶
GetMapString 能夠以 `map[string]string` 取得指定的參數。
func (*Session) GetStringMap ¶
GetStringMap 能夠以 `map[string]interface{}` 取得指定的參數。
func (*Session) GetStringSlice ¶
GetStringSlice 能夠以字串切片取得指定的參數。