티스토리 뷰

Flutter

flutter - SecureSocket

바람사탕 2022. 6. 24. 16:16
반응형
import 'dart:async';
import 'dart:convert';
import 'dart:io';

final garageExternalIP = '192.168.86.40';
const garagePort = 1416;
const garageEventType = 'event_type';
const garageResponse = 'response';
const garageEventDelay = 'delay';
const garageOpenEvent = 1;
const garageCloseEvent = 2;
const garageTriggerEvent = 3;
const garageIsOpenEvent = 4;
const garageCloseInEvent = 5;
const garageOpenForEvent = 6;



bool _onBadCertificate(X509Certificate cert) => true;

static Future<bool> _sendRequest(int type,
      {int delay, bool returnResponse = false}) async {
    try {
      final connection = await SecureSocket.connect(
          garageExternalIP, garagePort,
          context: _context, onBadCertificate: _onBadCertificate);
      final request = {
        garageEventType: type,
      };
      if (delay != null) {
        request[garageEventDelay] = delay;
      }
      connection.write(json.encode(request));
      if (returnResponse) {
        final completer = Completer<bool>();
        connection.listen((r) {
          final Map response = json.decode(utf8.decode(r));
          completer.complete(response[garageResponse]);
        });
        connection.close();
        return completer.future;
      }
      connection.close();
      return true;
    } catch (e) {
      print('Client Error: $e');
      return false;
    }
  }
}
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함