1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

Update to 4.2.0 - Configuration changes (big but old way still works, naming changes) & Implement https://github.com/kataras/iris/issues/409

This commit is contained in:
Gerasimos Maropoulos
2016-09-09 08:09:03 +03:00
parent 1a7c79db66
commit f561b7a90d
18 changed files with 1528 additions and 753 deletions

View File

@@ -1,86 +1,10 @@
package iris
import (
"log"
irisWebsocket "github.com/iris-contrib/websocket"
"github.com/kataras/go-websocket"
"github.com/kataras/iris/config"
)
// ---------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------
// --------------------------------Websocket implementation-------------------------------------------------
// Global functions in order to be able to use unlimitted number of websocket servers on each iris station--
// ---------------------------------------------------------------------------------------------------------
// 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
// NewWebsocketServer creates a websocket server and returns it
func NewWebsocketServer(c *config.Websocket) *WebsocketServer {
wsConfig := websocket.Config{
WriteTimeout: c.WriteTimeout,
PongTimeout: c.PongTimeout,
PingPeriod: c.PingPeriod,
MaxMessageSize: c.MaxMessageSize,
BinaryMessages: c.BinaryMessages,
ReadBufferSize: c.ReadBufferSize,
WriteBufferSize: c.WriteBufferSize,
}
wsServer := websocket.New(wsConfig)
upgrader := irisWebsocket.Custom(wsServer.HandleConnection, c.ReadBufferSize, c.WriteBufferSize, false)
srv := &WebsocketServer{Server: wsServer, Config: c, upgrader: upgrader}
return srv
}
// RegisterWebsocketServer registers the handlers for the websocket server
// it's a bridge between station and websocket server
func RegisterWebsocketServer(station FrameworkAPI, server *WebsocketServer, logger *log.Logger) {
c := server.Config
if c.Endpoint == "" {
return
}
websocketHandler := func(ctx *Context) {
if err := server.Upgrade(ctx); err != nil {
if ctx.framework.Config.IsDevelopment {
logger.Printf("Websocket error while trying to Upgrade the connection. Trace: %s", err.Error())
}
ctx.EmitError(StatusBadRequest)
}
}
if c.Headers != nil && len(c.Headers) > 0 { // only for performance matter just re-create the websocketHandler if we have headers to set
websocketHandler = func(ctx *Context) {
for k, v := range c.Headers {
ctx.SetHeader(k, v)
}
if err := server.Upgrade(ctx); err != nil {
if ctx.framework.Config.IsDevelopment {
logger.Printf("Websocket error while trying to Upgrade the connection. Trace: %s", err.Error())
}
ctx.EmitError(StatusBadRequest)
}
}
}
clientSideLookupName := "iris-websocket-client-side"
station.Get(c.Endpoint, websocketHandler)
// 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", contentJavascript, websocket.ClientSource)(clientSideLookupName)
}
// run the ws server
server.Serve()
}
// conversionals
const (
// All is the string which the Emmiter use to send a message to all
@@ -91,16 +15,38 @@ const (
Broadcast = websocket.Broadcast
)
// newUnderlineWsServer returns a new go-websocket.Server from configuration, used internaly by Iris.
func newUnderlineWsServer(c WebsocketConfiguration) websocket.Server {
wsConfig := websocket.Config{
WriteTimeout: c.WriteTimeout,
PongTimeout: c.PongTimeout,
PingPeriod: c.PingPeriod,
MaxMessageSize: c.MaxMessageSize,
BinaryMessages: c.BinaryMessages,
ReadBufferSize: c.ReadBufferSize,
WriteBufferSize: c.WriteBufferSize,
}
return websocket.New(wsConfig)
}
// 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
type (
// 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
Config *config.Websocket
config WebsocketConfiguration
upgrader irisWebsocket.Upgrader
}
)
// NewWebsocketServer returns an empty WebsocketServer, nothing special here.
func NewWebsocketServer() *WebsocketServer {
return &WebsocketServer{}
}
// Upgrade upgrades the HTTP server connection to the WebSocket protocol.
//
// The responseHeader is included in the response to the client's upgrade
@@ -113,6 +59,30 @@ func (s *WebsocketServer) Upgrade(ctx *Context) error {
return s.upgrader.Upgrade(ctx)
}
// Handler is the iris Handler to upgrade the request
// used inside RegisterRoutes
func (s *WebsocketServer) Handler(ctx *Context) {
if err := s.Upgrade(ctx); err != nil {
if ctx.framework.Config.IsDevelopment {
ctx.Log("Websocket error while trying to Upgrade the connection. Trace: %s", err.Error())
}
ctx.EmitError(StatusBadRequest)
}
}
// RegisterTo creates the client side source route and the route path Endpoint with the correct Handler
// receives the websocket configuration and the iris station
func (s *WebsocketServer) RegisterTo(station *Framework, c WebsocketConfiguration) {
s.config = c // save the configuration, we will need that on the .OnConnection
clientSideLookupName := "iris-websocket-client-side"
station.Get(s.config.Endpoint, s.Handler)
// 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", contentJavascript, websocket.ClientSource)(clientSideLookupName)
}
}
// WebsocketConnection is the front-end API that you will use to communicate with the client side
type WebsocketConnection interface {
websocket.Connection
@@ -120,6 +90,17 @@ type WebsocketConnection interface {
// OnConnection this is the main event you, as developer, will work with each of the websocket connections
func (s *WebsocketServer) OnConnection(connectionListener func(WebsocketConnection)) {
// let's initialize here the ws server, the user/dev is free to change its config before this step.
if s.Server == nil {
s.Server = newUnderlineWsServer(s.config)
}
if s.upgrader.Receiver == nil {
s.upgrader = irisWebsocket.Custom(s.Server.HandleConnection, s.config.ReadBufferSize, s.config.WriteBufferSize, false)
// run the ws server
s.Server.Serve()
}
s.Server.OnConnection(func(c websocket.Connection) {
connectionListener(c)
})