diff --git a/bin/acc_conn-example.go b/bin/acc_conn-example.go new file mode 100644 index 0000000..ba5cf13 --- /dev/null +++ b/bin/acc_conn-example.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "github.com/spf13/viper" + "joylink.club/bj-rtsts-server/bin/config" + "joylink.club/bj-rtsts-server/third_party/message" + "joylink.club/bj-rtsts-server/third_party/udp" + "log/slog" + "time" +) + +const config_name = "example" + +var exampleConfig config.ExampleConfig + +func initConfig() { + cnf := viper.New() + cnf.SetConfigName(config_name) + cnf.SetConfigType("yml") + cnf.AddConfigPath("./config/") + cnf.AddConfigPath(".") + err := cnf.ReadInConfig() + if err != nil { + panic(fmt.Errorf("读取配置文件错误: %w", err)) + } + err = cnf.Unmarshal(&exampleConfig) + if err != nil { + panic(fmt.Errorf("解析配置文件错误: %w", err)) + } + slog.Info("成功加载配置", "config", exampleConfig) +} +func main() { + initConfig() + testAcc() +} +func testAcc() { + var client udp.UdpClient + defer func() { + if client != nil { + client.Close() + } + slog.Info("连接关闭") + }() + client = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Acc.RemoteIp, exampleConfig.Acc.RemotePort)) + ac := message.Accelerometer{Acc: 1} + var index int64 + for { + err := client.Send(ac.Encode()) + index++ + if index%10 == 0 { + slog.Info(fmt.Sprintf("发送数据,时间戳:%v,发送次数:%v", time.Now().Unix(), index)) + } + + if err != nil { + slog.Error("发送数据失败", "err", err) + } + time.Sleep(time.Millisecond * 200) + } + +} diff --git a/bin/config/example.yml b/bin/config/example.yml new file mode 100644 index 0000000..c3fca56 --- /dev/null +++ b/bin/config/example.yml @@ -0,0 +1,3 @@ +acc: + remoteIp: "127.0.0.1" + remotePort: 1234 \ No newline at end of file diff --git a/bin/config/example_config.go b/bin/config/example_config.go new file mode 100644 index 0000000..b59499f --- /dev/null +++ b/bin/config/example_config.go @@ -0,0 +1,9 @@ +package config + +type ExampleConfig struct { + Acc acc +} +type acc struct { + RemoteIp string + RemotePort int +}