-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fe79220
Showing
12 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# Visual Studio Code related | ||
.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
**/doc/api/ | ||
.dart_tool/ | ||
.flutter-plugins | ||
.packages | ||
.pub-cache/ | ||
.pub/ | ||
build/ | ||
pubspec.lock | ||
|
||
# Android related | ||
**/android/**/gradle-wrapper.jar | ||
**/android/.gradle | ||
**/android/captures/ | ||
**/android/gradlew | ||
**/android/gradlew.bat | ||
**/android/local.properties | ||
**/android/**/GeneratedPluginRegistrant.java | ||
|
||
# iOS/XCode related | ||
**/ios/**/*.mode1v3 | ||
**/ios/**/*.mode2v3 | ||
**/ios/**/*.moved-aside | ||
**/ios/**/*.pbxuser | ||
**/ios/**/*.perspectivev3 | ||
**/ios/**/*sync/ | ||
**/ios/**/.sconsign.dblite | ||
**/ios/**/.tags* | ||
**/ios/**/.vagrant/ | ||
**/ios/**/DerivedData/ | ||
**/ios/**/Icon? | ||
**/ios/**/Pods/ | ||
**/ios/**/.symlinks/ | ||
**/ios/**/profile | ||
**/ios/**/xcuserdata | ||
**/ios/.generated/ | ||
**/ios/Flutter/App.framework | ||
**/ios/Flutter/Flutter.framework | ||
**/ios/Flutter/Generated.xcconfig | ||
**/ios/Flutter/app.flx | ||
**/ios/Flutter/app.zip | ||
**/ios/Flutter/flutter_assets/ | ||
**/ios/ServiceDefinitions.json | ||
**/ios/Runner/GeneratedPluginRegistrant.* | ||
|
||
# Exceptions to above rules. | ||
!**/ios/**/default.mode1v3 | ||
!**/ios/**/default.mode2v3 | ||
!**/ios/**/default.pbxuser | ||
!**/ios/**/default.perspectivev3 | ||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 7a4c33425ddd78c54aba07d86f3f9a4a0051769b | ||
channel: stable | ||
|
||
project_type: package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## [0.0.1] - TODO: Add release date. | ||
|
||
* TODO: Describe initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TODO: Add your license here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# websocket | ||
|
||
A new Flutter package project. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Dart | ||
[package](https://flutter.dev/developing-packages/), | ||
a library module containing code that can be shared easily across | ||
multiple Flutter or Dart projects. | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.dev/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
library websocket; | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:html' as html; | ||
import 'dart:typed_data'; | ||
import './websocket_stub.dart' as stub; | ||
|
||
class WebSocket implements stub.WebSocket { | ||
html.WebSocket _socket; | ||
|
||
WebSocket._(this._socket) { | ||
_socket.onClose.listen((html.CloseEvent event) { | ||
closeCode = event.code; | ||
closeReason = event.reason; | ||
_streamController.close(); | ||
}); | ||
|
||
_socket.onError.listen((html.Event error) { | ||
_streamController.addError(error); | ||
}); | ||
_socket.onMessage.listen((html.MessageEvent message) async { | ||
final data = message.data; | ||
if (data is String) { | ||
_streamController.add(data); | ||
return; | ||
} | ||
if (data is html.Blob) { | ||
final reader = html.FileReader(); | ||
reader.readAsArrayBuffer(data); | ||
await reader.onLoad.first; | ||
_streamController.add(reader.result); | ||
return; | ||
} | ||
|
||
throw UnsupportedError('unspported data type $data'); | ||
}); | ||
} | ||
|
||
static Future<WebSocket> connect( | ||
String url, { | ||
Iterable<String> protocols, | ||
}) async { | ||
final s = html.WebSocket(url, protocols); | ||
await s.onOpen.first; | ||
return WebSocket._(s); | ||
} | ||
|
||
@override | ||
void add(/*String|List<int>*/ data) { | ||
if (data is String) { | ||
return _socket.send(data); | ||
} | ||
if (data is List<int>) { | ||
return _socket.sendByteBuffer(Uint8List.fromList(data).buffer); | ||
} | ||
|
||
throw UnsupportedError('unspported data type $data'); | ||
} | ||
|
||
@override | ||
Future addStream(Stream stream) { | ||
final completer = Completer(); | ||
stream.listen((data) { | ||
_socket.send(data); | ||
}, onError: (error) { | ||
_socket.send(error.toString()); | ||
completer.completeError(error); | ||
}, onDone: () => completer.complete()); | ||
return completer.future; | ||
} | ||
|
||
@override | ||
void addUtf8Text(List<int> bytes) => | ||
_socket.send(utf8.decode(bytes)); | ||
|
||
@override | ||
Future close([int code, String reason]) { | ||
if (code != null) { | ||
_socket.close(code, reason); | ||
} else { | ||
_socket.close(); | ||
} | ||
return _socket.onClose.first; | ||
} | ||
|
||
@override | ||
int closeCode; | ||
|
||
@override | ||
String closeReason; | ||
|
||
@override | ||
String get extensions => _socket.extensions; | ||
|
||
@override | ||
String get protocol => _socket.protocol; | ||
|
||
@override | ||
int get readyState => _socket.readyState; | ||
|
||
@override | ||
void addError(Object error, [StackTrace stackTrace]) => | ||
_streamController.addError(error, stackTrace); | ||
|
||
@override | ||
Future get done => _socket.onClose.first; | ||
|
||
StreamController<dynamic /*String|List<int>*/ > _streamController = | ||
StreamController.broadcast(); //Add .broadcast here | ||
|
||
@override | ||
Stream<dynamic /*String|List<int>*/ > get stream => _streamController.stream; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
library websocket; | ||
|
||
import 'dart:async'; | ||
import 'dart:io' as io; | ||
import './websocket_stub.dart' as stub; | ||
|
||
class WebSocket implements stub.WebSocket { | ||
io.WebSocket _socket; | ||
|
||
WebSocket._(this._socket); | ||
|
||
static Future<WebSocket> connect( | ||
String url, { | ||
Iterable<String> protocols, | ||
}) async { | ||
return WebSocket._(await io.WebSocket.connect(url, protocols: protocols)); | ||
} | ||
|
||
@override | ||
void add(/*String|List<int>*/ data) => _socket.add(data); | ||
|
||
@override | ||
Future addStream(Stream stream) => _socket.addStream(stream); | ||
|
||
@override | ||
void addUtf8Text(List<int> bytes) => _socket.addUtf8Text(bytes); | ||
|
||
@override | ||
Future close([int code, String reason]) => _socket.close(code, reason); | ||
|
||
@override | ||
int get closeCode => _socket.closeCode; | ||
|
||
@override | ||
String get closeReason => _socket.closeReason; | ||
|
||
@override | ||
String get extensions => _socket.extensions; | ||
|
||
@override | ||
String get protocol => _socket.protocol; | ||
|
||
@override | ||
int get readyState => _socket.readyState; | ||
|
||
@override | ||
void addError(Object error, [StackTrace stackTrace]) => | ||
_socket.addError(error, stackTrace); | ||
|
||
@override | ||
Future get done => _socket.done; | ||
|
||
Stream _stream; | ||
|
||
@override | ||
Stream<dynamic /*String|List<int>*/ > get stream => | ||
_stream ??= _socket.asBroadcastStream(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
abstract class WebSocketStatus { | ||
static const int normalClosure = 1000; | ||
static const int goingAway = 1001; | ||
static const int protocolError = 1002; | ||
static const int unsupportedData = 1003; | ||
static const int reserved1004 = 1004; | ||
static const int noStatusReceived = 1005; | ||
static const int abnormalClosure = 1006; | ||
static const int invalidFramePayloadData = 1007; | ||
static const int policyViolation = 1008; | ||
static const int messageTooBig = 1009; | ||
static const int missingMandatoryExtension = 1010; | ||
static const int internalServerError = 1011; | ||
static const int reserved1015 = 1015; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
library websocket; | ||
|
||
import 'dart:async'; | ||
|
||
final _unsupportedError = UnsupportedError( | ||
'Cannot work with WebSocket without dart:html or dart:io.'); | ||
|
||
class WebSocket implements StreamSink<dynamic /*String|List<int>*/ > { | ||
static Future<WebSocket> connect( | ||
String url, { | ||
Iterable<String> protocols, | ||
}) async => | ||
throw _unsupportedError; | ||
|
||
void add(/*String|List<int>*/ data) => throw _unsupportedError; | ||
|
||
Future addStream(Stream stream) => throw _unsupportedError; | ||
|
||
void addUtf8Text(List<int> bytes) => throw _unsupportedError; | ||
|
||
Future close([int code, String reason]) => throw _unsupportedError; | ||
|
||
int get closeCode => throw _unsupportedError; | ||
|
||
String get closeReason => throw _unsupportedError; | ||
|
||
String get extensions => throw _unsupportedError; | ||
|
||
String get protocol => throw _unsupportedError; | ||
|
||
int get readyState => throw _unsupportedError; | ||
|
||
void addError(Object error, [StackTrace stackTrace]) => | ||
throw _unsupportedError; | ||
|
||
Future get done => throw _unsupportedError; | ||
|
||
Stream<dynamic /*String|List<int>*/ > get stream => throw _unsupportedError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
export 'src/websocket_stub.dart' | ||
if (dart.library.html) 'src/websocket_browser.dart' | ||
// ignore: uri_does_not_exist | ||
if (dart.library.io) 'src/websocket_io.dart'; | ||
export 'src/websocket_status.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: websocket | ||
description: Universal package for websocket, compatible for web, flutter, and vm. | ||
version: 0.0.4 | ||
author: TruongSinh Tran-Nguyen <[email protected]> | ||
homepage: https://github.com/truongsinh/dart-websocket | ||
|
||
environment: | ||
sdk: '>=2.0.0 <3.0.0' | ||
|
||
dev_dependencies: | ||
test: any |
Oops, something went wrong.