Skip to content

Commit

Permalink
Add websocket option to force wss connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Jan 2, 2025
1 parent c058e29 commit 9faff96
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/lib/models/communicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SocketNotifier extends StateNotifier<Socket?> {
state == ConnectionState.connected;
}

void init(String hostname, int? port, [String? token]) {
void init(String hostname, int? port, {String? token, bool secure = false}) {
if (state != null) return;
if (_connectionState != ConnectionState.none) return;
_connectionState = ConnectionState.connecting;
Expand All @@ -112,7 +112,7 @@ class SocketNotifier extends StateNotifier<Socket?> {
return;
}

var url = "//$hostname";
var url = secure ? "wss://$hostname" : "ws://$hostname";
if (port != null) url += ":$port";
if (token != null) url += "?token=$token";

Expand Down
11 changes: 8 additions & 3 deletions app/lib/pages/connect_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class ConnectPage extends HookConsumerWidget {
@QueryParam("host") this.hostname = "",
@QueryParam() this.port,
@QueryParam() this.token = "",
@QueryParam() this.secure = false,
super.key,
});

final String hostname;
final int? port;
final String token;
final bool secure;

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand All @@ -39,9 +41,12 @@ class ConnectPage extends HookConsumerWidget {
useEffect(
() {
final timer = Timer(1.seconds, () {
ref
.read(socketProvider.notifier)
.init(hostname, port, token.isEmpty ? null : token);
ref.read(socketProvider.notifier).init(
hostname,
port,
token: token.isEmpty ? null : token,
secure: secure,
);
});
return timer.cancel;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class CommunicationHandler : KoinComponent {
)
private val websocketHostname: String? by optionalConfig("websocket.hostname")
private val websocketAppendPort: Boolean? by optionalConfig("websocket.append_port")
private val websocketSecure: Boolean? by optionalConfig("websocket.secure")


val authenticationEnabled: Boolean
Expand Down Expand Up @@ -188,6 +189,7 @@ class CommunicationHandler : KoinComponent {
url += "/#/connect?host=${websocketHostname ?: hostName}"
if (websocketAppendPort != false) url += "&port=${webSocketPort}"
if (auth == "session") url += "&token=${generateSessionToken(playerId)}"
if (websocketSecure == true) url += "&secure=true"

return url
}
Expand Down

0 comments on commit 9faff96

Please sign in to comment.