2018-09-29 12:36:47 +00:00
|
|
|
package websocket
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-14 14:47:02 +00:00
|
|
|
"git.xdrm.io/go/ws/internal/uri/parser"
|
2018-05-03 17:31:09 +00:00
|
|
|
)
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// Client contains available information about a client
|
2018-05-03 17:31:09 +00:00
|
|
|
type Client struct {
|
2018-09-29 12:36:47 +00:00
|
|
|
Protocol string // choosen protocol (Sec-WebSocket-Protocol)
|
|
|
|
Arguments [][]string // URI parameters, index 0 is full URI, then matching groups
|
|
|
|
Store interface{} // store (for client implementation-specific data)
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// ControllerFunc is a websocket controller callback function
|
2018-05-05 16:43:16 +00:00
|
|
|
type ControllerFunc func(*Client, <-chan Message, chan<- Message, chan<- Message)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// Controller is a websocket controller
|
2018-05-03 17:31:09 +00:00
|
|
|
type Controller struct {
|
2018-09-29 12:36:47 +00:00
|
|
|
URI *parser.Scheme // uri scheme
|
|
|
|
Fun ControllerFunc // controller function
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// ControllerSet is set of controllers
|
2018-05-03 17:31:09 +00:00
|
|
|
type ControllerSet struct {
|
|
|
|
Def *Controller // default controller
|
2021-05-14 14:47:02 +00:00
|
|
|
URI []*Controller // uri controllers
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match finds a controller for a given URI
|
|
|
|
// also it returns the matching string patterns
|
2018-09-29 12:36:47 +00:00
|
|
|
func (s *ControllerSet) Match(uri string) (*Controller, [][]string) {
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (1) Initialise argument list */
|
2021-05-14 14:47:02 +00:00
|
|
|
arguments := [][]string{{uri}}
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (2) Try each controller */
|
2021-05-14 14:47:02 +00:00
|
|
|
for _, c := range s.URI {
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* 1. If matches */
|
|
|
|
if c.URI.Match(uri) {
|
|
|
|
|
|
|
|
/* Extract matches */
|
|
|
|
match := c.URI.GetAllMatch()
|
|
|
|
|
|
|
|
/* Add them to the 'arg' attribute */
|
|
|
|
arguments = append(arguments, match...)
|
|
|
|
|
|
|
|
/* Mark that we have a controller */
|
|
|
|
return c, arguments
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) If no controller found -> set default controller */
|
|
|
|
if s.Def != nil {
|
|
|
|
return s.Def, arguments
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) If default is NIL, return empty controller */
|
|
|
|
return nil, arguments
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
}
|