Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional host and port #3

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:pusher_dart/pusher_dart.dart';
final pusher = Pusher(
DotEnv().env['PUSHER_APP_KEY'],
PusherOptions(
//host:'10.0.2.2', //optional
//port:6001, //optional
authEndpoint: DotEnv().env['PUSHER_AUTH_URL'],
auth: PusherAuth(headers: {
'Authorization': 'Bearer $apiToken',
Expand Down
29 changes: 26 additions & 3 deletions lib/pusher_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:meta/meta.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';



/// Class to hold headers to send to authentication endpoint
/// Ex. `PusherAuth(headers: {'Authorization': 'Bearer $token'})`
@immutable
Expand All @@ -28,12 +30,25 @@ class PusherOptions {
final String authEndpoint;
final PusherAuth auth;

// for using a different host or port
final String host;
final int port;

//use wss or ws
final bool encrypted;

/// Pusher cluster
/// @see https://pusher.com/docs/channels/miscellaneous/clusters
final String cluster;

/// Default constructor
PusherOptions({this.authEndpoint, this.auth, this.cluster = 'ap2'});
PusherOptions(
{this.authEndpoint,
this.auth,
this.cluster = 'ap2',
this.host,
this.port = 443,
this.encrypted = true});
}

/// A channel
Expand Down Expand Up @@ -118,8 +133,16 @@ class Connection with _EventEmitter {
try {
state = 'connecting';
_broadcast('connecting');
webSocketChannel = IOWebSocketChannel.connect(
'wss://ws-${options.cluster}.pusher.com:443/app/$apiKey?protocol=5&client=dart-libPusher&version=0.1.0');

String protocol = options.encrypted ? 'wss://' : 'ws://';
String host = options.host ?? 'ws-${options.cluster}.pusher.com';

String domain = protocol + host + ":" + options.port.toString();
if (Pusher.log != null) Pusher.log('connecting to ' + domain);

webSocketChannel = IOWebSocketChannel.connect(domain +
'/app/$apiKey?protocol=5&client=dart-libPusher&version=0.1.0');

webSocketChannel.stream.listen(_handleMessage);
} catch (e) {
// Give up if we have to tray again after an hour
Expand Down