websocket临时提交

This commit is contained in:
Liujian
2022-10-26 15:06:29 +08:00
parent 87be0a46fe
commit 01e9f84a2c
5 changed files with 18 additions and 18 deletions

View File

@@ -1,11 +1,13 @@
package http_router package http_router
import ( import (
"net/http"
"strings"
http_complete "github.com/eolinker/apinto/drivers/router/http-router/http-complete" http_complete "github.com/eolinker/apinto/drivers/router/http-router/http-complete"
"github.com/eolinker/apinto/service" "github.com/eolinker/apinto/service"
"github.com/eolinker/eosc/eocontext" "github.com/eolinker/eosc/eocontext"
http_context "github.com/eolinker/eosc/eocontext/http-context" http_context "github.com/eolinker/eosc/eocontext/http-context"
"net/http"
) )
var completeCaller = http_complete.NewHttpCompleteCaller() var completeCaller = http_complete.NewHttpCompleteCaller()
@@ -16,10 +18,11 @@ type Handler struct {
routerName string routerName string
serviceName string serviceName string
finisher Finisher finisher Finisher
service service.IService service service.IService
filters eocontext.IChainPro filters eocontext.IChainPro
disable bool disable bool
websocket bool
} }
func (h *Handler) ServeHTTP(ctx eocontext.EoContext) { func (h *Handler) ServeHTTP(ctx eocontext.EoContext) {
@@ -28,6 +31,11 @@ func (h *Handler) ServeHTTP(ctx eocontext.EoContext) {
if err != nil { if err != nil {
return return
} }
if h.websocket {
if strings.Contains(httpContext.Request().Header().GetHeader("Connection"), "upgrade") && strings.Contains(httpContext.Request().Header().GetHeader("Upgrade"), "websocket") {
}
}
if h.disable { if h.disable {
httpContext.Response().SetStatus(http.StatusNotFound, "") httpContext.Response().SetStatus(http.StatusNotFound, "")

View File

@@ -116,7 +116,6 @@ func NewManager(tf traffic.ITraffic, listenCfg *config.ListensMsg, globalFilters
return m return m
} }
func (m *Manager) FastHandler(port int, ctx *fasthttp.RequestCtx) { func (m *Manager) FastHandler(port int, ctx *fasthttp.RequestCtx) {
log.Debug("fastHandler:", port)
httpContext := http_context.NewContext(ctx, port) httpContext := http_context.NewContext(ctx, port)
r, has := m.matcher.Match(port, httpContext.Request()) r, has := m.matcher.Match(port, httpContext.Request())
if !has { if !has {
@@ -124,6 +123,7 @@ func (m *Manager) FastHandler(port int, ctx *fasthttp.RequestCtx) {
httpContext.SetCompleteHandler(notFound) httpContext.SetCompleteHandler(notFound)
m.globalFilters.Chain(httpContext, completeCaller) m.globalFilters.Chain(httpContext, completeCaller)
} else { } else {
log.Debug("match has:", port) log.Debug("match has:", port)
r.ServeHTTP(httpContext) r.ServeHTTP(httpContext)
} }

View File

@@ -3,11 +3,12 @@ package http_context
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/eolinker/eosc/utils/config"
"net" "net"
"strings" "strings"
"time" "time"
"github.com/eolinker/eosc/utils/config"
fasthttp_client "github.com/eolinker/apinto/node/fasthttp-client" fasthttp_client "github.com/eolinker/apinto/node/fasthttp-client"
eoscContext "github.com/eolinker/eosc/eocontext" eoscContext "github.com/eolinker/eosc/eocontext"
@@ -101,7 +102,6 @@ func (ctx *Context) SetFinish(handler eoscContext.FinishHandler) {
} }
func (ctx *Context) Scheme() string { func (ctx *Context) Scheme() string {
return string(ctx.fastHttpRequestCtx.Request.URI().Scheme()) return string(ctx.fastHttpRequestCtx.Request.URI().Scheme())
} }
@@ -169,6 +169,7 @@ func (ctx *Context) Request() http_service.IRequestReader {
//NewContext 创建Context //NewContext 创建Context
func NewContext(ctx *fasthttp.RequestCtx, port int) *Context { func NewContext(ctx *fasthttp.RequestCtx, port int) *Context {
id := uuid.NewV4() id := uuid.NewV4()
requestID := id.String() requestID := id.String()
newCtx := &Context{ newCtx := &Context{

View File

@@ -149,6 +149,7 @@ func (ctx *Context) WithValue(key, val interface{}) {
var upgrader = websocket.FastHTTPUpgrader{} var upgrader = websocket.FastHTTPUpgrader{}
func (ctx *Context) Upgrade() { func (ctx *Context) Upgrade() {
err := upgrader.Upgrade(ctx.fastHttpRequestCtx, func(serverConn *websocket.Conn) { err := upgrader.Upgrade(ctx.fastHttpRequestCtx, func(serverConn *websocket.Conn) {
defer serverConn.Close() defer serverConn.Close()
clientConn, ok := <-ctx.connChan clientConn, ok := <-ctx.connChan

View File

@@ -1,10 +0,0 @@
package websocket_context
import "errors"
var (
ErrorNotForm = errors.New("contentType is not Form")
ErrorNotMultipart = errors.New("contentType is not Multipart")
ErrorNotAllowRaw = errors.New("contentType is not allow Raw")
ErrorNotSend = errors.New("not send")
)