main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "native-render/model"
  6. "os"
  7. "os/signal"
  8. "time"
  9. "github.com/aceld/zinx/ziface"
  10. "github.com/aceld/zinx/zlog"
  11. "github.com/aceld/zinx/znet"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. )
  14. type AppOption struct {
  15. DeviceId string
  16. RoomId int
  17. }
  18. var GAppOption *AppOption = nil
  19. func Connect() {
  20. client := znet.NewWsClient("127.0.0.1", 9000)
  21. //添加首次建立链接时的业务
  22. //注册收到服务器消息业务路由
  23. client.SetOnConnStart(DoClientConnectedBegin)
  24. //启动客户端client
  25. client.Start()
  26. // close
  27. c := make(chan os.Signal, 1)
  28. signal.Notify(c, os.Interrupt, os.Kill)
  29. sig := <-c
  30. fmt.Println("===exit===", sig)
  31. }
  32. // 创建连接的时候执行
  33. func DoClientConnectedBegin(conn ziface.IConnection) {
  34. nativeRender := &model.NativeRender{
  35. Id: primitive.NewObjectID().Hex(),
  36. RoomId: GAppOption.RoomId,
  37. CreateTime: time.Now(),
  38. }
  39. data, err := json.Marshal(&nativeRender)
  40. if err != nil {
  41. zlog.Error(err)
  42. }
  43. err = conn.SendMsg(NATIVE_RENDER_REGISTER, data)
  44. fmt.Println(err)
  45. }