ws/controller.go

49 lines
1.2 KiB
Go

package websocket
import "git.xdrm.io/go/ws/internal/uri"
// Client contains available information about a client
type Client struct {
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)
}
// ControllerFunc is a websocket controller callback function
type ControllerFunc func(*Client, <-chan Message, chan<- Message, chan<- Message)
// Controller is a websocket controller
type Controller struct {
URI *uri.Scheme // uri scheme
Fun ControllerFunc // controller function
}
// ControllerSet contains a set of controllers
type ControllerSet struct {
Def *Controller // default controller
URI []*Controller // uri controllers
}
// Match finds a controller for a given URI
// also it returns the matching string patterns
func (s *ControllerSet) Match(uri string) (*Controller, [][]string) {
arguments := [][]string{{uri}}
for _, c := range s.URI {
if c.URI.Match(uri) {
match := c.URI.GetAllMatch()
arguments = append(arguments, match...)
return c, arguments
}
}
// fallback to default
if s.Def != nil {
return s.Def, arguments
}
// no default
return nil, arguments
}