Currently this supports only the server browser api. It is possible to retrieve data from the masterservers as well as the server information from the game servers.
In order to download the latest released version, execute:
go get github.com/jxsl13/twapi@latest
In order to download the latest development version, execute:
go get github.com/jxsl13/twapi@master
- browser
- compression
- config
- econ
- network
- protocol
package main
import (
"fmt"
"github.com/jxsl13/twapi/browser"
)
func main() {
// fetch all server infos (that respond within 16 seconds)
// if no servers responded, this list will be empty.
infos, err := browser.ServerInfos()
if err != nil {
panic(err)
}
for _, info := range infos {
fmt.Println(info)
}
// fetches the specified server's players, server name, etc.
// if no answer is received within 16 seconds, this function returns
// an error
infos, err := browser.GetServerInfosOf("89.163.148.121:8305")
if err != nil {
panic(err)
} else {
fmt.Println(infos[0])
}
}
package main
import (
"fmt"
"net"
"time"
"github.com/jxsl13/twapi/browser"
)
func main() {
addr, err := net.ResolveUDPAddr("udp", "master1.teeworlds.com:8283")
if err != nil {
panic(err)
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
panic(err)
}
defer conn.Close()
err = browser.RequestToken(conn)
if err != nil {
panic(err)
}
conn.SetDeadline(time.Now().Add(5 * time.Second))
buffer := [1500]byte{}
bufSlice := buffer[:]
read, err := conn.Read(bufSlice)
if err != nil {
panic(err)
}
bufSlice = bufSlice[:read]
fmt.Printf("read: %d bytes from %s\n", read, conn.RemoteAddr().String())
// create toke from response
token, err := browser.ParseToken(bufSlice)
if err != nil {
panic(err)
}
// reset slice
bufSlice = bufSlice[:1500]
err = browser.Request("serverlist", token, conn)
if err != nil {
panic(err)
}
// timeout after 5 secods
// should not return an error
conn.SetDeadline(time.Now().Add(5 * time.Second))
// wait for response or time out
read, err = conn.Read(bufSlice)
bufSlice = bufSlice[:read]
fmt.Printf("read: %d bytes from %s\n", read, conn.RemoteAddr().String())
serverList, err := browser.ParseServerList(bufSlice)
if err != nil {
panic(err)
}
for _, server := range serverList {
fmt.Printf("Server: %s\n", server.String())
}
}
package main
import (
"fmt"
"net"
"time"
"github.com/jxsl13/twapi/browser"
)
func main() {
addr, err := net.ResolveUDPAddr("udp", "master1.teeworlds.com:8283")
if err != nil {
panic(err)
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
panic(err)
}
defer conn.Close()
// token request packet
tokenReq := browser.NewTokenRequestPacket()
// send request
written, err := conn.Write(tokenReq)
if err != nil {
panic(err)
}
fmt.Printf("written: %d bytes to %s\n", written, conn.RemoteAddr().String())
conn.SetDeadline(time.Now().Add(5 * time.Second))
buffer := [1500]byte{}
bufSlice := buffer[:]
read, err := conn.Read(bufSlice)
if err != nil {
panic(err)
}
bufSlice = bufSlice[:read]
fmt.Printf("read: %d bytes from %s\n", read, conn.RemoteAddr().String())
// create toke from response
token, err := browser.ParseToken(bufSlice)
if err != nil {
panic(err)
}
// reset slice
bufSlice = bufSlice[:1500]
// create a new request from token
serverListReq, err := browser.NewServerListRequestPacket(token)
// Send server list request
written, err = conn.Write(serverListReq)
if err != nil {
panic(err)
}
fmt.Printf("written: %d bytes to %s\n", written, conn.RemoteAddr().String())
// timeout after 5 secods
// should not return an error
conn.SetDeadline(time.Now().Add(5 * time.Second))
// wait for response or time out
read, err = conn.Read(bufSlice)
bufSlice = bufSlice[:read]
fmt.Printf("read: %d bytes from %s\n", read, conn.RemoteAddr().String())
serverList, err := browser.ParseServerList(bufSlice)
if err != nil {
panic(err)
}
for _, server := range serverList {
fmt.Printf("Server: %s\n", server.String())
}
}