mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Fix https://github.com/kataras/go-websocket/issues/27 on iris.v6 too
Former-commit-id: 29b1ea2ff313bdaeb719c242e08c5d5c9149156a
This commit is contained in:
parent
d2aee5e187
commit
86f34dc5df
37
iris.go
37
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)
|
||||
}
|
||||
}})
|
||||
|
||||
|
|
20
websocket.go
20
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user