From ca2e46f1c367bc5985ad9bb627bcd08da86229db Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Tue, 16 Aug 2016 13:17:26 +0300 Subject: [PATCH] Fix multi websocket servers https://github.com/kataras/iris/issues/365 --- iris.go | 10 +++++++--- websocket.go | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/iris.go b/iris.go index eb6bc720..17b67596 100644 --- a/iris.go +++ b/iris.go @@ -705,7 +705,11 @@ func Lookups() []Route { // Lookup returns a registed route by its name func (s *Framework) Lookup(routeName string) Route { - return s.mux.lookup(routeName) + r := s.mux.lookup(routeName) + if nil == r { + return nil + } + return r } // Lookups returns all registed routes @@ -1050,7 +1054,7 @@ type ( StaticFS(string, string, int) RouteNameFunc StaticWeb(string, string, int) RouteNameFunc StaticServe(string, ...string) RouteNameFunc - StaticContent(string, string, []byte) func(string) + StaticContent(string, string, []byte) RouteNameFunc Favicon(string, ...string) RouteNameFunc // templates @@ -1739,7 +1743,7 @@ func StaticContent(reqPath string, contentType string, content []byte) RouteName // StaticContent serves bytes, memory cached, on the reqPath // a good example of this is how the websocket server uses that to auto-register the /iris-ws.js -func (api *muxAPI) StaticContent(reqPath string, cType string, content []byte) func(string) { // func(string) because we use that on websockets +func (api *muxAPI) StaticContent(reqPath string, cType string, content []byte) RouteNameFunc { // func(string) because we use that on websockets modtime := time.Now() modtimeStr := modtime.UTC().Format(config.TimeFormat) h := func(ctx *Context) { diff --git a/websocket.go b/websocket.go index 1caeabf3..2e170433 100644 --- a/websocket.go +++ b/websocket.go @@ -51,10 +51,13 @@ func RegisterWebsocketServer(station FrameworkAPI, server WebsocketServer, logge } } } - + clientSideLookupName := "iris-websocket-client-side" station.Get(c.Endpoint, websocketHandler) - // serve the client side on domain:port/iris-ws.js - station.StaticContent("/iris-ws.js", "application/json", websocketClientSource) + // check if client side already exists + if station.Lookup(clientSideLookupName) == nil { + // serve the client side on domain:port/iris-ws.js + station.StaticContent("/iris-ws.js", "application/json", websocketClientSource)(clientSideLookupName) + } }