12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package Nats
- import (
- "Yunlot/Nats/NatsServer"
- "Yunlot/lib"
- "Yunlot/logs"
- "github.com/nats-io/nats-server/v2/server"
- "github.com/nats-io/nats.go"
- "time"
- )
- var YunlotNats *nats.Conn
- // 内部 Nats 通讯
- func Nats_Server_init() {
- opts := &server.Options{
- ServerName: "Yunlot-nats-server",
- Port: 6503,
- }
- // Initialize new server with options
- ns, err := server.NewServer(opts)
- if err != nil {
- panic(any(err))
- }
- // Start the server via goroutine
- go ns.Start()
- // Wait for server to be ready for connections
- if !ns.ReadyForConnections(4 * time.Second) {
- panic(any("not ready for connection"))
- }
- logs.Println("Nats_Server OK!")
- // 连接Nats服务器
- lib.Nats, err = nats.Connect("nats://127.0.0.1:6503", nats.MaxReconnects(10), nats.ReconnectWait(10*time.Second),
- nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
- // handle disconnect error event
- logs.PrintlnError("natsS.DisconnectErrHandler 断开 ", err)
- }),
- nats.ReconnectHandler(func(nc *nats.Conn) {
- // handle reconnect event
- logs.PrintlnError("natsS.ReconnectHandler, 已经重新连接 ")
- }))
- if err != nil {
- logs.Println("natsS 连接失败!")
- panic(any("natsS 连接失败!" + "nats://127.0.0.1:6503"))
- }
- if err != nil {
- logs.Println("nats 连接失败!")
- panic(any(err))
- }
- logs.Println("nats OK!")
- go NatsServer.NatsServer_Device()
- go NatsServer.NatsServer_Product()
- }
|