channelconnect method

dynamic channelconnect()

Implementation

channelconnect() async {
  // var businessUId = _bodSquareAppData.companyInformation.value.data?.uid;

  // //log('businessUId: ${kReleaseMode ? "wss://prod-api.bodsquare.com/$businessUId" : "wss://staging-api.bodsquare.com/$businessUId"}');

  // int? counter;
  // List<InternetAddress> result = [];

  // result = await InternetAddress.lookup('example.com');

  // if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  // log("Internet Active");

  final String businessUid = await _storageService.getString('companyUid');
  final bool env = await _storageService.getBool('env');

  try {
    //log('businessUid: $businessUid');
    await Future.delayed(const Duration(seconds: 10));
    channel = kIsWeb
        ? WebSocketChannel.connect(
            kReleaseMode || env
                ? Uri.parse("wss://prod-api.bodsquare.com/$businessUid")
                : Uri.parse("wss://staging-api.bodsquare.com/$businessUid"),
          )
        : IOWebSocketChannel.connect(
            // "ws://178.62.97.4:3004",
            kReleaseMode || env
                ? "wss://prod-api.bodsquare.com/$businessUid"
                : "wss://staging-api.bodsquare.com/$businessUid",

            pingInterval: const Duration(seconds: 10),
          ); //channel IP : Port
    isConnected.value = true;
    channel?.stream.listen(
      (message) async {
        // log(message);
        // log('counter: ${counter ?? 0 + 1}');

        var fina = jsonDecode(message);
        final name = Socketso.fromJson(fina);

        if (name.socketName == 'newMessage' &&
            _conversationsController.singleConversation.value &&
            name.contactUid ==
                _conversationsController.currentContactId.value) {
          final value = WebsocketNewMessage.fromJson(fina);
          final json = jsonEncode(value.data);
          convoMessage = ConversationMessage.fromJson(jsonDecode(json));

          _conversationsController.updateOfflineMessagesFromSocket(
              _conversationsController.currentConversationId.toString(),
              convoMessage ?? const ConversationMessage());

          //log('inside web socket');
          // _conversationsController.getSingleConversation(
          //     _conversationsController.currentConversationId.value);
        } else if (name.socketName == 'updatedConversation') {
          final value = WebsocketUpdatedConversation.fromJson(fina);
          final formattedValue = jsonEncode(value.data);

          GetAllConversationsData conversations =
              GetAllConversationsData.fromJson(jsonDecode(formattedValue));
          _conversationsController.getOfflineConversation(conversations);

          _conversationsController.update();
        }
      },
      onDone: () {
        //log("Web socket is closed");
        isConnected.value = false;
        channelconnect();
      },
      onError: (error) {
        //log(error.toString());
        isConnected.value = false;
        channelconnect();
      },
    );
  } catch (_) {
    //log("error on connecting to websocket.");
    isConnected.value = false;
    channelconnect();
  }
}