63 lines
1.5 KiB
Dart
63 lines
1.5 KiB
Dart
|
|
import 'dart:convert';
|
||
|
|
import 'package:pusher_channels_flutter/pusher_channels_flutter.dart';
|
||
|
|
import '../services/dio_client.dart';
|
||
|
|
|
||
|
|
class ChatRealtimeService {
|
||
|
|
static final PusherChannelsFlutter _pusher =
|
||
|
|
PusherChannelsFlutter.getInstance();
|
||
|
|
|
||
|
|
static Future<void> connect({
|
||
|
|
required int ticketId,
|
||
|
|
required Function(Map<String, dynamic>) onMessage,
|
||
|
|
}) async {
|
||
|
|
|
||
|
|
print(" 🧪🧪🧪🧪🧪🧪🧪🧪🧪SUBSCRIBING TO: private-ticket.$ticketId");
|
||
|
|
|
||
|
|
await _pusher.init(
|
||
|
|
apiKey: "q5fkk5rvcnatvbgadwvl",
|
||
|
|
cluster: "mt1",
|
||
|
|
|
||
|
|
onAuthorizer: (channelName, socketId, options) async {
|
||
|
|
print("🧪🧪🧪🧪🧪🧪🧪🧪🧪🧪AUTHORIZING CHANNEL: $channelName");
|
||
|
|
|
||
|
|
final dio = DioClient.getInstance(options);
|
||
|
|
|
||
|
|
final res = await dio.post(
|
||
|
|
'/broadcasting/auth',
|
||
|
|
data: {
|
||
|
|
'socket_id': socketId,
|
||
|
|
'channel_name': channelName,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
return res.data;
|
||
|
|
},
|
||
|
|
|
||
|
|
onEvent: (event) {
|
||
|
|
if (event.eventName == "NewChatMessage") {
|
||
|
|
final data = jsonDecode(event.data);
|
||
|
|
onMessage(Map<String, dynamic>.from(data));
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
onConnectionStateChange: (current, previous) {
|
||
|
|
print("PUSHER STATE: $current");
|
||
|
|
},
|
||
|
|
|
||
|
|
onError: (message, code, error) {
|
||
|
|
print("PUSHER ERROR: $message");
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
await _pusher.subscribe(
|
||
|
|
channelName: "private-ticket.$ticketId",
|
||
|
|
);
|
||
|
|
|
||
|
|
await _pusher.connect();
|
||
|
|
}
|
||
|
|
|
||
|
|
static Future<void> disconnect() async {
|
||
|
|
await _pusher.disconnect();
|
||
|
|
}
|
||
|
|
}
|