Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
/ dart-websocket_rails Public archive

pub package to communicate with a websocket managed by websocket-rails gem

License

Notifications You must be signed in to change notification settings

m0gg/dart-websocket_rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dart-websocket_rails

pub package to communicate with a websocket managed by websocket-rails/websocket-rails gem.

Usage

Quite similar to javascript-origin. See m0gg/dart-websocket_rails-sample.

Open a connection

WebSocketRails railsWs = new WebSocketRails('${window.location.host}/websocket')
  ..connect();

Subscribe to channel

Channel wsCh = railsWs.subscribe('foo');

Trigger an Event

// Future trigger(String name, [Map<String, String> data])
railsWs.trigger('poke');
railsWs.trigger('poke', { 'id': 1 });

Trigger returns a Future which will be resolved when the websocket receives a result event (WsResult). Internally it will send a WsData Event with data argument encoded as JSON. If you wish to Send a specific Event type or modify other attributes as data you'll need to create the Event yourself and pass it to WebSocketRails.triggerEvent().

Trigger an Event on Channel

Channel wsCh = railsWs.subscribe('foo');
wsCh.trigger('poke', { 'id': 1 })

or

railsWs.trigger('ch1.poke', { 'id': 1 })

Bind to event

WebSocketRails and Channel implement the internal Binable interface and thus can be bound the same ways. Currently there are two ways to bind to an event. But if you want to unbind a single event later, you'll need to choose the "dart-way".

"Old"-fashioned way

wsCh.bind('bar', (data) {
  dyynamic m = JSON.decode(data);
  print(m);
});

"dart"-way

StreamSubscription sc = wsCh.getEventStream('bar').listen((data) {
  dyynamic m = JSON.decode(data);
  print(m);
});

The StreamSubscription instance can later be sc.cancel()-ed to unbind a single event. The returned Stream of WsChannel.getEventStream() can be listened to multiple times.

CHANGELOG

  1. Mar. 2015 - 0.2.0:
6a92d1b rework internals

Reworked internal structure. Usage mostly keeps the same.

  • Stable reconnecting
  • trigger Channel messages
  1. Jan. 2015 - 0.1.0:
b193e37 Implement correct handling of reconnect

Reconnect will now be called automaically on connection loss, but not if the initial connect() was unsuccessful. The Periodic call of reconnect() can be configured by the optional parameter reconnectTimeout on initialization ob the WebsocketRails instance.

WIP

It's not finished yet, but it works.

About

pub package to communicate with a websocket managed by websocket-rails gem

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages