From 86f34dc5df33f08455672fc19f668fd9f776278f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 14 Feb 2017 20:32:36 +0200 Subject: [PATCH] Fix https://github.com/kataras/go-websocket/issues/27 on iris.v6 too Former-commit-id: 29b1ea2ff313bdaeb719c242e08c5d5c9149156a --- iris.go | 37 ++++++++++++++++++++----------------- websocket.go | 20 +++++++++++--------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/iris.go b/iris.go index cabd08a1..c0817bef 100644 --- a/iris.go +++ b/iris.go @@ -288,25 +288,28 @@ func New(setters ...OptionSetter) *Framework { } s.Adapt(EventPolicy{Build: func(*Framework) { - // first check if it's not setted already by any Boot event. - if s.Router.handler == nil { - // and most importantly, check if the user has provided a router - // adaptor, if not then it should panic here, iris can't run without a router attached to it - // and default router not any more, user should select one from ./adaptors or - // any other third-party adaptor may done by community. - // I was coding the new iris version for more than 20 days(~200+ hours of code) - // and I hope that once per application the addition of +1 line users have to put, - // is not a big deal. - if s.policies.RouterBuilderPolicy == nil { - // this is important panic and app can't continue as we said. - s.handlePanic(errRouterIsMissing.Format(s.Config.VHost)) - // don't trace anything else, - // the detailed errRouterIsMissing message will tell the user what to do to fix that. - os.Exit(0) + // user has registered routes + if s.Router.repository.Len() > 0 { + // first check if it's not setted already by any Boot event. + if s.Router.handler == nil { + // and most importantly, check if the user has provided a router + // adaptor, if not then it should panic here, iris can't run without a router attached to it + // and default router not any more, user should select one from ./adaptors or + // any other third-party adaptor may done by community. + // I was coding the new iris version for more than 20 days(~200+ hours of code) + // and I hope that once per application the addition of +1 line users have to put, + // is not a big deal. + if s.policies.RouterBuilderPolicy == nil { + // this is important panic and app can't continue as we said. + s.handlePanic(errRouterIsMissing.Format(s.Config.VHost)) + // don't trace anything else, + // the detailed errRouterIsMissing message will tell the user what to do to fix that. + os.Exit(0) + } + // buid the router using user's selection build policy + s.Router.build(s.policies.RouterBuilderPolicy) } - // buid the router using user's selection build policy - s.Router.build(s.policies.RouterBuilderPolicy) } }}) diff --git a/websocket.go b/websocket.go index d246f372..c0831a15 100644 --- a/websocket.go +++ b/websocket.go @@ -36,13 +36,14 @@ type ( // NewWebsocketServer returns a new empty unitialized websocket server // it runs on first OnConnection func NewWebsocketServer(station *Framework) *WebsocketServer { - return &WebsocketServer{station: station, Server: websocket.New()} + return &WebsocketServer{station: station, Server: websocket.New(), Config: DefaultWebsocketConfiguration()} } // NewWebsocketServer creates the client side source route and the route path Endpoint with the correct Handler // receives the websocket configuration and the iris station // and returns the websocket server which can be attached to more than one iris station (if needed) func (ws *WebsocketServer) init() { + if ws.Config.Endpoint == "" { ws.Config = ws.station.Config.Websocket } @@ -52,14 +53,6 @@ func (ws *WebsocketServer) init() { if c.Endpoint == "" { return } - // set the routing for client-side source (javascript) (optional) - clientSideLookupName := "iris-websocket-client-side" - ws.station.Get(c.Endpoint, ToHandler(ws.Server.Handler())) - // check if client side already exists - if ws.station.Routes().Lookup(clientSideLookupName) == nil { - // serve the client side on domain:port/iris-ws.js - ws.station.StaticContent("/iris-ws.js", contentJavascript, websocket.ClientSource).ChangeName(clientSideLookupName) - } if c.CheckOrigin == nil { c.CheckOrigin = DefaultWebsocketCheckOrigin @@ -85,6 +78,15 @@ func (ws *WebsocketServer) init() { CheckOrigin: c.CheckOrigin, IDGenerator: c.IDGenerator, }) + + // set the routing for client-side source (javascript) (optional) + clientSideLookupName := "iris-websocket-client-side" + ws.station.Get(c.Endpoint, ToHandler(ws.Server.Handler())) + // check if client side already exists + if ws.station.Routes().Lookup(clientSideLookupName) == nil { + // serve the client side on domain:port/iris-ws.js + ws.station.StaticContent("/iris-ws.js", contentJavascript, websocket.ClientSource).ChangeName(clientSideLookupName) + } } // WebsocketConnection is the front-end API that you will use to communicate with the client side