-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
not done ok save content; sse separatorBuilder ok optmize save icon color
- Loading branch information
liushihao
committed
Sep 26, 2023
1 parent
1af0fa2
commit 065b486
Showing
10 changed files
with
313 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,101 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:html'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:fsearch_flutter/service/service.dart'; | ||
import 'package:fsearch_flutter/service/types.dart'; | ||
|
||
import '../util/util.dart'; | ||
|
||
Future<StreamSubscription<dynamic>?> searchText({ | ||
required String appName, | ||
required String searchPathWS, | ||
required String searchPathSSE, | ||
required int nodeId, | ||
required List<String> files, | ||
required List<String> kw, | ||
required void Function(String data) onData, | ||
void Function()? onClose, | ||
}) async { | ||
// in browsers, you need to pass a http.BrowserClient: | ||
// todo 服务端适配协议 | ||
WebSocket ws; | ||
|
||
final params = <String, dynamic>{ | ||
'appName': appName, | ||
'nodeId': "$nodeId", | ||
'files': files, | ||
'kw': kw, | ||
'dataType': 'text', | ||
}; | ||
final path = searchPathWS; | ||
final path = searchPathSSE; | ||
final query = Uri(queryParameters: params).query; | ||
// Because there may be a large amount of content transmitted, we use websocket | ||
// to transmit the search result content in batches instead of using http | ||
String url = "ws://${window.location.host}$path?$query"; | ||
String url = "$path?$query"; | ||
if (kDebugMode) { | ||
// you can modify the port 9097 to that really is | ||
url = "ws://127.0.0.1:9097$path?$query"; | ||
url = 'http://127.0.0.1:9097$url'; | ||
} | ||
myPrint("url is : $url"); | ||
EventSource eventSource; | ||
try { | ||
ws = WebSocket(url); | ||
eventSource = EventSource(url); | ||
} catch (e) { | ||
myPrint("-------- $e"); | ||
return null; | ||
} | ||
ws.onError.listen((Event event) { | ||
myPrint("websocket error $event"); | ||
|
||
ws.close(); | ||
}); | ||
ws.onOpen.listen((Event event) { | ||
myPrint("websocket open"); | ||
}); | ||
ws.onClose.listen((Event event) { | ||
myPrint("websocket close"); | ||
eventSource.onError.listen((Event event) { | ||
myPrint( | ||
"----========onError========---------- ${event.type}${event} ${event.path}"); | ||
eventSource.close(); | ||
if (onClose != null) { | ||
onClose(); | ||
} | ||
}); | ||
|
||
final listener = ws.onMessage.listen((MessageEvent event) { | ||
eventSource.onOpen.listen((Event event) { | ||
myPrint("----========open========---------- ${event.type} ${event.path}"); | ||
}); | ||
final listener = eventSource.onMessage.listen((MessageEvent event) { | ||
onData(event.data); | ||
myPrint("---------------------- websocket onMessage -------------------"); | ||
}, onError: (e) { | ||
myPrint("-------->>> $e"); | ||
}, cancelOnError: true); | ||
// window.alert("SSE链接成功"); | ||
return listener; | ||
} | ||
|
||
Future<NodeConfigInfo> homeInfo(BuildContext context) async { | ||
String url; | ||
const internConfig = "/_internal/config"; | ||
final pathname = window.location.pathname ?? '/'; | ||
if (pathname == "/") { | ||
url = internConfig; | ||
} else if (pathname.endsWith("/")) { | ||
// /user/home/ | ||
String temp = pathname.substring(0, pathname.length - 1); | ||
temp = temp.substring(0, temp.lastIndexOf("/")); | ||
url = "$temp$internConfig"; | ||
} else { | ||
// /user/home | ||
String temp = pathname; | ||
temp = temp.substring(0, temp.lastIndexOf("/")); | ||
url = "$temp$internConfig"; | ||
} | ||
// FIXME | ||
if (kDebugMode) { | ||
url = 'http://127.0.0.1:9097/_internal/config'; | ||
} | ||
print("config url: $url"); | ||
final RespData<NodeConfigInfo> result = await dioTryGet(context, url, | ||
queryParameters: {}, fromJson: NodeConfigInfo.fromJson); | ||
if (result.code != 0) { | ||
myToast(context, result.message); | ||
return NodeConfigInfo(); | ||
} | ||
return result.data ?? NodeConfigInfo(); | ||
} | ||
|
||
void saveContent(String content, String fileName) { | ||
final downLink = AnchorElement(); | ||
downLink.download = fileName; | ||
final blob = Blob([content]); | ||
downLink.href = Url.createObjectUrlFromBlob(blob); | ||
document.body?.append(downLink); | ||
downLink.click(); | ||
downLink.remove(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.