gpt4 book ai didi

android - 使用 Node js 向 google ccs 发送下游消息

转载 作者:IT老高 更新时间:2023-10-28 23:27:40 27 4
gpt4 key购买 nike

我正在使用 node-xmpp连接到 google gcm ccs 服务器。我关注了from gcm google groups连接。现在,每当我收到来 self 的 redis 订阅者的消息(我订阅了一个 redis channel redis node package)时,我都需要发送一条下游消息。我的代码如下

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] } ] }

我尝试在发送之前打印(跟随 node xmpp )xmpp 节,并且它相同

//log of my message

<message id=""><gcm xmlns="google:mobile:data">{"to":"APA91bHIGZcbePZ-f1jSyWQkBAJMHorHJiwgtN1GWITzcHf6uyVOZ3k7AasUiB-vBix32ucSypin3xDTYmoxqSc_ZmTDTuKjuDQ8BPQLpC41SqYRimm-hn34ZjSAF4uQO0OP1LSbqUxjh2WF0K5n4KyD3-Vn8ghASQ","message_id":84,"data":{"test":"sample data to send"},"time_to_live":0,"delay_while_idle":false}</gcm></message>

正如他们在文档中提到的(Request format)。我的代码有什么问题?

最佳答案

我遇到了同样的问题。它几乎让我发疯,但最终它只是一个无效的 JSON 格式错误。

我想 messageData.from 或 messageData.message_id 未转换为正确的 JSON 格式。就我而言,我传递了一个标量,而 JSON.stringify() 没有将其转换为字符串。因此结果是 --> "message_id": 1234 而不是 "message_id": "1234"

关于android - 使用 Node js 向 google ccs 发送下游消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22606366/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com