startSocket method

void startSocket(
  1. String token
)

Implementation

void startSocket(String token) {
  socket = Get.put(socket_io.io(
      socketHost,
      OptionBuilder()
          .setTransports(['websocket'])
          .disableAutoConnect()
          .setQuery({'token': 'Bearer $token'})
          .build()));
  socket.connect();

  socket.onConnect((_) {});

  //LISTEN
  socket.on("move_approval", (_) async {
    NotiManager n = Get.find<NotiManager>();

    await n.flutterLocalNotificationsPlugin
        .show(1, "test", "testBody", n.platformChannelSpecifics);
  });

  socket.on("facility_approval", (data) {
    //dynamic json = jsonDecode(utf8.decode(data));
  });

  socket.on("assemble_command", (data) {
    dynamic json = jsonDecode(utf8.decode(data));

    prefs.write("assemble_location", json["beacon_id"]);
    prefs.write("assemble_visible", true);
    debugPrint("assemble arrived");
    Get.to(UserAssemble(location: prefs.read("assemble_location")));
    Snack.warnTop("소집 지시", "소집 지시가 내려왔습니다.");
  });

  socket.on("to_cohort", (data) async {
    dynamic json = jsonDecode(utf8.decode(data));
    bool flag = false;
    String tag = prefs.read("tag");
    for (int i = 0; i < json["tag"].length; i++) {
      if (json["tag"][i] == tag) {
        flag = true;
        break;
      }
    }
    if (flag) {
      GetStorage store = GetStorage();
      UserController u = Get.find<UserController>();
      PlaceController p = Get.find<PlaceController>();
      store.writeIfNull("state", "정상");

      await u.currentPosition(store.read("tag"));
      await p.positionAllInfo();
      CohortController c = Get.find<CohortController>();
      await c.timeTableAllInfo();

      Snack.top("로그인 시도", "성공");
      Get.to(CohortMain(
        location: store.read("recent_place_name") ?? "error in LoginPage",
        state: store.read("state") ?? "",
      ));
    }
  });

  socket.on("to_normal", (_) async {
    GetStorage store = GetStorage();
    UserController u = Get.find<UserController>();
    store.write("state", "정상");

    await u.currentPosition(store.read("recent_place_name"));

    Get.to(UserMain(
      location: store.read("recent_place_name"),
      state: store.read("state"),
    ));
    Snack.top("평시로 전환", "코호트 상황이 풀렸습니다.");
  });

  socket.on("contact_alert", (_) {
    Snack.warnTop("접촉 경고", "코호트 대상자와 접촉하였습니다.");
  });
}