2013-11-15 00:34:29 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
nforce = require('nforce')
|
|
|
|
faye = require('faye')
|
|
|
|
|
|
|
|
namespace = require('node-namespace')
|
|
|
|
|
2013-11-16 02:00:48 +08:00
|
|
|
namespace 'SF', ->
|
2013-11-15 00:34:29 +08:00
|
|
|
class @SFClient
|
|
|
|
constructor: (opts) ->
|
|
|
|
@client = null
|
|
|
|
@outboundMessages = []
|
|
|
|
@sfuser = opts.sfuser
|
|
|
|
@sfpass = opts.sfpass
|
|
|
|
@orgCreds = opts.orgCredentials
|
|
|
|
@org = nforce.createConnection(@orgCreds)
|
|
|
|
|
|
|
|
_processOutboundMessages: () ->
|
|
|
|
# Do work here
|
|
|
|
|
|
|
|
_handleStreamingAPI: (outboundCB) ->
|
|
|
|
client = new faye.Client(@oauth.instance_url + '/cometd/28.0')
|
|
|
|
client.setHeader("Authorization", "OAuth #{ @oauth.access_token }")
|
|
|
|
|
|
|
|
subscription = client.subscribe('/topic/SpheroMsgOutbound', outboundCB)
|
|
|
|
console.log("Streaming API Connected...")
|
|
|
|
|
|
|
|
authenticate: (outboundCB) ->
|
|
|
|
@org.authenticate({ username: @sfuser, password: @sfpass}, (err, _oauth) =>
|
|
|
|
if(err)
|
|
|
|
console.error('unable to authenticate to sfdc')
|
|
|
|
console.log(err)
|
|
|
|
process.exit(code=0)
|
|
|
|
else
|
|
|
|
console.log("authenticated")
|
|
|
|
@oauth = _oauth
|
|
|
|
@_handleStreamingAPI(outboundCB)
|
|
|
|
@_processOutboundMessages()
|
|
|
|
)
|
|
|
|
|
|
|
|
push: (msg) ->
|
|
|
|
#jsonBody = JSON.parse(msg)
|
|
|
|
#jsonString = JSON.stringify(msg)
|
|
|
|
jsonString = msg
|
2013-11-16 06:51:49 +08:00
|
|
|
console.log("SpheroController post msg:")
|
|
|
|
console.log(msg)
|
2013-11-15 00:34:29 +08:00
|
|
|
@org.apexRest({uri:'SpheroController', method: 'POST', body: jsonString}, @oauth, (err,resp) =>
|
|
|
|
if(err)
|
|
|
|
console.log(err)
|
|
|
|
else
|
|
|
|
console.log(resp)
|
|
|
|
)
|