목적 : flutter 에서 hexString 을 byte 로 변환하여 접속된 소켓으로 보내려고 한다.
javascript 에서는 hexString 을 Uint8Array 로 변환한다. (arrayBuffer)
# inutString
0000012931334331202020202020202020202020646f6d202020202057313931304130312020202020202020202020202020202020202020202020202020202020203030302e3030302e3030302e303030203030302e3030302e3030302e3030302030302d30302d30302d30302d30302d3030202020533031383238352020202020202020205748202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020207b7b2268223a2262617365222c2263223a225731393130413031222c226d223a22726563697665227d7d2020202020202020202020202020202020202020202020202020202020202020202020202020
hex package install
$ flutter pub add hex
pubspec.yaml hex: ^0.2.0 추가됨
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
hex: ^0.2.0
:: chatGPT ::
현재 mymq 연결하여 응답까지 받아오는것 까지 했음
전체소스
import 'dart:io';
import 'dart:typed_data';
import 'package:hex/hex.dart';
void main() async {
// const String host = "220.72.212."; // php server
// const int port = 25003;
const String host = "13.125.57."; // mymq
const int port = 9001;
// connect to the socket server
final socket = await Socket.connect(host, port);
print('Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
// listen for responses from the server
socket.listen(
// handle data from the server
(Uint8List data) {
//final serverResponse = String.fromCharCodes(data);
//print('Server: $serverResponse');
String hexString = HEX.encode(data);
print(hexString);
},
// handle errors
onError: (error) {
print(error);
socket.destroy();
},
// handle server ending connection
onDone: () {
print('Server left.');
socket.destroy();
},
);
// send bytes
const String hexString = "0000012931334331202020202020202020202020646f6d202020202057313931304130312020202020202020202020202020202020202020202020202020202020203030302e3030302e3030302e303030203030302e3030302e3030302e3030302030302d30302d30302d30302d30302d3030202020533031383238352020202020202020205748202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020207b7b2268223a2262617365222c2263223a225731393130413031222c226d223a22726563697665227d7d2020202020202020202020202020202020202020202020202020202020202020202020202020";
List<int> bytes = HEX.decode(hexString);
print(bytes);
socket.add(bytes);
// send String
// await sendMessage(socket, "hellow world");
}
/*
Future<void> sendMessage(Socket socket, String message) async {
print('Client: $message');
*/
/**
* write : 문자열
* add : 바이트
*//*
// socket.write(message);
socket.add(message as List<int>);
await Future.delayed(Duration(seconds: 2));
}*/
:: 응답 ::
앞으로 개발해야 될 사항
- flutter socket 연결후 데이타 계속 보내기 mymq 연결후 데이터 응답받음
- 소켓연결하고 웹뷰연결하여 소켓접속하여 응답받는 소스 샘플
https://pub.dev/packages/hex/install
'프로그래밍 > flutter' 카테고리의 다른 글
flutter webview 기본 샘플 (0) | 2023.06.29 |
---|---|
flutter socket 연결후 데이타 계속 보내기 mymq 연결후 응답받음 (0) | 2023.06.28 |
flutter Tcp Socket 가장 간단한 샘플 (0) | 2023.06.28 |
flutter webview 와 네이티브간의 데이터 통신 webview-javascript-bridge 사용 (0) | 2023.02.20 |
flutter 앱에 webView 추가 방법 (0) | 2023.02.16 |