/
danthes.js.coffee
174 lines (159 loc) · 6.1 KB
/
danthes.js.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Danthes Privat pub/sub Faye wrapper
#
# @example Howto enable debug
# Danthes.debug = true
# @example reset all internal data
# Danthes.reset()
# @example Howto sign and subscribe on channel with callback function
# Danthes.sign
# server: 'faye.example.com'
# channel: 'somechannel'
# signature: 'dc1c71d3e959ebb6f49aa6af0c86304a0740088d'
# timestamp: 1302306682972
# connect: (subscription) ->
# console.log(subscription)
# error: (subscription, error) ->
# console.log("error: #{error}")
window.Danthes = class Danthes
@debug: false
@debugMessage: (message) ->
console.log(message) if @debug
# Reset all
@reset: ->
@connecting = false
@fayeClient = null
@fayeCallbacks = []
@subscriptions = {}
@server = null
@disables = []
@connectionSettings =
timeout: 120
retry: 5
endpoints: {}
# Connect to faye
@faye: (callback) =>
if @fayeClient?
callback(@fayeClient)
else
@fayeCallbacks.push(callback)
if @server && !@connecting
@connecting = true
unless Faye?
script = document.createElement 'script'
script.type = 'text/javascript'
script.src = "#{@server}/client.js"
script.id = "faye-connection-script"
complete = false
script.onload = script.onreadystatechange = () =>
if !complete && (!script.readyState || script.readyState is "loaded" || script.readyState is "complete")
complete = true
script.onload = script.onreadystatechange = null
@debugMessage 'connect to faye after script loaded'
@connectToFaye()
@debugMessage 'faye script init'
document.documentElement.appendChild script
else
@debugMessage 'faye already inited'
@connectToFaye()
# Faye extension for incoming and outgoing messages
@fayeExtension:
incoming : (message, callback) =>
@debugMessage "incomming message #{JSON.stringify(message)}"
callback(message)
outgoing : (message, callback) =>
@debugMessage "outgoing message #{JSON.stringify(message)}"
if message.channel == "/meta/subscribe"
subscription = @subscriptions[message.subscription]['opts']
# Attach the signature and timestamp to subscription messages
message.ext = {} unless message.ext?
message.ext.danthes_signature = subscription.signature
message.ext.danthes_timestamp = subscription.timestamp
callback(message)
# Initialize Faye client
@connectToFaye: ->
if @server && Faye?
@debugMessage 'trying to connect faye'
@fayeClient = new Faye.Client(@server, @connectionSettings)
@fayeClient.addExtension(@fayeExtension)
# Disable any features what we want
@fayeClient.disable(key) for key in @disables
@debugMessage 'faye connected'
callback(@fayeClient) for callback in @fayeCallbacks
# Sign to channel
# @param [Object] options for signing
@sign: (options) ->
@debugMessage 'sign to faye'
@server = options.server unless @server
channel = options.channel
unless @subscriptions[channel]?
@subscriptions[channel] = {}
@subscriptions[channel]['callback'] = options['callback'] if options['callback']?
@subscriptions[channel]['opts'] =
signature: options['signature']
timestamp: options['timestamp']
# If we have 'connect' or 'error' option then force channel activation
if options['connect']? || options['error']?
@activateChannel channel, options
# Activating channel subscription
# @param channel [String] channel name
# @param options [Object] subscription callback options
@activateChannel: (channel, options = {}) ->
return true if @subscriptions[channel]['activated']
@faye (faye) =>
subscription = faye.subscribe channel, (message) => @handleResponse(message)
if subscription?
@subscriptions[channel]['sub'] = subscription
subscription.callback =>
options['connect']?(subscription)
@debugMessage "subscription for #{channel} is active now"
subscription.errback (error) =>
options['error']?(subscription, error)
@debugMessage "error for #{channel}: #{error.message}"
@subscriptions[channel]['activated'] = true
# Handle response from Faye
# @param [Object] message from Faye
@handleResponse: (message) ->
if message.eval
eval(message.eval)
channel = message.channel
return unless @subscriptions[channel]?
if callback = @subscriptions[channel]['callback']
callback(message.data, channel)
# Disable transports
# @param [String] name of transport
@disableTransport: (transport) ->
return unless transport in ['websocket', 'long-polling', 'callback-polling', 'in-process']
unless transport in @disables
@disables.push(transport)
@debugMessage "#{transport} faye transport will be disabled"
true
# Subscribe to channel with callback
# @param channel [String] Channel name
# @param callback [Function] Callback function
# @param options [Object] subscription callbacks options
@subscribe: (channel, callback, options = {}) ->
@debugMessage "subscribing to #{channel}"
if @subscriptions[channel]?
@activateChannel(channel, options)
# Changing callback on every call
@subscriptions[channel]['callback'] = callback
else
@debugMessage "Cannot subscribe on channel '#{channel}'. You need sign to channel first."
return false
true
# Unsubscribe from channel
# @param [String] Channel name
# @param [Boolean] Full unsubscribe
@unsubscribe: (channel, fullUnsubscribe = false) ->
@debugMessage "unsubscribing from #{channel}"
if @subscriptions[channel] && @subscriptions[channel]['activated']
@subscriptions[channel]['sub'].cancel()
if fullUnsubscribe
delete @subscriptions[channel]
else
delete @subscriptions[channel]['activated']
delete @subscriptions[channel]['sub']
# Unsubscribe from all channels
@unsubscribeAll: ->
@unsubscribe(channel) for channel, _ of @subscriptions
window.Danthes.reset()