mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
add content negotiation feature, add context.ReadYAML and fix https://github.com/kataras/neffos/issues/1#issuecomment-515698536
Former-commit-id: 9753e3e45c7c24788b97814d3ecfb4b03f5ff414
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/neffos"
|
||||
@@ -138,6 +140,14 @@ func SetDefaultUnmarshaler(fn func(data []byte, v interface{}) error) {
|
||||
// IDGenerator is an iris-specific IDGenerator for new connections.
|
||||
type IDGenerator func(context.Context) string
|
||||
|
||||
func wrapIDGenerator(idGen IDGenerator) func(ctx context.Context) neffos.IDGenerator {
|
||||
return func(ctx context.Context) neffos.IDGenerator {
|
||||
return func(w http.ResponseWriter, r *http.Request) string {
|
||||
return idGen(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handler returns an Iris handler to be served in a route of an Iris application.
|
||||
// Accepts the neffos websocket server as its first input argument
|
||||
// and optionally an Iris-specific `IDGenerator` as its second one.
|
||||
@@ -151,19 +161,19 @@ func Handler(s *neffos.Server, IDGenerator ...IDGenerator) context.Handler {
|
||||
if ctx.IsStopped() {
|
||||
return
|
||||
}
|
||||
Upgrade(ctx, idGen(ctx), s)
|
||||
Upgrade(ctx, idGen, s)
|
||||
}
|
||||
}
|
||||
|
||||
// Upgrade upgrades the request and returns a new websocket Conn.
|
||||
// Use `Handler` for higher-level implementation instead.
|
||||
func Upgrade(ctx context.Context, customID string, s *neffos.Server) *neffos.Conn {
|
||||
func Upgrade(ctx context.Context, idGen IDGenerator, s *neffos.Server) *neffos.Conn {
|
||||
conn, _ := s.Upgrade(ctx.ResponseWriter(), ctx.Request(), func(socket neffos.Socket) neffos.Socket {
|
||||
return &socketWrapper{
|
||||
Socket: socket,
|
||||
ctx: ctx,
|
||||
}
|
||||
}, customID)
|
||||
}, wrapIDGenerator(idGen)(ctx))
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ type (
|
||||
GorillaDialerOptions = gorilla.Options
|
||||
// GobwasDialerOptions is just an alias for the `gorilla/websocket.Dialer` struct type.
|
||||
GobwasDialerOptions = gobwas.Options
|
||||
// GobwasHeader is an alias to the adapter that allows the use of `http.Header` as
|
||||
// handshake headers.
|
||||
GobwasHeader = gobwas.Header
|
||||
|
||||
// Conn describes the main websocket connection's functionality.
|
||||
// Its `Connection` will return a new `NSConn` instance.
|
||||
|
||||
Reference in New Issue
Block a user