Documentation ¶
Overview ¶
Package mudp 关于 UDP 组播的快捷使用包
Example (Send) ¶
package main import ( "log" "github.com/gogorepos/skeleton/net/mudp" ) func main() { // 发送字节 err := mudp.Send("224.224.1.1:35001", []byte("hello")) if err != nil { log.Printf("Send err: %s", err) } // 发送字符串 err = mudp.SendString("224.224.1.1:35001", "hello") if err != nil { log.Printf("Send string err: %s", err) } // 将数据 JSON 序列化后再发送,本质发送的是 JSON 字符串 data := map[string]int{"bob": 98, "小明": 99} err = mudp.SendJson("224.224.1.1:35001", data) if err != nil { log.Printf("Send json err: %s", err) } }
Output:
Example (Server) ¶
package main import ( "fmt" "github.com/gogf/gf/net/gudp" "github.com/gogorepos/skeleton/net/mudp" ) func main() { // 创建 UDP 组播 s := mudp.NewServer("224.224.1.1:35001", func(conn *gudp.Conn) { // 不要忘记关闭连接 defer conn.Close() for { // 接受数据示例 if data, err := conn.Recv(-1); err == nil { if len(data) > 0 { // 发送数据示例 _ = conn.Send([]byte("> " + string(data))) fmt.Printf("Recv: %s", string(data)) } } } }) // 启动服务 s.Run() }
Output: Recv: xxx
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMulticastConn ¶
NewMulticastConn 根据 <address> 创建并返回一个组播连接 *net.UDPConn
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) SetAddress ¶
func (*Server) SetHandler ¶
Click to show internal directories.
Click to hide internal directories.