mudp

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2021 License: GPL-3.0 Imports: 11 Imported by: 0

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 NewConn

func NewConn(address string) (*gudp.Conn, error)

NewConn 根据 <address> 创建 UDP 连接。 注意:该 *gudp.Conn 只能发送数据,无法接受数据

func NewMulticastConn

func NewMulticastConn(address string) (*net.UDPConn, error)

NewMulticastConn 根据 <address> 创建并返回一个组播连接 *net.UDPConn

func Send

func Send(address string, data []byte, retry ...gudp.Retry) error

Send 向组播地址 <address> 发送信息

func SendJson

func SendJson(address string, data interface{}) error

SendJson 将 <data> JSON 序列化后发送到组播地址 <address>

func SendString

func SendString(address, data string) error

SendString 向组播地址 <address> 发送字符串信息

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

func GetServer

func GetServer(name ...interface{}) *Server

func NewServer

func NewServer(address string, handler func(*gudp.Conn), name ...string) *Server

NewServer 根据 <address> 和处理函数 <handler> 创建并返回一个 *Server

func (*Server) Close

func (s *Server) Close() error

func (*Server) Run

func (s *Server) Run() error

func (*Server) SetAddress

func (s *Server) SetAddress(address string)

func (*Server) SetHandler

func (s *Server) SetHandler(handler func(*gudp.Conn))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL