Skip to content

Commit caad9ca

Browse files
authored
Create a package:web_socket WebSocket from dart:io WebSocket (#1173)
1 parent 5214f76 commit caad9ca

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

pkgs/web_socket/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.1
2+
3+
- Add the ability to create a `package:web_socket` `WebSocket` given a
4+
`dart:io` `WebSocket`.
5+
16
## 0.1.0
27

38
- Basic functionality in place.

pkgs/web_socket/lib/src/io_web_socket.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class IOWebSocket implements WebSocket {
4949
return IOWebSocket._(webSocket);
5050
}
5151

52+
// Create an `IOWebSocket` from an existing `dart:io` `WebSocket`.
53+
factory IOWebSocket.fromWebSocket(io.WebSocket webSocket) =>
54+
IOWebSocket._(webSocket);
55+
5256
IOWebSocket._(this._webSocket) {
5357
_webSocket.listen(
5458
(event) {

pkgs/web_socket/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: >-
33
Any easy-to-use library for communicating with WebSockets
44
that has multiple implementations.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/web_socket
6-
version: 0.1.0
6+
version: 0.1.1
77

88
environment:
99
sdk: ^3.3.0
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
@TestOn('vm')
6+
library;
7+
8+
import 'dart:io' as io;
9+
10+
import 'package:test/test.dart';
11+
import 'package:web_socket/io_web_socket.dart';
12+
import 'package:web_socket/web_socket.dart';
13+
14+
void main() {
15+
group('fromWebSocket', () {
16+
late final io.HttpServer server;
17+
late io.HttpHeaders headers;
18+
late Uri uri;
19+
20+
setUp(() async {
21+
server = (await io.HttpServer.bind('localhost', 0))
22+
..listen((request) async {
23+
headers = request.headers;
24+
await io.WebSocketTransformer.upgrade(request)
25+
.then((webSocket) => webSocket.listen(webSocket.add));
26+
});
27+
uri = Uri.parse('ws://localhost:${server.port}');
28+
});
29+
30+
test('custom headers', () async {
31+
final ws = IOWebSocket.fromWebSocket(await io.WebSocket.connect(
32+
uri.toString(),
33+
headers: {'fruit': 'apple'}));
34+
expect(headers['fruit'], ['apple']);
35+
ws.sendText('Hello World!');
36+
expect(await ws.events.first, TextDataReceived('Hello World!'));
37+
await ws.close();
38+
});
39+
});
40+
}

0 commit comments

Comments
 (0)