-
Notifications
You must be signed in to change notification settings - Fork 106
Rough STOMP Adapter #212
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
base: master
Are you sure you want to change the base?
Rough STOMP Adapter #212
Changes from all commits
b9d7988
3f8b298
64e2202
36c03d2
f15cb58
d37b1b5
a6b023f
80bda8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,16 @@ $ = jQuery | |
readyQueue: [] | ||
|
||
init: -> | ||
return unless SyncConfig? && Sync[SyncConfig.adapter] | ||
@adapter ||= new Sync[SyncConfig.adapter] | ||
|
||
$ => | ||
return unless SyncConfig? && Sync[SyncConfig.adapter] | ||
@adapter ||= new Sync[SyncConfig.adapter] | ||
return if @isReady() || [email protected]() | ||
@ready = true | ||
@connect() | ||
@flushReadyQueue() | ||
@bindUnsubscribe() | ||
onReadyInterval = setInterval (=> | ||
return if @isReady() || [email protected]() | ||
clearInterval(onReadyInterval) | ||
@ready = true | ||
@connect() | ||
), 250 | ||
|
||
|
||
# Handle Turbolinks teardown, unsubscribe from all channels before transition | ||
|
@@ -30,15 +32,29 @@ $ = jQuery | |
|
||
onConnectFailure: (error) -> #noop | ||
|
||
connect: -> @adapter.connect() | ||
connect: -> | ||
@adapter.connect() | ||
|
||
onConnectInterval = setInterval (=> | ||
return unless @isConnected() | ||
clearInterval(onConnectInterval) | ||
@adapter.onConnect() | ||
), 250 | ||
|
||
isConnected: -> @adapter.isConnected() | ||
|
||
onReady: (callback) -> | ||
if @isReady() | ||
callback() | ||
else | ||
@readyQueue.push callback | ||
onDebug: (message) -> | ||
return unless SyncConfig.debug_flag | ||
|
||
window?.console?.log(message) | ||
|
||
onReady: (callbacks) -> | ||
callbackOrAddToQueue = (callback) => | ||
return callback() if @isReady() | ||
@readyQueue.push(callback) | ||
|
||
return callbackOrAddToQueue(callbacks) unless callbacks.constructor == Array | ||
callbackOrAddToQueue(callback) for callback in callbacks | ||
|
||
|
||
flushReadyQueue: -> | ||
|
@@ -89,14 +105,26 @@ class Sync.Adapter | |
return | ||
|
||
subscribe: (channel, callback) -> | ||
@unsubscribeChannel(channel) | ||
subscription = new Sync[SyncConfig.adapter].Subscription(@client, channel, callback) | ||
@subscriptions.push(subscription) | ||
subscription | ||
onReadyCallback = (channel, callback) => | ||
@unsubscribeChannel(channel) | ||
subscription = new Sync[SyncConfig.adapter].Subscription(@client, channel, callback) | ||
@subscriptions.push(subscription) | ||
subscription | ||
|
||
if @isConnected() | ||
onReadyCallback(channel, callback) | ||
else | ||
Sync.onReady(onReadyCallback) | ||
|
||
onConnect: -> | ||
Sync.flushReadyQueue() | ||
Sync.bindUnsubscribe() | ||
|
||
onError: (error) -> | ||
window?.console?.log({ error: error }) | ||
|
||
class Sync.Faye extends Sync.Adapter | ||
|
||
class Sync.Faye extends Sync.Adapter | ||
subscriptions: [] | ||
|
||
available: -> | ||
|
@@ -126,8 +154,8 @@ class Sync.Pusher extends Sync.Adapter | |
!!window.Pusher | ||
|
||
connect: -> | ||
opts = | ||
encrypted: SyncConfig.pusher_encrypted | ||
# Pusher doesn't properly transform native boolean flags in options. | ||
opts = { encrypted: SyncConfig.encryption_flag.toString() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Way less cryptic than before :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that was a weird one. Although, somebody will have to keep an eye on that (re: pusher). I'm not using it anymore, as we found a less expensive option without faye's issues, rabbitmq + stomp. They may correct this in the future. |
||
|
||
opts.wsHost = SyncConfig.pusher_ws_host if SyncConfig.pusher_ws_host | ||
opts.wsPort = SyncConfig.pusher_ws_port if SyncConfig.pusher_ws_port | ||
|
@@ -137,12 +165,44 @@ class Sync.Pusher extends Sync.Adapter | |
|
||
isConnected: -> @client?.connection.state is "connected" | ||
|
||
subscribe: (channel, callback) -> | ||
@unsubscribeChannel(channel) | ||
subscription = new Sync.Pusher.Subscription(@client, channel, callback) | ||
@subscriptions.push(subscription) | ||
subscription | ||
class Sync.Stomp extends Sync.Adapter | ||
|
||
subscriptions: [] | ||
|
||
client: null | ||
socket: null | ||
|
||
headers: { | ||
login: SyncConfig.api_key, | ||
passcode: SyncConfig.auth_token | ||
} | ||
|
||
available: -> | ||
!!window.Stomp | ||
|
||
connect: -> | ||
@socket = new window.SockJS(SyncConfig.websocket) | ||
@client = window.Stomp.over(@socket) | ||
|
||
@client.debug = Sync.onDebug | ||
|
||
# SockJS does not support heart-beat: disable heart-beats | ||
@client.heartbeat.outgoing = 0 | ||
@client.heartbeat.incoming = 0 | ||
|
||
@client.connect( | ||
@headers['login'], | ||
@headers['passcode'], | ||
@onConnect, | ||
@onError, | ||
'/' | ||
) | ||
|
||
isConnected: -> | ||
try | ||
@client.connected | ||
catch error | ||
false | ||
|
||
class Sync.Pusher.Subscription | ||
constructor: (@client, channel, callback) -> | ||
|
@@ -154,6 +214,18 @@ class Sync.Pusher.Subscription | |
cancel: -> | ||
@client.unsubscribe(@channel) if @client.channel(@channel)? | ||
|
||
class Sync.Stomp.Subscription | ||
constructor: (client, channel, callback) -> | ||
@channel = channel | ||
@client = client | ||
|
||
@subscription = @client.subscribe channel, ( (e) -> | ||
callback(JSON.parse(e.body)) | ||
) | ||
|
||
cancel: -> | ||
@subscription.unsubscribe() | ||
|
||
|
||
class Sync.View | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
require 'sync/resource' | ||
require 'sync/clients/faye' | ||
require 'sync/clients/pusher' | ||
require 'sync/clients/stomp' | ||
require 'sync/clients/dummy' | ||
require 'sync/reactor' | ||
if defined? Rails | ||
|
@@ -46,13 +47,16 @@ def config | |
def config_json | ||
@config_json ||= begin | ||
{ | ||
server: server, | ||
adapter: adapter, | ||
api_key: api_key, | ||
auth_token: auth_token, | ||
debug_flag: debug_flag, | ||
encryption_flag: encryption_flag, | ||
pusher_ws_host: pusher_ws_host, | ||
pusher_ws_port: pusher_ws_port, | ||
pusher_wss_port: pusher_wss_port, | ||
pusher_encrypted: pusher_encrypted, | ||
adapter: adapter | ||
server: server, | ||
websocket: websocket | ||
}.reject { |k, v| v.nil? }.to_json | ||
end | ||
end | ||
|
@@ -101,6 +105,16 @@ def server | |
config[:server] | ||
end | ||
|
||
def websocket | ||
config[:websocket] | ||
end | ||
|
||
def destination | ||
config[:destination] ||= '/' | ||
return "/#{config[:destination]}" unless config[:destination][0] == '/' | ||
config[:destination] | ||
end | ||
|
||
def adapter_javascript_url | ||
config[:adapter_javascript_url] | ||
end | ||
|
@@ -145,12 +159,12 @@ def pusher_wss_port | |
config[:pusher_wss_port] | ||
end | ||
|
||
def pusher_encrypted | ||
if config[:pusher_encrypted].nil? | ||
true | ||
else | ||
config[:pusher_encrypted] | ||
end | ||
def encryption_flag | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 on consolidating these. |
||
config[:encryption] | ||
end | ||
|
||
def debug_flag | ||
!!config[:debug] | ||
end | ||
|
||
def reactor | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still in favor of removing the
onDebug
function completely, unless there's a reason it needs to be added.