Skip to content

Instantly share code, notes, and snippets.

@spy16
Last active July 1, 2024 15:28
Show Gist options
  • Save spy16/15e6f5f716d5d2160f90b31f5ceaace9 to your computer and use it in GitHub Desktop.
Save spy16/15e6f5f716d5d2160f90b31f5ceaace9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/panjf2000/gnet/v2"
)
func main() {
echo := &echoServer{}
log.Fatal(gnet.Run(echo,
"udp://239.0.0.1:9999",
gnet.WithNumEventLoop(2), // running 2 loops.
))
}
type echoServer struct {
gnet.BuiltinEventEngine
}
func (es *echoServer) OnTraffic(c gnet.Conn) gnet.Action {
data, err := c.Next(-1)
if err != nil {
log.Printf("faield to read: %v", err)
} else {
// this happens 2 times for every single packet i send to the above
// multicast address.
fmt.Println(string(data))
}
return gnet.None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment