You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diplomatic-quarter/lib/uitl/SignalRUtil.dart

49 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:http/io_client.dart';
import 'package:signalr_core/signalr_core.dart';
class SignalRUtil {
String hubName;
BuildContext context;
SignalRUtil({@required this.hubName, @required this.context});
HubConnection connectionBuilder;
void startSignalRConnection() async {
connectionBuilder = HubConnectionBuilder()
.withUrl(
hubName,
HttpConnectionOptions(
client: IOClient(HttpClient()..badCertificateCallback = (x, y, z) => true),
logging: (level, message) => print(message),
))
.build();
await connectionBuilder.start();
connectionBuilder.on('ReceiveMessage', (message) {
handleIncomingMessage(message);
});
}
void closeSignalRConnection() {
connectionBuilder.stop();
}
void handleIncomingMessage(List<dynamic> message) {
print(message.toString());
}
void sendMessage(List<dynamic> args) async {
await connectionBuilder.invoke('SendMessage', args: args); //['Bob', 'Says hi!']
}
bool getConnectionState() {
if (connectionBuilder.state == HubConnectionState.connected || connectionBuilder.state == HubConnectionState.connecting) return true;
if (connectionBuilder.state == HubConnectionState.disconnected || connectionBuilder.state == HubConnectionState.disconnecting) return false;
}
}