add reconnect to websocket, add get world json object
This commit is contained in:
@@ -3,18 +3,19 @@ package foundry
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/actions"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/requests"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/transport"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
||||
)
|
||||
|
||||
var (
|
||||
ListenIsDone = errors.New("Listen for websocket data in foundry is stopped")
|
||||
ChannelIsClosed = errors.New("Channel is closed")
|
||||
ListenIsDone = errors.New("Listen for websocket data in foundry is stopped")
|
||||
ReconnectToWebSocket = errors.New("Reconnect to websocket")
|
||||
ChannelIsClosed = errors.New("Channel is closed")
|
||||
|
||||
CloseTimeoutExceed = errors.New("Timeout of websocket close is exceed")
|
||||
)
|
||||
@@ -24,7 +25,7 @@ type FoundryApi struct {
|
||||
transport *transport.FoundryTransport
|
||||
IsAvailable bool
|
||||
|
||||
Logger *slog.Logger
|
||||
// Logger *slog.Logger
|
||||
Status types.FoundryStatus
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
@@ -61,6 +62,7 @@ func (foundry *FoundryApi) ServeWebSocket() error {
|
||||
case message, ok := <-wsChannels.Msg():
|
||||
if !ok {
|
||||
foundry.transport.Logger.Debug("Read channel is closed", "type", types.WebSocketCode)
|
||||
foundry.transport.Http.SessionID = nil
|
||||
return ChannelIsClosed
|
||||
}
|
||||
foundry.transport.Logger.Debug("Data has been received\n", "msg", message.Code, "type", types.WebSocketCode) //, "msg", message.MsgJson)
|
||||
@@ -80,6 +82,7 @@ func (foundry *FoundryApi) ServeWebSocket() error {
|
||||
continue
|
||||
}
|
||||
|
||||
foundry.transport.Logger.Debug("RespServerChangeCode", "data", data)
|
||||
if data == nil {
|
||||
continue
|
||||
}
|
||||
@@ -100,13 +103,18 @@ func (foundry *FoundryApi) ServeWebSocket() error {
|
||||
if errors.As(err, &foundryErr) {
|
||||
if foundryErr.IsFatal {
|
||||
foundry.transport.CloseWebSocketConn()
|
||||
foundry.transport.Http.SessionID = nil
|
||||
return foundryErr
|
||||
}
|
||||
foundry.transport.Logger.Warn("Got error when listening or served", "err", err.Error(), "type", foundryErr.Type, "direction", foundryErr.Direction)
|
||||
}
|
||||
case <-wsChannels.Done():
|
||||
foundry.transport.CloseWebSocketConn()
|
||||
foundry.transport.Http.SessionID = nil
|
||||
return ListenIsDone
|
||||
case <-wsChannels.Reconnect():
|
||||
foundry.transport.CloseWebSocketConn()
|
||||
return ReconnectToWebSocket
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,11 +159,11 @@ func (foundry *FoundryApi) ConnectToWebSocket() (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
err = foundry.transport.ConnectToFoundry()
|
||||
if err != nil {
|
||||
return false, err
|
||||
err = foundry.transport.ConnectToFoundry()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
err = foundry.transport.InitWebSocketConnection()
|
||||
@@ -184,6 +192,16 @@ func (foundry *FoundryApi) StartListenFoundry() {
|
||||
for {
|
||||
ok, err := foundry.ConnectToWebSocket()
|
||||
if err != nil {
|
||||
if errors.Is(err, ReconnectToWebSocket) {
|
||||
if foundry.transport.ReconnectNum >= foundry.transport.ReconnectNumMax {
|
||||
return
|
||||
}
|
||||
time.Sleep(foundry.transport.ReconnectTimeout)
|
||||
foundry.transport.ReconnectNum++
|
||||
foundry.transport.Logger.Info("Reconnecting to WebSocket", "times", foundry.transport.ReconnectNum)
|
||||
continue
|
||||
}
|
||||
|
||||
foundry.transport.Logger.Error("Error raised", "err", err.Error())
|
||||
|
||||
if ok {
|
||||
@@ -199,3 +217,20 @@ func (foundry *FoundryApi) StartListenFoundry() {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (foundry *FoundryApi) Test() ([]byte, error) {
|
||||
wsMsg := types.NewWsMessage("world", foundry.transport.CurrWsId)
|
||||
foundry.transport.CurrWsId++
|
||||
|
||||
answer, err := foundry.transport.HandleWebsocketRequest(wsMsg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// foundry.transport.Logger.Info("World data", "answer", string(answer))
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
// TODO: remove
|
||||
func (foundry *FoundryApi) GetHTTP() *requests.FoundryHttpRequest {
|
||||
return foundry.transport.Http
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user