Skip to content

Commit

Permalink
Merge pull request #8 from takenet/feature/blip-chat-sdk
Browse files Browse the repository at this point in the history
Feature/blip chat sdk
  • Loading branch information
leonardogbr authored Nov 7, 2022
2 parents e589b03 + b4564d5 commit b765dda
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"flutterSdkVersion": "3.3.1",
"flutterSdkVersion": "3.3.5",
"flavors": {}
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.18

* Updated lime
* Added onConnectionDone Listener

## 0.0.17

* Updated lime
Expand Down
51 changes: 22 additions & 29 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:blip_sdk/src/config.dart';
import 'package:lime/lime.dart';
import 'package:ssl_pinning_plugin/ssl_pinning_plugin.dart';
import 'application.dart';
import 'extensions/base.extension.dart';
import 'extensions/enums/extension_type.enum.dart';
Expand All @@ -17,7 +14,7 @@ class Client {
final String uri;
final Application application;
final Transport transport;
final bool forceSecureConnection;
final bool useMtls;

ClientChannel _clientChannel;
final _notificationListeners = <Listener<Notification>>[];
Expand All @@ -27,7 +24,9 @@ class Client {
final _sessionFinishedHandlers = <StreamController>[];
final _sessionFailedHandlers = <StreamController>[];
final _extensions = <ExtensionType, BaseExtension>{};
final onListeningChanged = StreamController();
final onListeningChanged = StreamController<bool>();

late StreamController<bool> onConnectionDone;

bool _listening = false;
bool _closing = false;
Expand All @@ -39,7 +38,7 @@ class Client {
required this.uri,
required this.transport,
required this.application,
this.forceSecureConnection = false,
this.useMtls = false,
}) : _clientChannel = ClientChannel(transport) {
_initializeClientChannel();
}
Expand All @@ -52,7 +51,8 @@ class Client {
}

/// Allows connection with an identifier and password
Future<Session> connectWithPassword(String identifier, String password, {Presence? presence}) {
Future<Session> connectWithPassword(String identifier, String password,
{Presence? presence}) {
application.identifier = identifier;
application.authentication = PlainAuthentication(password: password);

Expand All @@ -61,7 +61,8 @@ class Client {
}

/// Allows connection with an identifier and key
Future<Session> connectWithKey(String identifier, String key, {Presence? presence}) {
Future<Session> connectWithKey(String identifier, String key,
{Presence? presence}) {
application.identifier = identifier;
application.authentication = KeyAuthentication(key: key);

Expand All @@ -72,23 +73,6 @@ class Client {

/// Starts the process of connecting to the server and establish a session
Future<Session> connect() async {
if (forceSecureConnection) {
const wssProtocol = 'wss://';
const httpsProtocol = 'https://';
var uri = this.uri;

if (Platform.isAndroid && uri.contains(wssProtocol)) {
uri = uri.replaceFirst(wssProtocol, httpsProtocol);
}

await SslPinningPlugin.check(
serverURL: uri,
sha: SHA.SHA256,
allowedSHAFingerprints: Config.allowedFingerprints,
timeout: 300,
);
}

if (_connectionTryCount >= maxConnectionTryCount) {
throw Exception(
'Could not connect: Max connection try count of $maxConnectionTryCount reached. Please check you network and refresh the page.');
Expand All @@ -97,7 +81,7 @@ class Client {
_connectionTryCount++;
_closing = false;
return transport
.open(uri)
.open(uri, useMtls: useMtls)
.then(
(_) => _clientChannel.establishSession(
application.identifier + '@' + application.domain,
Expand Down Expand Up @@ -129,6 +113,9 @@ class Client {
transport.onEnvelope?.close();
transport.onEnvelope = StreamController<Map<String, dynamic>>();

transport.onConnectionDone?.close();
transport.onConnectionDone = StreamController<bool>();

transport.onClose.close();
transport.onClose = StreamController<bool>();

Expand Down Expand Up @@ -206,6 +193,8 @@ class Client {
stream.sink.add(session);
}
});

onConnectionDone = _clientChannel.onConnectionDone;
}

/// Notifies the [Message] listeners with the received [Message]
Expand Down Expand Up @@ -298,6 +287,7 @@ class Client {
if (_clientChannel.state == SessionState.established) {
final result = await _clientChannel.sendFinishingSession();
onListeningChanged.close();
onConnectionDone.close();

await transport.close();

Expand Down Expand Up @@ -390,7 +380,8 @@ class Client {
}

/// Allow to add a new [Command] listeners, returns a function that can be called to delete this listener from the list
void Function() addCommandListener(StreamController<Command> stream, {bool Function(Command)? filter}) {
void Function() addCommandListener(StreamController<Command> stream,
{bool Function(Command)? filter}) {
_commandListeners.add(Listener<Command>(stream, filter: filter));

return () {
Expand Down Expand Up @@ -457,7 +448,8 @@ class Client {
}

/// A function to filter a listener
bool Function(Listener<T>) filterListener<T extends Envelope>(StreamController<T> stream, bool Function(T)? filter) {
bool Function(Listener<T>) filterListener<T extends Envelope>(
StreamController<T> stream, bool Function(T)? filter) {
return (Listener<T> l) => l.stream == stream && l.filter == filter;
}

Expand Down Expand Up @@ -493,5 +485,6 @@ class Client {
}

/// Returns a media extension
MediaExtension get media => _getExtension<MediaExtension>(ExtensionType.media, application.domain);
MediaExtension get media =>
_getExtension<MediaExtension>(ExtensionType.media, application.domain);
}
51 changes: 26 additions & 25 deletions lib/src/client_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,136 +5,137 @@ import 'client.dart';
class ClientBuilder {
Application application;
Transport transport;
bool forceSecureConnection;
bool useMtls;

ClientBuilder({
required this.transport,
this.forceSecureConnection = false,
this.useMtls = false,
}) : application = Application();

/// Allows you to set a custom application for configuration
ClientBuilder withApplication(Application application) {
ClientBuilder withApplication(final Application application) {
this.application = application;
return this;
}

/// Sets a identifier
ClientBuilder withIdentifier(String identifier) {
ClientBuilder withIdentifier(final String identifier) {
application.identifier = identifier;
return this;
}

/// Sets an instance
ClientBuilder withInstance(String instance) {
ClientBuilder withInstance(final String instance) {
application.instance = instance;
return this;
}

/// Sets a domain
ClientBuilder withDomain(String domain) {
ClientBuilder withDomain(final String domain) {
application.domain = domain;
return this;
}

/// Sets a scheme
ClientBuilder withScheme(String scheme) {
ClientBuilder withScheme(final String scheme) {
application.scheme = scheme;
return this;
}

/// Sets a host name
ClientBuilder withHostName(String hostName) {
ClientBuilder withHostName(final String hostName) {
application.hostName = hostName;
return this;
}

/// Sets a port
ClientBuilder withPort(int port) {
ClientBuilder withPort(final int port) {
application.port = port;
return this;
}

/// Sets authentication to [KeyAuthentication] type
ClientBuilder withAccessKey(String accessKey) {
ClientBuilder withAccessKey(final String accessKey) {
application.authentication = KeyAuthentication(key: accessKey);
return this;
}

/// Sets authentication to [PlainAuthentication] type
ClientBuilder withPassword(String password) {
ClientBuilder withPassword(final String password) {
application.authentication = PlainAuthentication(password: password);
return this;
}

/// Sets authentication to [ExternalAuthentication] type
ClientBuilder withToken(String token, String issuer) {
ClientBuilder withToken(final String token, final String issuer) {
application.authentication =
ExternalAuthentication(token: token, issuer: issuer);
return this;
}

/// Sets the [SessionCompression]
ClientBuilder withCompression(SessionCompression compression) {
ClientBuilder withCompression(final SessionCompression compression) {
application.compression = compression;
return this;
}

/// Sets the [SessionEncryption]
ClientBuilder withEncryption(SessionEncryption encryption) {
ClientBuilder withEncryption(final SessionEncryption encryption) {
application.encryption = encryption;
return this;
}

/// Sets the presence routing rule
ClientBuilder withRoutingRule(RoutingRule routingRule) {
ClientBuilder withRoutingRule(final RoutingRule routingRule) {
application.presence.routingRule = routingRule;
return this;
}

/// Sets the presence echo
ClientBuilder withEcho(bool echo) {
ClientBuilder withEcho(final bool echo) {
application.presence.echo = echo;
return this;
}

/// Sets the presence priority
ClientBuilder withPriority(int priority) {
ClientBuilder withPriority(final int priority) {
application.presence.priority = priority;
return this;
}

/// Sets the presence round robin
ClientBuilder withRoundRobin(bool roundRobin) {
ClientBuilder withRoundRobin(final bool roundRobin) {
application.presence.roundRobin = roundRobin;
return this;
}

/// Send a [Notification] when consume the [Message]
ClientBuilder withNotifyConsumed(bool notifyConsumed) {
ClientBuilder withNotifyConsumed(final bool notifyConsumed) {
application.notifyConsumed = notifyConsumed;
return this;
}

/// Set a default timeout
ClientBuilder withCommandTimeout(int timeoutInMilliSecs) {
ClientBuilder withCommandTimeout(final int timeoutInMilliSecs) {
application.commandTimeout = timeoutInMilliSecs;
return this;
}

/// Set a secure connection
ClientBuilder withSecureConnection() {
forceSecureConnection = true;
ClientBuilder withMtls(final bool? useMtls) {
this.useMtls = useMtls ?? false;
return this;
}

/// Returns a new instance of SDK Client
Client build() {
final uri = '${application.scheme}://${application.hostName}:${application.port}';
final uri =
'${application.scheme}://${application.hostName}:${application.port}';
return Client(
uri: uri,
transport: transport,
application: application,
forceSecureConnection: forceSecureConnection,
useMtls: useMtls,
);
}
}
Loading

0 comments on commit b765dda

Please sign in to comment.