add reconnect to websocket, add get world json object

This commit is contained in:
lbenedar
2026-04-13 17:40:59 +03:00
parent cd204ebcf5
commit 5bb592ea8a
14 changed files with 321 additions and 103 deletions

View File

@@ -37,14 +37,7 @@ func (tr *FoundryTransport) LaunchWorld(worldName string) error {
}
func (tr *FoundryTransport) ConnectToFoundry() error {
var err error
ok, err := tr.Http.GetSessionToken()
if err != nil {
return err
}
if tr.Http.Password != "" && !ok {
if tr.Http.Password != "" {
err := tr.Authenticate()
if err != nil {
return err
@@ -56,7 +49,7 @@ func (tr *FoundryTransport) ConnectToFoundry() error {
}
func (tr *FoundryTransport) Authenticate() error {
if tr.Http.SessionID == "" {
if tr.Http.SessionID == nil {
err := tr.Http.GetSessionId()
if err != nil {
return err
@@ -74,13 +67,13 @@ func (tr *FoundryTransport) Authenticate() error {
return err
}
return tr.CheckAuthRespAnswer(resp.Body, contentLen)
return tr.CheckAuthResponse(resp.Body, contentLen)
}
func (tr *FoundryTransport) CheckAuthRespAnswer(r io.Reader, respLength int) error {
func (tr *FoundryTransport) CheckAuthResponse(body io.Reader, respLength int) error {
authBody := make([]byte, respLength)
_, err := r.Read(authBody)
_, err := body.Read(authBody)
if err != nil && err != io.EOF {
return err
}
@@ -92,8 +85,50 @@ func (tr *FoundryTransport) CheckAuthRespAnswer(r io.Reader, respLength int) err
return nil
}
func (tr *FoundryTransport) LogInToWorld() error {
if tr.Http.SessionID == nil {
err := tr.Http.GetSessionId()
if err != nil {
return err
}
}
// TODO: make userid and password from tr object
resp, err := tr.Http.PostJoinWorld("YpMHZge0dxBZS5Cm", "")
if err != nil {
return err
}
defer resp.Body.Close()
contentLen, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return err
}
return tr.CheckLoginResponse(resp.Body, contentLen)
// {"userid":"YpMHZge0dxBZS5Cm","password":"","action":"join"}
}
func (tr *FoundryTransport) CheckLoginResponse(body io.Reader, respLength int) error {
joinBody := make([]byte, respLength)
_, err := body.Read(joinBody)
if err != nil && err != io.EOF {
return err
}
tr.Logger.Debug("Received response", "response", string(joinBody))
if !strings.Contains(string(joinBody), "\"status\":\"success\"") {
return ErrorAuthPassWrong
}
// tr.IsAuth = true
return nil
}
func (tr *FoundryTransport) HasSessionId() bool {
return tr.Http.SessionID != ""
return tr.Http.SessionID != nil
}
func (tr *FoundryTransport) InitSessionId() error {