-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathpusher_channels_flutter.dart
307 lines (281 loc) · 10.9 KB
/
pusher_channels_flutter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
class PusherEvent {
String channelName;
String eventName;
dynamic data;
String? userId;
PusherEvent({
required this.channelName,
required this.eventName,
this.data,
this.userId,
});
Map<String, dynamic> toMap() {
return {
'channelName': channelName,
'eventName': eventName,
'data': data,
'userId': userId,
};
}
factory PusherEvent.fromMap(Map<String, dynamic> map) {
return PusherEvent(
channelName: map['channelName'] ?? '',
eventName: map['eventName'] ?? '',
data: map['data'] ?? null,
userId: map['userId'],
);
}
String toJson() => json.encode(toMap());
factory PusherEvent.fromJson(String source) => PusherEvent.fromMap(json.decode(source));
@override
String toString() {
return 'PusherEvent(channelName: $channelName, eventName: $eventName, data: $data, userId: $userId)';
}
}
class PusherMember {
String userId;
dynamic userInfo;
PusherMember(this.userId, this.userInfo);
@override
String toString() => '{ userId: $userId, userInfo: $userInfo }';
}
class PusherChannel {
String channelName;
Map<String, PusherMember> members = {};
PusherMember? me;
int subscriptionCount = 0;
Function(dynamic data)? onSubscriptionSucceeded;
Function(dynamic event)? onEvent;
Function(PusherMember member)? onMemberAdded;
Function(PusherMember member)? onMemberRemoved;
Function(int subscriptionCount)? onSubscriptionCount;
PusherChannel({
required this.channelName,
this.onSubscriptionSucceeded,
this.onEvent,
this.onMemberAdded,
this.onMemberRemoved,
this.onSubscriptionCount,
this.me,
});
Future<void> unsubscribe() async {
return PusherChannelsFlutter.getInstance()
.unsubscribe(channelName: channelName);
}
Future<void> trigger(PusherEvent event) async {
if (event.channelName != channelName) {
throw ('Event is not for this channel');
}
return PusherChannelsFlutter.getInstance().trigger(event);
}
}
class PusherChannelsFlutter {
static PusherChannelsFlutter? _instance;
MethodChannel methodChannel = const MethodChannel('pusher_channels_flutter');
Map<String, PusherChannel> channels = {};
String connectionState = 'DISCONNECTED';
Function(String currentState, String previousState)? onConnectionStateChange;
Function(String channelName, dynamic data)? onSubscriptionSucceeded;
Function(String message, dynamic error)? onSubscriptionError;
Function(String event, String reason)? onDecryptionFailure;
Function(String message, int? code, dynamic error)? onError;
Function(PusherEvent event)? onEvent;
Function(String channelName, PusherMember member)? onMemberAdded;
Function(String channelName, PusherMember member)? onMemberRemoved;
Function(String channelName, String socketId, dynamic options)? onAuthorizer;
Function(String channelName, int subscriptionCount)? onSubscriptionCount;
static PusherChannelsFlutter getInstance() {
_instance ??= PusherChannelsFlutter();
return _instance!;
}
Future<void> init({
required String apiKey,
required String cluster,
bool? useTLS,
int? activityTimeout,
int? pongTimeout,
int? maxReconnectionAttempts,
int? maxReconnectGapInSeconds,
String? proxy, // pusher-websocket-java only
bool? enableStats, // pusher-js only
List<String>? disabledTransports, // pusher-js only
List<String>? enabledTransports, // pusher-js only
bool? ignoreNullOrigin, // pusher-js only
String? authEndpoint, // pusher-js only
String? authTransport, // pusher-js only
Map<String, Map<String, String>>? authParams, // pusher-js only
bool? logToConsole, // pusher-js only
Function(String currentState, String previousState)?
onConnectionStateChange,
Function(String channelName, dynamic data)? onSubscriptionSucceeded,
Function(String message, dynamic error)? onSubscriptionError,
Function(String event, String reason)? onDecryptionFailure,
Function(String message, int? code, dynamic error)? onError,
Function(PusherEvent event)? onEvent,
Function(String channelName, PusherMember member)? onMemberAdded,
Function(String channelName, PusherMember member)? onMemberRemoved,
Function(String channelName, String socketId, dynamic options)?
onAuthorizer,
Function(String channelName, int subscriptionCount)? onSubscriptionCount,
}) async {
methodChannel.setMethodCallHandler(_platformCallHandler);
this.onConnectionStateChange = onConnectionStateChange;
this.onError = onError;
this.onSubscriptionSucceeded = onSubscriptionSucceeded;
this.onEvent = onEvent;
this.onSubscriptionError = onSubscriptionError;
this.onDecryptionFailure = onDecryptionFailure;
this.onMemberAdded = onMemberAdded;
this.onMemberRemoved = onMemberRemoved;
this.onAuthorizer = onAuthorizer;
this.onSubscriptionCount = onSubscriptionCount;
await methodChannel.invokeMethod('init', {
"apiKey": apiKey,
"cluster": cluster,
"useTLS": useTLS,
"activityTimeout": activityTimeout,
"pongTimeout": pongTimeout,
"maxReconnectionAttempts": maxReconnectionAttempts,
"maxReconnectGapInSeconds": maxReconnectGapInSeconds,
"authorizer": onAuthorizer != null ? true : null,
"proxy": proxy,
"enableStats": enableStats,
"disabledTransports": disabledTransports,
"enabledTransports": enabledTransports,
"ignoreNullOrigin": ignoreNullOrigin,
"authEndpoint": authEndpoint,
"authTransport": authTransport,
"authParams": authParams,
"logToConsole": logToConsole
});
}
Future<dynamic> _platformCallHandler(MethodCall call) async {
final String? channelName = call.arguments['channelName'];
final String? eventName = call.arguments['eventName'];
final dynamic data = call.arguments['data'];
final dynamic user = call.arguments['user'];
final String? userId = call.arguments["userId"];
switch (call.method) {
case 'onConnectionStateChange':
connectionState = call.arguments['currentState'].toUpperCase();
onConnectionStateChange?.call(
call.arguments['currentState'].toUpperCase(),
call.arguments['previousState'].toUpperCase());
return Future.value(null);
case 'onError':
onError?.call(call.arguments['message'], call.arguments['code'],
call.arguments['error']);
return Future.value(null);
case 'onEvent':
switch (eventName) {
case 'pusher:subscription_succeeded':
case 'pusher_internal:subscription_succeeded':
// Depending on the platform implementation we get json or a Map.
var decodedData = data is Map ? data : jsonDecode(data);
decodedData?["presence"]?["hash"]?.forEach((userId_, userInfo) {
var member = PusherMember(userId_, userInfo);
channels[channelName]?.members[userId_] = member;
if (userId_ == userId) {
channels[channelName]?.me = member;
}
});
onSubscriptionSucceeded?.call(channelName!, decodedData);
channels[channelName]?.onSubscriptionSucceeded?.call(decodedData);
break;
case 'pusher:subscription_count':
case 'pusher_internal:subscription_count':
// Depending on the platform implementation we get json or a Map.
var decodedData = data is Map ? data : jsonDecode(data);
var subscriptionCount = decodedData['subscription_count'];
channels[channelName]?.subscriptionCount = subscriptionCount;
onSubscriptionCount?.call(channelName!, subscriptionCount);
channels[channelName]?.onSubscriptionCount?.call(subscriptionCount);
break;
}
final event = PusherEvent(
channelName: channelName!,
eventName: eventName!.replaceFirst("pusher_internal", "pusher"),
data: data,
userId: call.arguments['userId']);
onEvent?.call(event);
channels[channelName]?.onEvent?.call(event);
return Future.value(null);
case 'onSubscriptionError':
onSubscriptionError?.call(
call.arguments['message'], call.arguments['error']);
return Future.value(null);
case 'onDecryptionFailure':
onDecryptionFailure?.call(
call.arguments['event'], call.arguments['reason']);
return Future.value(null);
case 'onMemberAdded':
var member = PusherMember(user["userId"], user["userInfo"]);
channels[channelName]?.members[member.userId] = member;
onMemberAdded?.call(channelName!, member);
channels[channelName]?.onMemberAdded?.call(member);
return Future.value(null);
case 'onMemberRemoved':
var member = PusherMember(user["userId"], user["userInfo"]);
channels[channelName]?.members.remove(member.userId);
onMemberRemoved?.call(channelName!, member);
channels[channelName]?.onMemberRemoved?.call(member);
return Future.value(null);
case 'onAuthorizer':
return await onAuthorizer?.call(channelName!,
call.arguments['socketId'], call.arguments['options']);
default:
throw MissingPluginException('Unknown method ${call.method}');
}
}
Future<void> connect() async {
await methodChannel.invokeMethod('connect');
}
Future<void> disconnect() async {
await methodChannel.invokeMethod('disconnect');
}
Future<PusherChannel> subscribe(
{required String channelName,
var onSubscriptionSucceeded,
var onSubscriptionError,
var onMemberAdded,
var onMemberRemoved,
var onEvent,
var onSubscriptionCount}) async {
var channel = PusherChannel(
channelName: channelName,
onSubscriptionSucceeded: onSubscriptionSucceeded,
onMemberAdded: onMemberAdded,
onMemberRemoved: onMemberRemoved,
onSubscriptionCount: onSubscriptionCount,
onEvent: onEvent);
await methodChannel.invokeMethod("subscribe", {"channelName": channelName});
channels[channelName] = channel;
return channel;
}
Future<void> unsubscribe({required String channelName}) async {
channels.remove(channelName);
await methodChannel
.invokeMethod("unsubscribe", {"channelName": channelName});
}
Future<void> trigger(PusherEvent event) async {
if (event.channelName.startsWith("private-") ||
event.channelName.startsWith("presence-")) {
await methodChannel.invokeMethod('trigger', {
"channelName": event.channelName,
"eventName": event.eventName,
"data": event.data
});
} else {
throw ('Trigger event is only for private/presence channels');
}
}
Future<String> getSocketId() async {
return (await methodChannel.invokeMethod('getSocketId')).toString();
}
PusherChannel? getChannel(String channelName) {
return channels[channelName];
}
}