tsviewer/service/service.go

42 lines
739 B
Go
Raw Normal View History

2019-01-14 20:07:11 +00:00
package service
import (
2019-01-17 00:35:51 +00:00
"fmt"
"log"
"time"
2019-01-14 20:07:11 +00:00
2019-01-17 00:35:51 +00:00
"git.cliffbreak.de/Cliffbreak/tsviewer/config"
2019-01-14 20:07:11 +00:00
ts3 "github.com/multiplay/go-ts3"
)
type Service struct {
2019-01-15 00:46:39 +00:00
TSClient *ts3.Client
2019-01-14 20:07:11 +00:00
}
func New(config config.Config) (*Service, error) {
2019-01-17 00:35:51 +00:00
client, err := ts3.NewClient(fmt.Sprintf("%s:%d", config.ServerTS.IP, config.ServerTS.PortQuery))
2019-01-14 20:07:11 +00:00
if err != nil {
return nil, err
}
if err := client.Login(config.User.Name, config.User.Password); err != nil {
return nil, err
}
2019-01-17 00:35:51 +00:00
if err := client.UsePort(int(config.ServerTS.PortServer)); err != nil {
2019-01-15 00:46:39 +00:00
return nil, err
}
2019-01-17 00:35:51 +00:00
go func() {
for true {
time.Sleep(time.Second * 150)
client.Server.Version()
log.Println("keep alive")
}
}()
2019-01-14 20:07:11 +00:00
return &Service{
2019-01-15 00:46:39 +00:00
TSClient: client,
2019-01-14 20:07:11 +00:00
}, nil
}