pub package to communicate with a websocket managed by websocket-rails/websocket-rails gem.
Quite similar to javascript-origin. See m0gg/dart-websocket_rails-sample.
WebSocketRails railsWs = new WebSocketRails('${window.location.host}/websocket')
..connect();
Channel wsCh = railsWs.subscribe('foo');
// 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()
.
Channel wsCh = railsWs.subscribe('foo');
wsCh.trigger('poke', { 'id': 1 })
or
railsWs.trigger('ch1.poke', { 'id': 1 })
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.
- Mar. 2015 - 0.2.0:
6a92d1b rework internals
Reworked internal structure. Usage mostly keeps the same.
- Stable reconnecting
- trigger Channel messages
- 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.
It's not finished yet, but it works.