实现GAE XMPP服务作为现有XMPP服务器的外部组件(例如ejabberd或OpenFire)

com*_*tta 3 java google-app-engine xmpp openfire

我可能知道你们用什么集成技术来实现现有XMPP服务器的外部组件(例如ejabberd或OpenFire).是通过直接向另一个用户@ externaldomain发送xmpp消息还是使用urlfetch等机制?

sys*_*out 6

Google应用引擎(Gae)确实像CLIENT一样支持XMPP .

使用XMPP Gae JAVA客户端功能,您可以:

发信息

JID jid = new JID("youraccount@jabber.org");
Message msg = new MessageBuilder()
    .withRecipientJids(jid)
    .withBody("Hello i'm a fancy GAE app, how are you?")
    .build();                    
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
if (xmpp.getPresence(jid).isAvailable()) {
   SendResponse status = xmpp.sendMessage(msg);               
}
Run Code Online (Sandbox Code Playgroud)

收到消息

public class XMPPReceiverServlet extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res)
          throws IOException {
    XMPPService xmpp = XMPPServiceFactory.getXMPPService();
    Message message = xmpp.parseMessage(req);    
    JID fromJid = message.getFromJid();
    String body = message.getBody();
    //Save to Big Table
  }
}
Run Code Online (Sandbox Code Playgroud)

请记住,JID只能是yourappid@appspot.com或者是foo@yourappid.appspotchat.com, 因为Google域名尚不支持.

例如,您可以使用以下简单页面制作玩具Gae应用程序:

  1. 用于发送文本的html表单
  2. 一个html表,显示接收并存储到大表的消息列表.

要测试您的应用程序:

  1. 在jabber.org上创建一个帐户
  2. 下载Smack
  3. 尝试从Smack发送消息到yourappid@appspot.com
  4. 尝试从Gae App发送消息到youraccount@jabber.org

如果您的个人XMPP服务器(openfire)启动并运行,只需跳过步骤1并使用您的域帐户接收来自您的花式Gae应用程序的消息.

查看XMPP 消息传递以了解其工作原理.