提交 84ed1397 编写于 作者: J Javan Makhmali

Cable.Consumer

上级 fa0281ae
#= require_self
#= require cable/subscriber_manager
#= require cable/connection
#= require cable/subscription
#= require cable/consumer
class @Cable
@PING_IDENTIFIER: "_ping"
@Cable =
PING_IDENTIFIER: "_ping"
constructor: (@url) ->
@subscribers = new Cable.SubscriberManager this
@connection = new Cable.Connection this
createSubscription: (channelName, mixin) ->
channel = channelName
params = if typeof channel is "object" then channel else {channel}
new Cable.Subscription this, params, mixin
send: (data) ->
@connection.send(data)
createConsumer: (url) ->
new Cable.Consumer url
#= require cable/connection_monitor
class Cable.Connection
constructor: (@cable) ->
new Cable.ConnectionMonitor @cable
constructor: (@consumer) ->
new Cable.ConnectionMonitor @consumer
@connect()
send: (data) ->
......@@ -17,7 +17,7 @@ class Cable.Connection
@createWebSocket()
createWebSocket: ->
@websocket = new WebSocket(@cable.url)
@websocket = new WebSocket(@consumer.url)
@websocket.onmessage = @onMessage
@websocket.onopen = @onConnect
@websocket.onclose = @onClose
......@@ -33,10 +33,10 @@ class Cable.Connection
onMessage: (message) =>
data = JSON.parse message.data
@cable.subscribers.notify(data.identifier, "received", data.message)
@consumer.subscribers.notify(data.identifier, "received", data.message)
onConnect: =>
@cable.subscribers.reload()
@consumer.subscribers.reload()
onClose: =>
@disconnect()
......@@ -48,5 +48,5 @@ class Cable.Connection
@websocket?.readyState is 1
disconnect: ->
@cable.subscribers.notifyAll("disconnected")
@consumer.subscribers.notifyAll("disconnected")
@removeWebsocket()
......@@ -4,9 +4,9 @@ class Cable.ConnectionMonitor
identifier: Cable.PING_IDENTIFIER
constructor: (@cable) ->
constructor: (@consumer) ->
@reset()
@cable.subscribers.add(this)
@consumer.subscribers.add(this)
@pollConnection()
connected: ->
......@@ -35,7 +35,7 @@ class Cable.ConnectionMonitor
reconnect: ->
console.log "Ping took too long to arrive. Reconnecting.."
@connectionAttempts += 1
@cable.connection.connect()
@consumer.connection.connect()
now = ->
new Date().getTime()
#= require cable/connection
#= require cable/subscription
#= require cable/subscriber_manager
class Cable.Consumer
constructor: (@url) ->
@subscribers = new Cable.SubscriberManager this
@connection = new Cable.Connection this
createSubscription: (channelName, mixin) ->
channel = channelName
params = if typeof channel is "object" then channel else {channel}
new Cable.Subscription this, params, mixin
send: (data) ->
@connection.send(data)
class Cable.SubscriberManager
constructor: (@cable) ->
constructor: (@consumer) ->
@subscribers = []
add: (subscriber) ->
......@@ -33,4 +33,4 @@ class Cable.SubscriberManager
sendCommand: (subscriber, command) ->
{identifier} = subscriber
return true if identifier is Cable.PING_IDENTIFIER
@cable.send({command, identifier})
@consumer.send({command, identifier})
class Cable.Subscription
constructor: (@cable, params = {}, mixin) ->
constructor: (@consumer, params = {}, mixin) ->
@identifier = JSON.stringify(params)
extend(this, mixin)
@cable.subscribers.add(this)
@consumer.subscribers.add(this)
# Perform a channel action with the optional data passed as an attribute
perform: (action, data = {}) ->
......@@ -10,10 +10,10 @@ class Cable.Subscription
@send(data)
send: (data) ->
@cable.send(command: "message", identifier: @identifier, data: JSON.stringify(data))
@consumer.send(command: "message", identifier: @identifier, data: JSON.stringify(data))
unsubscribe: ->
@cable.subscribers.remove(this)
@consumer.subscribers.remove(this)
extend = (object, properties) ->
if properties?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册