我使用node-xmpp连接到谷歌gcm ccs服务器.我跟着来自gcm谷歌群组进行连接.现在,每当我收到来自我的redis订阅者的消息(我订阅了redis频道redis节点包)时,我都需要发送下游消息.我的代码如下
var gearmanode = require('gearmanode'); var redis = require("redis"); var xmpp = require('node-xmpp'); var gearJob; var redisSubChan = 'test_channel'; var gearmanJobName = 'reverse'; var jobPayload; var redisClient; var xmppClient; var gearClient; gearClient = gearmanode.client(); var options = { type: 'client', jid: 'myid@gcm.googleapis.com', password: 'myserverkey', port: 5235, host: 'gcm.googleapis.com', legacySSL: true, preferredSaslMechanism: 'PLAIN' }; console.log('creating xmpp app'); xmppClient = new xmpp.Client(options); xmppClient.connection.socket.setTimeout(0) xmppClient.connection.socket.setKeepAlive(true, 10000) redisClient = redis.createClient(); redisClient.subscribe(redisSubChan); redisClient.on("message", function(channel, message) { console.log('received message'); console.log(message); message = JSON.parse(message); //send the messages to google ccs server via xmpp var payload = { "to": message.to, "message_id": message.message_id, "data": message.data, "time_to_live": message.time_to_live, "delay_while_idle": message.delay_while_idle }; var jsonPayload = JSON.stringify(payload); console.log(jsonPayload); var ackToDevice = new xmpp.Element('message', {'id': ''}).c('gcm', {xmlns: 'google:mobile:data'}).t(jsonPayload); console.log('prepared message'); console.log(ackToDevice.root().toString()); xmppClient.send(ackToDevice); console.log('sent!!!'); }); xmppClient.on('online', function() { console.log("online"); }); xmppClient.on('connection', function() { console.log('online'); }); xmppClient.on('stanza', function(stanza) { if (stanza.is('message') && stanza.attrs.type !== 'error') { // Best to ignore an error console.log("Message received"); //Message format as per here: https://developer.android.com/google/gcm/ccs.html#upstream var messageData = JSON.parse(stanza.getChildText("gcm")); if (messageData && messageData.message_type != "ack" && messageData.message_type != "nack") { var ackMsg = new xmpp.Element('message', {'id': ''}).c('gcm', {xmlns: 'google:mobile:data'}).t(JSON.stringify({ "to": messageData.from, "message_id": messageData.message_id, "message_type": "ack" })); //send back the ack. xmppClient.send(ackMsg); console.log("Sent ack"); //receive messages from ccs and give it to PHP workers gearClient.submitJob(gearmanJobName, JSON.stringify(messageData), {background: true}); } else { //Need to do something more here for a nack. console.log("message was an ack or nack...discarding"); } } else { console.log("error"); console.log(stanza); } }); xmppClient.on('authenticate', function(opts, cb) { console.log('AUTH' + opts.jid + ' -> ' + opts.password); cb(null); }); xmppClient.on('error', function(e) { console.log("Error occured:"); console.error(e); console.error(e.children); });
我能够从ccs服务器接收消息,但无法在消息回调时从redis发送下游消息.
我收到以下错误
error { name: 'message', parent: null, attrs: { id: '', type: 'error', to: '1026645507924@gcm.googleapis.com/8DF23ED7', 'xmlns:stream': 'http://etherx.jabber.org/streams' }, children: [ { name: 'gcm', parent: [Circular], attrs: [Object], children: [Object] }, { name: 'error', parent: [Circular], attrs: [Object], children: [Object] } ] }
我尝试在发送之前打印(遵循节点xmpp)xmpp节,并且相同
//记录我的消息
{"to":"APA91bHIGZcbePZ-f1jSyWQkBAJMHorHJiwgtN1GWITzcHf6uyVOZ3k7AasUiB-vBix32ucSypin3xDTYmoxqSc_ZmTDTuKjuDQ8BPQLpC41SqYRimm-hn34ZjSAF4uQO0OP1LSbqUxjh2WF0K5n4KyD3-Vn8ghASQ","message_id":84,"data":{"test":"sample data to send"},"time_to_live":0,"delay_while_idle":false}
正如他们在文档中提到的(请求格式).我的代码出了什么问题?