Implement websocket gateway

This commit is contained in:
LittleSheep 2024-11-02 13:09:52 +08:00
parent 6bb8eab3fc
commit ea150608b4

View File

@ -1,11 +1,16 @@
package ws package ws
import ( import (
"context"
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
"git.solsynth.dev/hypernet/nexus/pkg/nex" "git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"github.com/gofiber/contrib/websocket" "github.com/gofiber/contrib/websocket"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -42,10 +47,9 @@ func Listen(c *websocket.Conn) {
packet.Endpoint = val packet.Endpoint = val
} }
/*
service := directory.GetServiceInstanceByType(packet.Endpoint) service := directory.GetServiceInstanceByType(packet.Endpoint)
if service == nil { if service == nil {
_ = c.WriteMessage(mt, nex.NetworkPackage{ _ = c.WriteMessage(mt, nex.WebSocketPackage{
Action: "error", Action: "error",
Message: "service not found", Message: "service not found",
}.Marshal()) }.Marshal())
@ -53,7 +57,7 @@ func Listen(c *websocket.Conn) {
} }
pc, err := service.GetGrpcConn() pc, err := service.GetGrpcConn()
if err != nil { if err != nil {
_ = c.WriteMessage(mt, nex.NetworkPackage{ _ = c.WriteMessage(mt, nex.WebSocketPackage{
Action: "error", Action: "error",
Message: fmt.Sprintf("unable to connect to service: %v", err.Error()), Message: fmt.Sprintf("unable to connect to service: %v", err.Error()),
}.Marshal()) }.Marshal())
@ -61,19 +65,18 @@ func Listen(c *websocket.Conn) {
} }
sc := proto.NewStreamServiceClient(pc) sc := proto.NewStreamServiceClient(pc)
_, err = sc.EmitStreamEvent(context.Background(), &proto.StreamEventRequest{ _, err = sc.PushStream(context.Background(), &proto.PushStreamRequest{
Event: packet.Action, UserId: lo.ToPtr(uint64(user.ID)),
UserId: uint64(user.ID), ClientId: lo.ToPtr(clientId),
ClientId: uint64(clientId), Body: packet.Marshal(),
Payload: packet.RawPayload(),
}) })
if err != nil { if err != nil {
_ = c.WriteMessage(mt, nex.NetworkPackage{ _ = c.WriteMessage(mt, nex.WebSocketPackage{
Action: "error", Action: "error",
Message: fmt.Sprintf("unable send message to service: %v", err.Error()), Message: fmt.Sprintf("unable send message to service: %v", err.Error()),
}.Marshal()) }.Marshal())
continue continue
}*/ }
} }
// Pop connection // Pop connection