Skip to content

Commit

Permalink
Fix websocket usage in web env, add simple web example (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia authored Jan 17, 2024
1 parent 9ec4956 commit acca78c
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 21 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ jobs:
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
environment: test_ci
runs-on: ubuntu-latest
strategy:
matrix:
sdk: [2.19, stable, beta]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.sdk }}

- name: Install dependencies
run: dart pub get
Expand Down
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ linter:
# - prefer_const_constructors
# - prefer_constructors_over_static_methods # not yet tested
- prefer_contains
- prefer_equal_for_default_values
# - prefer_expression_function_bodies # conflicts with https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#consider-using--for-short-functions-and-methods
# - prefer_final_fields # https://github.com/dart-lang/linter/issues/506
- prefer_final_locals
Expand Down
1 change: 0 additions & 1 deletion example/chat_app/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ analyzer:
missing_required_param: error
invalid_use_of_protected_member: error
dead_code: info
sdk_version_async_exported_from_core: ignore
linter:
rules:
- unnecessary_statements
Expand Down
9 changes: 2 additions & 7 deletions example/chat_app/lib/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class ChatClient {
_client = createClient(
url,
ClientConfig(
name: conf.userName,
token: conf.userJwtToken,
headers: <String, dynamic>{
'user-id': chatUserId,
Expand Down Expand Up @@ -85,9 +84,7 @@ class ChatClient {
token: conf.subscriptionJwtToken,
),
);
subscription.publication
.map<String>((e) => utf8.decode(e.data))
.listen((data) {
subscription.publication.map<String>((e) => utf8.decode(e.data)).listen((data) {
final d = json.decode(data) as Map<String, dynamic>;
final username = d["username"].toString();
final msg = d["message"].toString();
Expand All @@ -96,9 +93,7 @@ class ChatClient {
text: msg,
user: ChatUser(
name: username,
containerColor: username == conf.userName
? Colors.lightBlueAccent
: Colors.grey[300],
containerColor: username == conf.userName ? Colors.lightBlueAccent : Colors.grey[300],
color: Colors.black87,
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/chat_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
version: 0.1.0

environment:
sdk: ">=2.18.0 <3.0.0"
sdk: ">=2.19.0 <4.0.0"

dependencies:
centrifuge:
Expand Down
2 changes: 0 additions & 2 deletions example/console/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:centrifuge/centrifuge.dart' as centrifuge;
void main() async {
final url = 'ws://localhost:8000/connection/websocket';
final channel = 'chat:index';
final userName = 'dart';
// generate user JWT token for user "dart":
// ./centrifugo gentoken --user dart
final userJwtToken =
Expand All @@ -24,7 +23,6 @@ void main() async {
final client = centrifuge.createClient(
url,
centrifuge.ClientConfig(
name: userName,
token: userJwtToken,
// Headers are only supported on platforms that support dart:io
headers: <String, dynamic>{'X-Example-Header': 'example'},
Expand Down
1 change: 0 additions & 1 deletion example/flutter_app/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ linter:
# - prefer_const_constructors
# - prefer_constructors_over_static_methods # not yet tested
- prefer_contains
- prefer_equal_for_default_values
# - prefer_expression_function_bodies # conflicts with https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#consider-using--for-short-functions-and-methods
# - prefer_final_fields # https://github.com/dart-lang/linter/issues/506
- prefer_final_locals
Expand Down
8 changes: 4 additions & 4 deletions example/flutter_app/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/usr/local/Caskroom/flutter/2.0.6/flutter"
export "FLUTTER_ROOT=/usr/local/Caskroom/flutter/3.10.6/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/fz/projects/centrifugal/centrifuge-dart/example/flutter_app"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.packages"
export "PACKAGE_CONFIG=.dart_tool/package_config.json"
2 changes: 1 addition & 1 deletion example/flutter_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: centrifuge_app
description: A new Flutter project.

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=2.19.0 <4.0.0'

dependencies:
flutter:
Expand Down
3 changes: 3 additions & 0 deletions example/web_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
3 changes: 3 additions & 0 deletions example/web_app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
6 changes: 6 additions & 0 deletions example/web_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
An absolute bare-bones web app.

```
dart pub global activate webdev
webdev serve
```
30 changes: 30 additions & 0 deletions example/web_app/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
19 changes: 19 additions & 0 deletions example/web_app/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: web_app
description: An absolute bare-bones web app.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: ^3.2.4

# Add regular dependencies here.
dependencies:
centrifuge:
path: ../../

publish_to: none

dev_dependencies:
build_runner: ^2.4.0
build_web_compilers: ^4.0.0
lints: ^2.1.0
19 changes: 19 additions & 0 deletions example/web_app/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="scaffolded-by" content="https://github.com/dart-lang/sdk">
<title>web_app</title>
<link rel="stylesheet" href="styles.css">
<script defer src="main.dart.js"></script>
</head>

<body>

<div id="output"></div>

</body>
</html>
26 changes: 26 additions & 0 deletions example/web_app/web/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:html';

import 'package:centrifuge/centrifuge.dart';

void main() {
querySelector('#output')?.text = 'Your Dart app is running.';

void onEvent(dynamic event) {
querySelector('#output')?.text = 'client> $event';
}

const url = 'ws://localhost:8000/connection/websocket';
Client client = createClient(
url,
ClientConfig(),
);

// State changes.
client.connecting.listen(onEvent);
client.connected.listen(onEvent);
client.disconnected.listen(onEvent);
// Handle async errors.
client.error.listen(onEvent);

client.connect();
}
14 changes: 14 additions & 0 deletions example/web_app/web/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@import url(https://fonts.googleapis.com/css?family=Roboto);

html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
}

#output {
padding: 20px;
text-align: center;
}
2 changes: 1 addition & 1 deletion lib/src/channel_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension HtmlWebSocketChannelExtension on WebSocketChannel {
final channel = this;
if (channel is HtmlWebSocketChannel) {
final byteBuffer = data is Uint8List ? data.buffer : Uint8List.fromList(data).buffer;
channel.innerWebSocket.sendByteBuffer(byteBuffer);
channel.sink.add(byteBuffer);
} else {
channel.sink.add(data);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
fixnum: ^1.0.0
meta: ^1.3.0
protobuf: ^3.0.0
web_socket_channel: ^2.3.0
web_socket_channel: ^2.4.0

dev_dependencies:
build_runner: ^2.0.3
Expand Down

0 comments on commit acca78c

Please sign in to comment.