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 message) { print(message.toString()); } void sendMessage(List 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; } }