Update to 4.0.0-alpha.3 - Response Engines, 'inject' the context.JSON/JSONP/Text/Data/Markdown/Render, Read HISTORY.md
## 4.0.0-alpha.2 -> 4.0.0-alpha.3
**New**
A **Response Engine** gives you the freedom to create/change the
render/response writer for
- `context.JSON`
- `context.JSONP`
- `context.XML`
- `context.Text`
- `context.Markdown`
- `context.Data`
- `context.Render("my_custom_type",mystructOrData{},
iris.RenderOptions{"gzip":false,"charset":"UTF-8"})`
- `context.MarkdownString`
- `iris.ResponseString(...)`
**Fix**
- https://github.com/kataras/iris/issues/294
**Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` &
`iris.Config.Render.Template.Charset`, but you can override it at
runtime by passinth a map `iris.RenderOptions` on the `context.Render`
call .
- `iris.Config.IsDevelopment` , before alpha.1 was
`iris.Config.Render.Template.IsDevelopment`
**Websockets changes**
No need to import the `github.com/kataras/iris/websocket` to use the
`Connection` iteral, the websocket moved inside `kataras/iris` , now all
exported variables' names have the prefix of `Websocket`, so the old
`websocket.Connection` is now `iris.WebsocketConnection`.
Generally, no other changes on the 'frontend API', for response engines
examples and how you can register your own to add more features on
existing response engines or replace them, look
[here](https://github.com/iris-contrib/response).
**BAD SIDE**: E-Book is still pointing on the v3 release, but will be
updated soon.
2016-07-18 16:40:42 +02:00
|
|
|
package iris
|
|
|
|
|
|
|
|
import (
|
2017-01-02 20:20:17 +01:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2016-09-01 05:01:53 +02:00
|
|
|
"github.com/kataras/go-websocket"
|
Update to 4.0.0-alpha.3 - Response Engines, 'inject' the context.JSON/JSONP/Text/Data/Markdown/Render, Read HISTORY.md
## 4.0.0-alpha.2 -> 4.0.0-alpha.3
**New**
A **Response Engine** gives you the freedom to create/change the
render/response writer for
- `context.JSON`
- `context.JSONP`
- `context.XML`
- `context.Text`
- `context.Markdown`
- `context.Data`
- `context.Render("my_custom_type",mystructOrData{},
iris.RenderOptions{"gzip":false,"charset":"UTF-8"})`
- `context.MarkdownString`
- `iris.ResponseString(...)`
**Fix**
- https://github.com/kataras/iris/issues/294
**Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` &
`iris.Config.Render.Template.Charset`, but you can override it at
runtime by passinth a map `iris.RenderOptions` on the `context.Render`
call .
- `iris.Config.IsDevelopment` , before alpha.1 was
`iris.Config.Render.Template.IsDevelopment`
**Websockets changes**
No need to import the `github.com/kataras/iris/websocket` to use the
`Connection` iteral, the websocket moved inside `kataras/iris` , now all
exported variables' names have the prefix of `Websocket`, so the old
`websocket.Connection` is now `iris.WebsocketConnection`.
Generally, no other changes on the 'frontend API', for response engines
examples and how you can register your own to add more features on
existing response engines or replace them, look
[here](https://github.com/iris-contrib/response).
**BAD SIDE**: E-Book is still pointing on the v3 release, but will be
updated soon.
2016-07-18 16:40:42 +02:00
|
|
|
)
|
|
|
|
|
2016-09-09 07:09:03 +02:00
|
|
|
// conversionals
|
|
|
|
const (
|
|
|
|
// All is the string which the Emmiter use to send a message to all
|
|
|
|
All = websocket.All
|
|
|
|
// NotMe is the string which the Emmiter use to send a message to all except this websocket.Connection
|
|
|
|
NotMe = websocket.NotMe
|
|
|
|
// Broadcast is the string which the Emmiter use to send a message to all except this websocket.Connection, same as 'NotMe'
|
|
|
|
Broadcast = websocket.Broadcast
|
|
|
|
)
|
2016-09-01 05:01:53 +02:00
|
|
|
|
2016-09-09 07:09:03 +02:00
|
|
|
// Note I keep this code only to no change the front-end API, we could only use the go-websocket and set our custom upgrader
|
Update to 4.0.0-alpha.3 - Response Engines, 'inject' the context.JSON/JSONP/Text/Data/Markdown/Render, Read HISTORY.md
## 4.0.0-alpha.2 -> 4.0.0-alpha.3
**New**
A **Response Engine** gives you the freedom to create/change the
render/response writer for
- `context.JSON`
- `context.JSONP`
- `context.XML`
- `context.Text`
- `context.Markdown`
- `context.Data`
- `context.Render("my_custom_type",mystructOrData{},
iris.RenderOptions{"gzip":false,"charset":"UTF-8"})`
- `context.MarkdownString`
- `iris.ResponseString(...)`
**Fix**
- https://github.com/kataras/iris/issues/294
**Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` &
`iris.Config.Render.Template.Charset`, but you can override it at
runtime by passinth a map `iris.RenderOptions` on the `context.Render`
call .
- `iris.Config.IsDevelopment` , before alpha.1 was
`iris.Config.Render.Template.IsDevelopment`
**Websockets changes**
No need to import the `github.com/kataras/iris/websocket` to use the
`Connection` iteral, the websocket moved inside `kataras/iris` , now all
exported variables' names have the prefix of `Websocket`, so the old
`websocket.Connection` is now `iris.WebsocketConnection`.
Generally, no other changes on the 'frontend API', for response engines
examples and how you can register your own to add more features on
existing response engines or replace them, look
[here](https://github.com/iris-contrib/response).
**BAD SIDE**: E-Book is still pointing on the v3 release, but will be
updated soon.
2016-07-18 16:40:42 +02:00
|
|
|
|
|
|
|
type (
|
2016-09-01 05:01:53 +02:00
|
|
|
// WebsocketServer is the iris websocket server, expose the websocket.Server
|
|
|
|
// the below code is a wrapper and bridge between iris-contrib/websocket and kataras/go-websocket
|
|
|
|
WebsocketServer struct {
|
|
|
|
websocket.Server
|
2017-01-02 20:20:17 +01:00
|
|
|
station *Framework
|
|
|
|
once sync.Once
|
|
|
|
// Config:
|
|
|
|
// if endpoint is not empty then this configuration is used instead of the station's
|
|
|
|
// useful when the user/dev wants more than one websocket server inside one iris instance.
|
|
|
|
Config WebsocketConfiguration
|
Update to 4.0.0-alpha.3 - Response Engines, 'inject' the context.JSON/JSONP/Text/Data/Markdown/Render, Read HISTORY.md
## 4.0.0-alpha.2 -> 4.0.0-alpha.3
**New**
A **Response Engine** gives you the freedom to create/change the
render/response writer for
- `context.JSON`
- `context.JSONP`
- `context.XML`
- `context.Text`
- `context.Markdown`
- `context.Data`
- `context.Render("my_custom_type",mystructOrData{},
iris.RenderOptions{"gzip":false,"charset":"UTF-8"})`
- `context.MarkdownString`
- `iris.ResponseString(...)`
**Fix**
- https://github.com/kataras/iris/issues/294
**Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` &
`iris.Config.Render.Template.Charset`, but you can override it at
runtime by passinth a map `iris.RenderOptions` on the `context.Render`
call .
- `iris.Config.IsDevelopment` , before alpha.1 was
`iris.Config.Render.Template.IsDevelopment`
**Websockets changes**
No need to import the `github.com/kataras/iris/websocket` to use the
`Connection` iteral, the websocket moved inside `kataras/iris` , now all
exported variables' names have the prefix of `Websocket`, so the old
`websocket.Connection` is now `iris.WebsocketConnection`.
Generally, no other changes on the 'frontend API', for response engines
examples and how you can register your own to add more features on
existing response engines or replace them, look
[here](https://github.com/iris-contrib/response).
**BAD SIDE**: E-Book is still pointing on the v3 release, but will be
updated soon.
2016-07-18 16:40:42 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-01-02 20:20:17 +01:00
|
|
|
// 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()}
|
2016-09-09 07:09:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 20:20:17 +01:00
|
|
|
// 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
|
2016-09-09 07:09:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 20:20:17 +01:00
|
|
|
c := ws.Config
|
2016-09-20 09:15:45 +02:00
|
|
|
|
2017-01-02 20:20:17 +01:00
|
|
|
if c.Endpoint == "" {
|
|
|
|
return
|
2016-10-05 12:21:40 +02:00
|
|
|
}
|
2016-09-20 09:15:45 +02:00
|
|
|
// set the routing for client-side source (javascript) (optional)
|
2016-09-09 07:09:03 +02:00
|
|
|
clientSideLookupName := "iris-websocket-client-side"
|
2017-01-02 20:20:17 +01:00
|
|
|
ws.station.Get(c.Endpoint, ToHandler(ws.Server.Handler()))
|
2016-09-09 07:09:03 +02:00
|
|
|
// check if client side already exists
|
2017-01-02 20:20:17 +01:00
|
|
|
if ws.station.Lookup(clientSideLookupName) == nil {
|
2016-09-09 07:09:03 +02:00
|
|
|
// serve the client side on domain:port/iris-ws.js
|
2017-01-02 20:20:17 +01:00
|
|
|
ws.station.StaticContent("/iris-ws.js", contentJavascript, websocket.ClientSource)(clientSideLookupName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.CheckOrigin == nil {
|
|
|
|
c.CheckOrigin = DefaultWebsocketCheckOrigin
|
2016-09-09 07:09:03 +02:00
|
|
|
}
|
2016-09-20 09:15:45 +02:00
|
|
|
|
2017-01-02 20:20:17 +01:00
|
|
|
if c.Error == nil {
|
|
|
|
c.Error = DefaultWebsocketError
|
|
|
|
}
|
|
|
|
// set the underline websocket server's configuration
|
|
|
|
ws.Server.Set(websocket.Config{
|
2016-09-20 09:15:45 +02:00
|
|
|
WriteTimeout: c.WriteTimeout,
|
|
|
|
PongTimeout: c.PongTimeout,
|
|
|
|
PingPeriod: c.PingPeriod,
|
|
|
|
MaxMessageSize: c.MaxMessageSize,
|
|
|
|
BinaryMessages: c.BinaryMessages,
|
|
|
|
ReadBufferSize: c.ReadBufferSize,
|
|
|
|
WriteBufferSize: c.WriteBufferSize,
|
2017-01-02 20:20:17 +01:00
|
|
|
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
|
|
|
ctx := ws.station.AcquireCtx(w, r)
|
|
|
|
c.Error(ctx, status, reason)
|
|
|
|
ws.station.ReleaseCtx(ctx)
|
|
|
|
},
|
|
|
|
CheckOrigin: c.CheckOrigin,
|
2017-01-09 05:23:49 +01:00
|
|
|
IDGenerator: c.IDGenerator,
|
2016-09-20 09:15:45 +02:00
|
|
|
})
|
2016-09-09 07:09:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 05:01:53 +02:00
|
|
|
// WebsocketConnection is the front-end API that you will use to communicate with the client side
|
|
|
|
type WebsocketConnection interface {
|
|
|
|
websocket.Connection
|
Update to 4.0.0-alpha.3 - Response Engines, 'inject' the context.JSON/JSONP/Text/Data/Markdown/Render, Read HISTORY.md
## 4.0.0-alpha.2 -> 4.0.0-alpha.3
**New**
A **Response Engine** gives you the freedom to create/change the
render/response writer for
- `context.JSON`
- `context.JSONP`
- `context.XML`
- `context.Text`
- `context.Markdown`
- `context.Data`
- `context.Render("my_custom_type",mystructOrData{},
iris.RenderOptions{"gzip":false,"charset":"UTF-8"})`
- `context.MarkdownString`
- `iris.ResponseString(...)`
**Fix**
- https://github.com/kataras/iris/issues/294
**Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` &
`iris.Config.Render.Template.Charset`, but you can override it at
runtime by passinth a map `iris.RenderOptions` on the `context.Render`
call .
- `iris.Config.IsDevelopment` , before alpha.1 was
`iris.Config.Render.Template.IsDevelopment`
**Websockets changes**
No need to import the `github.com/kataras/iris/websocket` to use the
`Connection` iteral, the websocket moved inside `kataras/iris` , now all
exported variables' names have the prefix of `Websocket`, so the old
`websocket.Connection` is now `iris.WebsocketConnection`.
Generally, no other changes on the 'frontend API', for response engines
examples and how you can register your own to add more features on
existing response engines or replace them, look
[here](https://github.com/iris-contrib/response).
**BAD SIDE**: E-Book is still pointing on the v3 release, but will be
updated soon.
2016-07-18 16:40:42 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 05:01:53 +02:00
|
|
|
// OnConnection this is the main event you, as developer, will work with each of the websocket connections
|
2017-01-02 20:20:17 +01:00
|
|
|
func (ws *WebsocketServer) OnConnection(connectionListener func(WebsocketConnection)) {
|
|
|
|
ws.once.Do(ws.init)
|
|
|
|
|
|
|
|
ws.Server.OnConnection(func(c websocket.Connection) {
|
2016-09-01 05:01:53 +02:00
|
|
|
connectionListener(c)
|
Update to 4.0.0-alpha.3 - Response Engines, 'inject' the context.JSON/JSONP/Text/Data/Markdown/Render, Read HISTORY.md
## 4.0.0-alpha.2 -> 4.0.0-alpha.3
**New**
A **Response Engine** gives you the freedom to create/change the
render/response writer for
- `context.JSON`
- `context.JSONP`
- `context.XML`
- `context.Text`
- `context.Markdown`
- `context.Data`
- `context.Render("my_custom_type",mystructOrData{},
iris.RenderOptions{"gzip":false,"charset":"UTF-8"})`
- `context.MarkdownString`
- `iris.ResponseString(...)`
**Fix**
- https://github.com/kataras/iris/issues/294
**Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` &
`iris.Config.Render.Template.Charset`, but you can override it at
runtime by passinth a map `iris.RenderOptions` on the `context.Render`
call .
- `iris.Config.IsDevelopment` , before alpha.1 was
`iris.Config.Render.Template.IsDevelopment`
**Websockets changes**
No need to import the `github.com/kataras/iris/websocket` to use the
`Connection` iteral, the websocket moved inside `kataras/iris` , now all
exported variables' names have the prefix of `Websocket`, so the old
`websocket.Connection` is now `iris.WebsocketConnection`.
Generally, no other changes on the 'frontend API', for response engines
examples and how you can register your own to add more features on
existing response engines or replace them, look
[here](https://github.com/iris-contrib/response).
**BAD SIDE**: E-Book is still pointing on the v3 release, but will be
updated soon.
2016-07-18 16:40:42 +02:00
|
|
|
})
|
|
|
|
}
|