2018-09-29 12:36:47 +00:00
|
|
|
package websocket
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:23:33 +00:00
|
|
|
import "git.xdrm.io/go/ws/internal/uri"
|
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 {
|
2021-05-14 15:23:33 +00:00
|
|
|
URI *uri.Scheme // uri scheme
|
2018-09-29 12:36:47 +00:00
|
|
|
Fun ControllerFunc // controller function
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// ControllerSet contains a 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) {
|
2021-05-14 14:47:02 +00:00
|
|
|
arguments := [][]string{{uri}}
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
for _, c := range s.URI {
|
2018-05-03 17:31:09 +00:00
|
|
|
if c.URI.Match(uri) {
|
|
|
|
match := c.URI.GetAllMatch()
|
|
|
|
arguments = append(arguments, match...)
|
|
|
|
return c, arguments
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// fallback to default
|
2018-05-03 17:31:09 +00:00
|
|
|
if s.Def != nil {
|
|
|
|
return s.Def, arguments
|
|
|
|
}
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// no default
|
2018-05-03 17:31:09 +00:00
|
|
|
return nil, arguments
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
}
|