-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added websockets to receive job status
- Loading branch information
1 parent
49bb285
commit 35d7b2a
Showing
3 changed files
with
114 additions
and
6 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 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:web_socket_channel/web_socket_channel.dart'; | ||
|
||
class TaskUpdate { | ||
final String status; | ||
final String job; | ||
const TaskUpdate(this.status, this.job); | ||
TaskUpdate.fromJson(Map<String, dynamic> json) | ||
: status = json['status'], | ||
job = json['job']; | ||
Map<String, dynamic> toJson() => { | ||
'status': status, | ||
'job': job, | ||
}; | ||
} | ||
|
||
Future<WebSocketChannel> listenForTaskUpdates(url, onSuccess, onFailure) async { | ||
final webSocketChannel = WebSocketChannel.connect( | ||
Uri.parse(url), | ||
); | ||
await webSocketChannel.ready; | ||
webSocketChannel.stream.listen((message) { | ||
debugPrint(message); | ||
TaskUpdate update = | ||
TaskUpdate.fromJson(jsonDecode(message) as Map<String, dynamic>); | ||
if (update.status == 'success') { | ||
onSuccess(update); | ||
} | ||
if (update.status == 'failure') { | ||
onFailure(update); | ||
} | ||
}); | ||
return webSocketChannel; | ||
} | ||
|
||
String getWsUrl(url, clientId) { | ||
return url.replaceFirst('http', 'ws') + '/ws?clientID=' + clientId; | ||
} |
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