Google Talk 接收具有不同 jid 资源的消息
我想我做错了什么。 我想向我的 GTalk id 发送 XMPP 消息,但我不希望 GTalk 应用程序接收该消息,因此我正在更改收件人 JID 的资源。
我的问题是 GTalk 正在接收所有消息,尽管它们有不同的资源。
我的代码:
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
JID jid = msg.getFromJid();
String body = msg.getBody();
String jidID = jid.getId().split("/")[0];
JID jid2 = new JID(jidID+"/myownresource453242352");
String response = jid2.getId() + " " + body;
// Send out response
msg = new MessageBuilder().withRecipientJids(jid2).withBody(response).build();
xmpp.sendMessage(msg);
}
输出:
怎么了?
更新:
现在我正在将消息发送到 [email protected]/bot aSmack 客户端,它正在我的客户端重新向我发送消息。
问题是 GTalk for Gmail 和 GTalk for Android 正在注册所有发送的消息,但它们没有收到应用程序响应。其他客户端不会显示我未与他们一起发送的消息。
我可以隐藏发送至 Gmail 和 Android 的邮件吗?
我的代码:
SERVER
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
LOG.setLevel(Level.INFO);
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
LOG.info(msg.getStanza());
JID jid = msg.getFromJid();
String body = msg.getBody();
String response = "FullID: "+jid.getId()+" El mensaje recibido es: "+body;
// Send out response
msg = new MessageBuilder().
withRecipientJids(jid)
.withMessageType(MessageType.NORMAL)
.withBody(response)
.build();
xmpp.sendMessage(msg);
}
CLIENT:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connectionConfiguration);
try {
Log.i("TAG","Trying to connect");
connection.connect();
Log.i("TAG","Connected");
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
Log.i("TAG","Trying to Log In");
connection.login("[email protected]",mypass, mires");
Log.i("TAG","Logged In");
} catch (XMPPException e) {
e.printStackTrace();
Log.i("TAG","Problem connecting or logging in");
}
//Creating chat object for processing friend chat
Chat chat = connection.getChatManager().createChat(Server, new MessageListener() {
//Overriding process message function of MessageListener Interface which will be
//called whenever a message is received
@Override
public void processMessage(Chat c, Message m) {
//Displaying message sent by friend
//System.out.println(friendId+ " : " + m.getBody());
Log.i("TAG", m.getBody());
message = m.getBody();
}
});
try {
Message out = new Message();
out.setBody("Definitivo22222222");
out.setType(Type.normal);
chat.sendMessage(out);
Log.i("TAG", "Mensaje enviado");
} catch (XMPPException e) {
Log.i("TAG", "No se envió el mensaje");
e.printStackTrace();
}
最后一件事:我在 AppEngine 日志中看到,从 aSmack 收到的 Stanza 不是普通类型,而是聊天类型。
感谢您的帮助!
最后一件事:您可以通过同时从任何客户端和 Gmail 进行连接并从客户端进行对话来测试 Gmail 正在执行的操作。 Gmail 正在接收您的邮件。
再次感谢。
另一件事: 我的目标是使用 XMPP 与游戏的 2 个客户端及其 Gmail 帐户进行通信。你知道替代方案吗?
I think I'm doing something wrong.
I want to send a XMPP message to my GTalk id but I don't want that the GTalk app receives the message so I'm changing the resource of the recipient JID.
My problem is that GTalk is receiving all the messages although thay have different resource.
My code:
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
JID jid = msg.getFromJid();
String body = msg.getBody();
String jidID = jid.getId().split("/")[0];
JID jid2 = new JID(jidID+"/myownresource453242352");
String response = jid2.getId() + " " + body;
// Send out response
msg = new MessageBuilder().withRecipientJids(jid2).withBody(response).build();
xmpp.sendMessage(msg);
}
The output:
Rafa Espillaque, 18:33 -
You shouldn't respond![email protected], 18:33 -
[email protected]/myownresource453242352 You shouldn't respond!
What's wrong?
UPDATE:
Now I'm sending messages to [email protected]/bot from an aSmack client and it is resending the message to me at my client.
The problem is GTalk for Gmail and GTalk for Android is registering all sent messages but they don't receive the app responses. Other clients don't show the messages I don't sent with them.
Will I be able to hide my messages to Gmail and Android?
My code:
SERVER
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
LOG.setLevel(Level.INFO);
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
LOG.info(msg.getStanza());
JID jid = msg.getFromJid();
String body = msg.getBody();
String response = "FullID: "+jid.getId()+" El mensaje recibido es: "+body;
// Send out response
msg = new MessageBuilder().
withRecipientJids(jid)
.withMessageType(MessageType.NORMAL)
.withBody(response)
.build();
xmpp.sendMessage(msg);
}
CLIENT:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connectionConfiguration);
try {
Log.i("TAG","Trying to connect");
connection.connect();
Log.i("TAG","Connected");
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
Log.i("TAG","Trying to Log In");
connection.login("[email protected]",mypass, mires");
Log.i("TAG","Logged In");
} catch (XMPPException e) {
e.printStackTrace();
Log.i("TAG","Problem connecting or logging in");
}
//Creating chat object for processing friend chat
Chat chat = connection.getChatManager().createChat(Server, new MessageListener() {
//Overriding process message function of MessageListener Interface which will be
//called whenever a message is received
@Override
public void processMessage(Chat c, Message m) {
//Displaying message sent by friend
//System.out.println(friendId+ " : " + m.getBody());
Log.i("TAG", m.getBody());
message = m.getBody();
}
});
try {
Message out = new Message();
out.setBody("Definitivo22222222");
out.setType(Type.normal);
chat.sendMessage(out);
Log.i("TAG", "Mensaje enviado");
} catch (XMPPException e) {
Log.i("TAG", "No se envió el mensaje");
e.printStackTrace();
}
Last thing: I've seen in AppEngine Logs that the Stanza received from aSmack isn't of normal type but chat type.
Thanks for helping!!
Last-last thing: You can test what Gmail is doing by connecting from any client and Gmail at same time and talking from the client. Gmail is receiving your messages.
Thanks again.
Another thing:
My goalis use XMPP to communicate 2 clients of a game with their gmail account. Do you know an alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 RFC 6120 第 10.5.4 节:
如果您发送到无效资源,服务器会将其视为您已将其发送到裸 JID。在 GoogleTalk 上,这适用于所有非负优先级资源。
See RFC 6120, section 10.5.4:
If you send to an invalid resource, the server treats it as if you had sent it to the bare JID. On GoogleTalk, this goes to all non-negative priority resources.
我认为这是设计使然。 IIRC GTalk 将给定 JID 的所有消息路由到该 JID 的所有连接资源。如果消息具有完整的 JID 作为
to
,情况也是如此。I think that this is by design. IIRC GTalk routes all messages for a given JID to all connected resources of the JID. This is even true if the message has a full JID as
to
.如果您发送带有 JID 和资源 (user@yoursever/reourcex) 的消息,那么接收服务器会将您的请求路由到预期的接收者。接收服务器将决定如何路由 JID/资源。
标准服务器将查找发送的 JID/资源的精确匹配。如果找到,它将按预期运行并且消息将被传递。另一方面,如果失败,它将把消息发送到优先级最高的在线资源。如果失败,它将离线存储该消息。
对于 Google Talk,就会发生这种情况:它将尝试匹配确切的资源,如果失败,则将其广播到所有在线资源。如果不包含资源,也同样适用。
If you send the message with JID and resource (user@yoursever/reourcex) then the receiving server will route your request to the intended receiver. The receiving server will decide how to route the JID/resource.
A standard server will look for an exact match of the sent JID/resource. If found then it will behave as expected and the message will be delivered. If that fails on the other hand, it will send the message to the highest priority online resource. If that fails, it will store the message offline.
In the case of Google Talk this will happen: it will try to match the exact resource, and if that fails, broadcast it to all of the online resources. The same is also applied if no resource included.