Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions _example/context-ping/main.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

mcpinger "github.com/Raqbit/mc-pinger"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
<-sig
// NOW cancel
fmt.Println("aborting due to interrupt...")
cancel()
}()

// Create new Pinger with 10 seconds Timeout
pinger := mcpinger.NewContext(ctx, "mc.herobone.de", 25565)
// Get server info
info, err := pinger.Ping()

if err != nil {
log.Println(err)
return
}

// Print server info
fmt.Printf("Description: \"%s\"\n", info.Description.Text)
fmt.Printf("Online: %d/%d\n", info.Players.Online, info.Players.Max)
fmt.Printf("Version: %s\n", info.Version.Name)
}
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

mcpinger "github.com/Raqbit/mc-pinger"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
<-sig
// NOW cancel
fmt.Println("aborting due to interrupt...")
cancel()
}()

// Create new Pinger with 10 seconds Timeout
pinger := mcpinger.New("mc.herobone.de", 25565, mcpinger.WithContext(ctx))

// Get server info
info, err := pinger.Ping()

if err != nil {
log.Println(err)
return
}

// Print server info
fmt.Printf("Description: \"%s\"\n", info.Description.Text)
fmt.Printf("Online: %d/%d\n", info.Players.Online, info.Players.Max)
fmt.Printf("Version: %s\n", info.Version.Name)
}
2 changes: 1 addition & 1 deletion _example/timed-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func main() {
// Create new Pinger with 10 seconds Timeout
pinger := mcpinger.NewTimed("mc.herobone.de", 25565, 10*time.Second)
pinger := mcpinger.New("mc.herobone.de", 25565, mcpinger.WithTimeout(10*time.Second))

// Get server info
info, err := pinger.Ping()
Expand Down
16 changes: 6 additions & 10 deletions pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,17 @@ func New(host string, port uint16, options ...McPingerOption) Pinger {

// NewTimed Creates a new Pinger with specified host & port
// to connect to a minecraft server with Timeout
//
// Deprecated: Use the WithTimeout option & New instead
func NewTimed(host string, port uint16, timeout time.Duration) Pinger {
return &mcPinger{
Host: host,
Port: port,
Timeout: timeout,
}
return New(host, port, WithTimeout(timeout))
}

// NewContext Creates a new Pinger with the given Context
//
// Deprecated: Use the WithContext option & New instead
func NewContext(ctx context.Context, host string, port uint16) Pinger {
return &mcPinger{
Host: host,
Port: port,
Context: ctx,
}
return New(host, port, WithContext(ctx))
}

// McPingerOption instances can be combined when creating a new Pinger
Expand Down