Skip to content

Instantly share code, notes, and snippets.

@olvap
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save olvap/33eb964774c7921f3c4c to your computer and use it in GitHub Desktop.

Select an option

Save olvap/33eb964774c7921f3c4c to your computer and use it in GitHub Desktop.
#= require paho
freezer =
randomToken: ->
Math.random().toString(36).substr(2)
id: ->
$('body').data('freezer-id').replace(/-/g,"")
domain: ->
$('body').data('xively-credentials-domain')
xiserviceinstance: ->
$('body').data('xively-credentials-serviceinstance')
username: ->
$('body').data('xively-credentials-username')
password: ->
$('body').data('xively-credentials-password')
subscribe_channel: ->
"/v3/service_instances/#{ freezer.xiserviceinstance() }/domains/#{ freezer.domain() }/devices/#{ freezer.id() }/queues/messagesOUT"
paho =
setupClient: ->
paho.client ||= new Messaging.Client("v3mqtt.xively.com", 443, freezer.randomToken() )
connect: ->
options =
#connection attempt timeout in seconds
timeout: 3
userName: freezer.username()
password: freezer.password()
useSSL: true
#Gets Called if the connection has successfully been established
onSuccess: ->
console.log "Connected"
console.log freezer.subscribe_channel()
paho.client.subscribe(freezer.subscribe_channel(), qos: 0)
#Gets Called if the connection could not be established
onFailure: (message) ->
console.log "Connection failed: " + message.errorMessage
paho.client.connect options
paho.client.onMessageArrived = (message) ->
try
console.log message.payloadString
json = JSON.parse(message.payloadString)
catch e
console.log e
paho.client.onConnectionLost = (message) ->
console.log("Connection Lost")
if message != 0
paho.setupClient()
paho.connect()
$ ->
paho.setupClient()
paho.connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment