Files
lone-services/bff/internal/wsClient/client.go
T
gjs 6b8500ac5e
CI / changes (push) Successful in 1m29s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 29s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 1s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / product (push) Successful in 1s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 20s
CI / support (push) Successful in 1s
CI / task (push) Successful in 0s
CI / user (push) Successful in 0s
CI / wecom (push) Successful in 1s
ws
2026-10-08 14:14:39 +08:00

81 lines
2.4 KiB
Go

package wsClient
import (
"context"
"lone-services/bff/internal/request"
"lone-services/bff/internal/wsproxy"
percutaneous "lone-services/rpc/percutaneous/pb"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc"
"google.golang.org/protobuf/reflect/protoreflect"
)
// ws handler
func PercutaneousLogin(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
wsHandler := request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("ws request: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(ctx context.Context) (grpc.ClientStream, error) {
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
return streamCli.WsLoginStream(ctx)
},
func() protoreflect.ProtoMessage { return &percutaneous.WsStreamRequest{} },
)
proxyHandler(w, r)
})
return wsHandler
}
func PercutaneousAddUser(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
wsHandler := request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("ws request: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(ctx context.Context) (grpc.ClientStream, error) {
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
return streamCli.WsAddUserStream(ctx)
},
func() protoreflect.ProtoMessage { return &percutaneous.WsStreamRequest{} },
)
proxyHandler(w, r)
})
return wsHandler
}
func PercutaneousUsed(rpcMgr *wsproxy.RpcClientManager) http.HandlerFunc {
wsHandler := request.UserReadMiddleware(func(w http.ResponseWriter, r *http.Request) {
cli, ready := rpcMgr.GetClient()
if !ready {
logx.Error("ws request: rpc client not ready, return 503")
http.Error(w, "rpc service unavailable", http.StatusServiceUnavailable)
return
}
proxyHandler := wsproxy.NewWsProxyHandler(
func(ctx context.Context) (grpc.ClientStream, error) {
streamCli := percutaneous.NewPercutaneousClient(cli.Conn())
return streamCli.WsUsedStream(ctx)
},
func() protoreflect.ProtoMessage { return &percutaneous.WsStreamRequest{} },
)
proxyHandler(w, r)
})
return wsHandler
}