Merge branch 'master' into dev

fix route url reverse and add OnPing at websocket


Former-commit-id: ac8317f8a2516f00150d52f27e48953f49cf76d4
This commit is contained in:
kataras 2017-12-04 05:08:10 +02:00
commit 2d80d73ad7
3 changed files with 28 additions and 14 deletions

View File

@ -209,12 +209,13 @@ func (ps *RoutePathReverser) Path(routeName string, paramValues ...interface{})
return r.ResolvePath(toStringSlice(paramValues)...) return r.ResolvePath(toStringSlice(paramValues)...)
} }
func toStringSlice(args []interface{}) []string { func toStringSlice(args []interface{}) (argsString []string) {
var argsString []string argsSize := len(args)
if len(args) > 0 { if argsSize <= 0 {
argsString = make([]string, len(args), len(args)) return
} }
argsString = make([]string, argsSize, argsSize)
for i, v := range args { for i, v := range args {
if s, ok := v.(string); ok { if s, ok := v.(string); ok {
argsString[i] = s 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, // 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 return
} }
if len(paramValues) == 0 {
return r.Path
}
args := toStringSlice(paramValues)
host := ps.vhost host := ps.vhost
scheme := ps.vscheme scheme := ps.vscheme
args := toStringSlice(paramValues)
// if it's dynamic subdomain then the first argument is the subdomain part // if it's dynamic subdomain then the first argument is the subdomain part
// for this part we are responsible not the custom routers // for this part we are responsible not the custom routers
if r.Subdomain == SubdomainWildcardIndicator { if len(args) > 0 && r.Subdomain == SubdomainWildcardIndicator {
subdomain := args[0] subdomain := args[0]
host = subdomain + "." + host host = subdomain + "." + host
args = args[1:] // remove the subdomain part for the arguments, args = args[1:] // remove the subdomain part for the arguments,
} }
if parsedPath := r.ResolvePath(args...); parsedPath != "" { if parsedPath := r.ResolvePath(args...); parsedPath != "" {

View File

@ -213,7 +213,7 @@ func (s *AmberEngine) ExecuteWriter(w io.Writer, filename string, layout string,
} }
if tmpl := s.fromCache(filename); tmpl != nil { 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) return fmt.Errorf("Template with name %s doesn't exists in the dir", filename)

View File

@ -131,6 +131,8 @@ type (
// MessageFunc is the second argument to the Emitter's Emit functions. // 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 // A callback which should receives one parameter of type string, int, bool or any valid JSON/Go struct
MessageFunc interface{} 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 is the front-end API that you will use to communicate with the client side
Connection interface { Connection interface {
// Emitter implements EmitMessage & Emit // Emitter implements EmitMessage & Emit
@ -154,6 +156,8 @@ type (
OnDisconnect(DisconnectFunc) OnDisconnect(DisconnectFunc)
// OnError registers a callback which fires when this connection occurs an error // OnError registers a callback which fires when this connection occurs an error
OnError(ErrorFunc) 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 // 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. // It does nothing more than firing the OnError listeners. It doesn't sends anything to the client.
@ -200,6 +204,7 @@ type (
onDisconnectListeners []DisconnectFunc onDisconnectListeners []DisconnectFunc
onRoomLeaveListeners []LeaveRoomFunc onRoomLeaveListeners []LeaveRoomFunc
onErrorListeners []ErrorFunc onErrorListeners []ErrorFunc
onPingListeners []PingFunc
onNativeMessageListeners []NativeMessageFunc onNativeMessageListeners []NativeMessageFunc
onEventListeners map[string][]MessageFunc onEventListeners map[string][]MessageFunc
// these were maden for performance only // these were maden for performance only
@ -305,6 +310,8 @@ func (c *connection) startPinger() {
// verifies if already disconected // verifies if already disconected
break break
} }
//fire all OnPing methods
c.fireOnPing()
// try to ping the client, if failed then it disconnects // try to ping the client, if failed then it disconnects
err := c.write(websocket.PingMessage, []byte{}) err := c.write(websocket.PingMessage, []byte{})
if err != nil { if err != nil {
@ -430,6 +437,10 @@ func (c *connection) OnError(cb ErrorFunc) {
c.onErrorListeners = append(c.onErrorListeners, cb) c.onErrorListeners = append(c.onErrorListeners, cb)
} }
func (c *connection) OnPing(cb PingFunc) {
c.onPingListeners = append(c.onPingListeners, cb)
}
func (c *connection) FireOnError(errorMessage string) { func (c *connection) FireOnError(errorMessage string) {
for _, cb := range c.onErrorListeners { for _, cb := range c.onErrorListeners {
cb(errorMessage) 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 { func (c *connection) Disconnect() error {
return c.server.Disconnect(c.ID()) return c.server.Disconnect(c.ID())
} }