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

Unify dart:html/dart:io WebSocket interfaces. #13513

Closed
DartBot opened this Issue Sep 23, 2013 · 9 comments

Comments

Projects
None yet
6 participants
@DartBot

DartBot commented Sep 23, 2013

This issue was originally filed by george.moschoviti...@gmail.com


The WbeSocket interface in dart:html is different than the one defined in dart:io.

So when you have a class that could perfectly well work on both the console and the browser (e.g. a client for a websocket protocol) you have to create and maintain two slightly different versions.

@anders-sandholm

This comment has been minimized.

Show comment
Hide comment
Member

anders-sandholm commented Sep 24, 2013

Added Area-IO, Triaged labels.

@sgjesse

This comment has been minimized.

Show comment
Hide comment
@sgjesse

sgjesse Sep 25, 2013

Member

In the short term the WebSocket interface in dart:io and dart:html will focus on providing access to all features that each underlying platform supports. Based on these two APIs a common interface can be implemented a a separate package.


Added this to the Later milestone.
Removed Type-Defect, Priority-Unassigned, Area-IO labels.
Added Type-Enhancement, Priority-Low, Area-Library labels.

Member

sgjesse commented Sep 25, 2013

In the short term the WebSocket interface in dart:io and dart:html will focus on providing access to all features that each underlying platform supports. Based on these two APIs a common interface can be implemented a a separate package.


Added this to the Later milestone.
Removed Type-Defect, Priority-Unassigned, Area-IO labels.
Added Type-Enhancement, Priority-Low, Area-Library labels.

@DartBot

This comment has been minimized.

Show comment
Hide comment
@DartBot

DartBot Nov 3, 2013

This comment was originally written by igor.ko...@mailvision.com


Can it be used as such interface ?

import 'dart:async';

abstract class AbstractWebSocket {
  static const int CONNECTING = 0;
  static const int OPEN = 1;
  static const int CLOSING = 2;
  static const int CLOSED = 3;
  
  int readyState;
  int closeCode;
  String closeState;
  
  void close([int code, String reason]);
  void send(dynamic data); // String or List<int>
  
  Stream get onOpen;
  Stream get onMessage;
  Stream get onClose;
  Stream get onError;
}

DartBot commented Nov 3, 2013

This comment was originally written by igor.ko...@mailvision.com


Can it be used as such interface ?

import 'dart:async';

abstract class AbstractWebSocket {
  static const int CONNECTING = 0;
  static const int OPEN = 1;
  static const int CLOSING = 2;
  static const int CLOSED = 3;
  
  int readyState;
  int closeCode;
  String closeState;
  
  void close([int code, String reason]);
  void send(dynamic data); // String or List<int>
  
  Stream get onOpen;
  Stream get onMessage;
  Stream get onClose;
  Stream get onError;
}

@DartBot

This comment has been minimized.

Show comment
Hide comment
@DartBot

DartBot Nov 3, 2013

This comment was originally written by igor.ko...@mailvision.com


Problem here is stream events !

For example html web socket version defined in dartium.dart:
@DomName('WebSocket.onclose')
  @­DocsEditable()
  Stream<CloseEvent> get onClose => closeEvent.forTarget(this);

  @­DomName('WebSocket.onerror')
  @­DocsEditable()
  Stream<Event> get onError => errorEvent.forTarget(this);

  @­DomName('WebSocket.onmessage')
  @­DocsEditable()
  Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);

Event, CloseEvent, MessageEvent defined in the same file, so cannot be used in
server code (dart:io)

DartBot commented Nov 3, 2013

This comment was originally written by igor.ko...@mailvision.com


Problem here is stream events !

For example html web socket version defined in dartium.dart:
@DomName('WebSocket.onclose')
  @­DocsEditable()
  Stream<CloseEvent> get onClose => closeEvent.forTarget(this);

  @­DomName('WebSocket.onerror')
  @­DocsEditable()
  Stream<Event> get onError => errorEvent.forTarget(this);

  @­DomName('WebSocket.onmessage')
  @­DocsEditable()
  Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);

Event, CloseEvent, MessageEvent defined in the same file, so cannot be used in
server code (dart:io)

@DartBot

