Compare commits
2 Commits
8d28ac1d59
...
2569265a1f
Author | SHA1 | Date | |
---|---|---|---|
|
2569265a1f | ||
|
06f8445a98 |
61
bin/acc_conn-example.go
Normal file
61
bin/acc_conn-example.go
Normal file
@ -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)
|
||||
}
|
||||
|
||||
}
|
3
bin/config/example.yml
Normal file
3
bin/config/example.yml
Normal file
@ -0,0 +1,3 @@
|
||||
acc:
|
||||
remoteIp: "127.0.0.1"
|
||||
remotePort: 1234
|
9
bin/config/example_config.go
Normal file
9
bin/config/example_config.go
Normal file
@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
type ExampleConfig struct {
|
||||
Acc acc
|
||||
}
|
||||
type acc struct {
|
||||
RemoteIp string
|
||||
RemotePort int
|
||||
}
|
Loading…
Reference in New Issue
Block a user