mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
Merge branch 'master' into dev
fix route url reverse and add OnPing at websocket Former-commit-id: ac8317f8a2516f00150d52f27e48953f49cf76d4
This commit is contained in:
commit
2d80d73ad7
|
@ -209,12 +209,13 @@ func (ps *RoutePathReverser) Path(routeName string, paramValues ...interface{})
|
|||
return r.ResolvePath(toStringSlice(paramValues)...)
|
||||
}
|
||||
|
||||
func toStringSlice(args []interface{}) []string {
|
||||
var argsString []string
|
||||
if len(args) > 0 {
|
||||
argsString = make([]string, len(args), len(args))
|
||||
func toStringSlice(args []interface{}) (argsString []string) {
|
||||
argsSize := len(args)
|
||||
if argsSize <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
argsString = make([]string, argsSize, argsSize)
|
||||
for i, v := range args {
|
||||
if s, ok := v.(string); ok {
|
||||
argsString[i] = s
|
||||
|
@ -229,7 +230,7 @@ func toStringSlice(args []interface{}) []string {
|
|||
}
|
||||
}
|
||||
}
|
||||
return argsString
|
||||
return
|
||||
}
|
||||
|
||||
// Remove the URL for now, it complicates things for the whole framework without a specific benefits,
|
||||
|
@ -246,21 +247,16 @@ func (ps *RoutePathReverser) URL(routeName string, paramValues ...interface{}) (
|
|||
return
|
||||
}
|
||||
|
||||
if len(paramValues) == 0 {
|
||||
return r.Path
|
||||
}
|
||||
|
||||
args := toStringSlice(paramValues)
|
||||
|
||||
host := ps.vhost
|
||||
scheme := ps.vscheme
|
||||
args := toStringSlice(paramValues)
|
||||
|
||||
// if it's dynamic subdomain then the first argument is the subdomain part
|
||||
// for this part we are responsible not the custom routers
|
||||
if r.Subdomain == SubdomainWildcardIndicator {
|
||||
if len(args) > 0 && r.Subdomain == SubdomainWildcardIndicator {
|
||||
subdomain := args[0]
|
||||
host = subdomain + "." + host
|
||||
args = args[1:] // remove the subdomain part for the arguments,
|
||||
|
||||
}
|
||||
|
||||
if parsedPath := r.ResolvePath(args...); parsedPath != "" {
|
||||
|
|
|
@ -213,7 +213,7 @@ func (s *AmberEngine) ExecuteWriter(w io.Writer, filename string, layout string,
|
|||
}
|
||||
|
||||
if tmpl := s.fromCache(filename); tmpl != nil {
|
||||
return tmpl.ExecuteTemplate(w, filename, bindingData)
|
||||
return tmpl.Execute(w, bindingData)
|
||||
}
|
||||
|
||||
return fmt.Errorf("Template with name %s doesn't exists in the dir", filename)
|
||||
|
|
|
@ -131,6 +131,8 @@ type (
|
|||
// MessageFunc is the second argument to the Emitter's Emit functions.
|
||||
// A callback which should receives one parameter of type string, int, bool or any valid JSON/Go struct
|
||||
MessageFunc interface{}
|
||||
// PingFunc is the callback which fires each ping
|
||||
PingFunc func()
|
||||
// Connection is the front-end API that you will use to communicate with the client side
|
||||
Connection interface {
|
||||
// Emitter implements EmitMessage & Emit
|
||||
|
@ -154,6 +156,8 @@ type (
|
|||
OnDisconnect(DisconnectFunc)
|
||||
// OnError registers a callback which fires when this connection occurs an error
|
||||
OnError(ErrorFunc)
|
||||
// OnPing registers a callback which fires on each ping
|
||||
OnPing(PingFunc)
|
||||
// FireStatusCode can be used to send a custom error message to the connection
|
||||
//
|
||||
// It does nothing more than firing the OnError listeners. It doesn't sends anything to the client.
|
||||
|
@ -200,6 +204,7 @@ type (
|
|||
onDisconnectListeners []DisconnectFunc
|
||||
onRoomLeaveListeners []LeaveRoomFunc
|
||||
onErrorListeners []ErrorFunc
|
||||
onPingListeners []PingFunc
|
||||
onNativeMessageListeners []NativeMessageFunc
|
||||
onEventListeners map[string][]MessageFunc
|
||||
// these were maden for performance only
|
||||
|
@ -305,6 +310,8 @@ func (c *connection) startPinger() {
|
|||
// verifies if already disconected
|
||||
break
|
||||
}
|
||||
//fire all OnPing methods
|
||||
c.fireOnPing()
|
||||
// try to ping the client, if failed then it disconnects
|
||||
err := c.write(websocket.PingMessage, []byte{})
|
||||
if err != nil {
|
||||
|
@ -430,6 +437,10 @@ func (c *connection) OnError(cb ErrorFunc) {
|
|||
c.onErrorListeners = append(c.onErrorListeners, cb)
|
||||
}
|
||||
|
||||
func (c *connection) OnPing(cb PingFunc) {
|
||||
c.onPingListeners = append(c.onPingListeners, cb)
|
||||
}
|
||||
|
||||
func (c *connection) FireOnError(errorMessage string) {
|
||||
for _, cb := range c.onErrorListeners {
|
||||
cb(errorMessage)
|
||||
|
@ -492,6 +503,13 @@ func (c *connection) fireOnLeave(roomName string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *connection) fireOnPing() {
|
||||
// fire the onPingListeners
|
||||
for i := range c.onPingListeners {
|
||||
c.onPingListeners[i]()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *connection) Disconnect() error {
|
||||
return c.server.Disconnect(c.ID())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user