This comment has been minimized.

Show comment
Hide comment
@DartBot

DartBot Nov 11, 2013

This comment was originally written by igor.ko...@mailvision.com


I really needed such common interface, so implemented it for dart:io and dart:html

abstract class AbstractWebSocket {
  static const int CONNECTING = 0;
  static const int OPEN = 1;
  static const int CLOSING = 2;
  static const int CLOSED = 3;

  int get readyState;
  int get closeCode;
  String get closeState;

  void close([int code, String reason]);
  void send(dynamic data); // String or List<int>

  Future<Stream<dynamic>> connect(String url);
  Stream<dynamic> get stream; // data is String or List<int>
}

The test:
 - connect to echo web socket server
 - send string message, send binary message,
 - receive messages
 - disconnect

passed for

  • dart application mode
  • browser mode as dart, and as js (in dartium).

Test projects in attachment.
(I only learn Future/Stream API, so sure that it can be improved)


Attachments:
web_socket_client.zip (135.93 KB)
web_socket_server.zip (1.35 KB)

DartBot commented Nov 11, 2013

This comment was originally written by igor.ko...@mailvision.com


I really needed such common interface, so implemented it for dart:io and dart:html

abstract class AbstractWebSocket {
  static const int CONNECTING = 0;
  static const int OPEN = 1;
  static const int CLOSING = 2;
  static const int CLOSED = 3;

  int get readyState;
  int get closeCode;
  String get closeState;

  void close([int code, String reason]);
  void send(dynamic data); // String or List<int>

  Future<Stream<dynamic>> connect(String url);
  Stream<dynamic> get stream; // data is String or List<int>
}

The test:
 - connect to echo web socket server
 - send string message, send binary message,
 - receive messages
 - disconnect

passed for

  • dart application mode
  • browser mode as dart, and as js (in dartium).

Test projects in attachment.
(I only learn Future/Stream API, so sure that it can be improved)


Attachments:
web_socket_client.zip (135.93 KB)
web_socket_server.zip (1.35 KB)

@DartBot

This comment has been minimized.

Show comment
Hide comment
@DartBot

DartBot Dec 5, 2013

This comment was originally written by igor.ko...@mailvision.com


  1. to AbstractWebSocket interface
       added optional sub-protocol (header Sec-Websocket-Protocol in http handshake)

  connect(String url, [dymamic subProtocols])

where subProtocols is String or List<String>

  1. compatibility question:
    I see that "io" websocket send in handshake header: Accept-Encoding: gzip
    Can I remove the header (and use plain text) ?

DartBot commented Dec 5, 2013

This comment was originally written by igor.ko...@mailvision.com


  1. to AbstractWebSocket interface
       added optional sub-protocol (header Sec-Websocket-Protocol in http handshake)

  connect(String url, [dymamic subProtocols])

where subProtocols is String or List<String>

  1. compatibility question:
    I see that "io" websocket send in handshake header: Accept-Encoding: gzip
    Can I remove the header (and use plain text) ?
@kasperl

This comment has been minimized.

Show comment
Hide comment
@kasperl

kasperl Jul 10, 2014

Contributor

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

Contributor

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl

This comment has been minimized.

Show comment
Hide comment
@kasperl

kasperl Aug 4, 2014

Contributor

Removed Oldschool-Milestone-Later label.

Contributor

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@DartBot

This comment has been minimized.

Show comment
Hide comment
@DartBot

DartBot Feb 26, 2015

This comment was originally written by @stevenroose


A unified HTTP interface would be very beneficial as well.

I'd propose a (fictional) dart:http.HttpClient interface that both dart:io.HttpClient and package:browser_client.BrowserClient can implement.

It's very cumbersome that libraries that try to implement a protocol that is specified around HTTP has to worry about whether it is run in VM or a browser.

DartBot commented Feb 26, 2015

This comment was originally written by @stevenroose


A unified HTTP interface would be very beneficial as well.

I'd propose a (fictional) dart:http.HttpClient interface that both dart:io.HttpClient and package:browser_client.BrowserClient can implement.

It's very cumbersome that libraries that try to implement a protocol that is specified around HTTP has to worry about whether it is run in VM or a browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment