package client import ( "encoding/json" "fmt" "native-render/model" "os" "os/signal" "time" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" "github.com/aceld/zinx/znet" "go.mongodb.org/mongo-driver/bson/primitive" ) type AppOption struct { DeviceId string RoomId int } var GAppOption *AppOption = nil func Connect() { client := znet.NewWsClient("127.0.0.1", 9000) //添加首次建立链接时的业务 //注册收到服务器消息业务路由 client.SetOnConnStart(DoClientConnectedBegin) //启动客户端client client.Start() // close c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) sig := <-c fmt.Println("===exit===", sig) } // 创建连接的时候执行 func DoClientConnectedBegin(conn ziface.IConnection) { nativeRender := &model.NativeRender{ Id: primitive.NewObjectID().Hex(), RoomId: GAppOption.RoomId, CreateTime: time.Now(), } data, err := json.Marshal(&nativeRender) if err != nil { zlog.Error(err) } err = conn.SendMsg(NATIVE_RENDER_REGISTER, data) fmt.Println(err) }