Skip to content

Commit

Permalink
✨ Ping
Browse files Browse the repository at this point in the history
  • Loading branch information
MateoGreil committed May 7, 2024
1 parent 4c5da9d commit 97adab2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/protocols/socket/response.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package socket

type Response struct {
Status bool `json:"status"`
}

type LoginResponse struct {
Status bool `json:"status"`
StreamSessionId string `json:"streamSessionId"`
Expand Down
31 changes: 31 additions & 0 deletions xapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xapi

import (
"fmt"
"time"

socket "github.com/MateoGreil/xapi-go/internal/protocols/socket"
stream "github.com/MateoGreil/xapi-go/internal/protocols/stream"
Expand All @@ -16,6 +17,7 @@ type client struct {

const (
websocketBaseURL = "wss://ws.xtb.com"
pingInterval = 5 * time.Minute
)

func NewClient(userId string, password string, connectionType string) (*client, error) {
Expand Down Expand Up @@ -52,10 +54,39 @@ func NewClient(userId string, password string, connectionType string) (*client,
streamConn: streamConn,
streamSessionId: streamSessionId,
}
go c.pingSocket()
go c.pingStream()

return c, nil
}

func (c *client) pingSocket() {
for {
request := socket.Request{
Command: "ping",
Arguments: nil,
}
c.conn.WriteJSON(request)
response := socket.Response{}
err := c.conn.ReadJSON(&response)
if err != nil {
fmt.Println(err.Error())
}
time.Sleep(pingInterval)
}
}

func (c *client) pingStream() {
for {
request := stream.Request{
Command: "ping",
StreamSessionId: c.streamSessionId,
}
c.streamConn.WriteJSON(request)
time.Sleep(pingInterval)
}
}

func login(conn *websocket.Conn, userId string, password string) (string, error) {
request := socket.Request{
Command: "login",
Expand Down

0 comments on commit 97adab2

Please sign in to comment.