美文网首页ejabberd手机移动程序开发程序员
node.js使用node-xmpp进行xmpp通信

node.js使用node-xmpp进行xmpp通信

作者: 龙腾九天ly | 来源:发表于2017-01-05 09:01 被阅读579次

博主CSDN昵称:守护者ly,欢迎大家前去指点

最近写了一个Node.js使用node-xmpp的例子,实现了登录、收发消息和消息的解析,贴出来与大家参详一下。
我使用的node-xmpp版本为1.0.4,在创建好node.js项目后,可以运行npm install node-xmpp进行安装,在此我就不再赘述了。实现通信还需要配置ejabberd服务器,这个网上有资料,请自行谷歌。

将node-xmpp引入项目后,首先声明变量

    //version 1.0.4  
    var XMPP = require('node-xmpp'); 

接下来配置连接属性

var cl = new XMPP.Client({  
    //用户名  
    jid: 'lf@h.com',  
    //密码  
    password: '111111',  
    reconnect: true,  
    //ejabberd服务器地址  
    host: '192.168.26.38',  
    //端口号  
    port: '5222'  
});

然后就可以登录了。我下面的代码实现了登陆后向一个名为“ly@h.com”人发送一条"fighting!"的消息,然后开启接收消息的监听

// Do things when online  
    cl.on('online', function () {  
        console.log("We're online!");  
        // Set client's presence  
        cl.send(new XMPP.Element('presence', {type: 'available'}).c('show').t('chat'));  

        // Send keepalive  
        setInterval(function () {  
            cl.send(' ');  
        }, 30000);  

    var chatMessage = new XMPP.Element('message', {to: 'ly@h.com', type:'chat'})
        //添加名为body的标签,并设置内容为fighting!  up()表示添加的标签不作为子标签           
        .c('body').t('fighting!').up()        
        //添加名为userinfo的标签,与body标签同级别          
        .c('userinfo', {'xmlns':'extension'})          
        //添加作为userinfo子标签的名为name的标签          
        .c('name').t("lf");  
    cl.send(chatMessage.tree());  
    //监听方法  
        cl.on('stanza', function(message) {  
            console.log('message get'+ message);  
            //此if判断无效  
            if(message.attr.type == 'message'){  
                console.log('ok');  
            }  
            if(message.is('message')){  
                console.log('get');  
                console.log("type = " + message.type);  
                var userinfo = message.getChild('userinfo');  
                var name;  
                if(userinfo != null){  
                    name = userinfo.getChild('name');  
                    console.log("name = "+name);  
                    if(name != null){  
                        console.log("name text = " + name.getText());  
                    }  
                }  
                console.log("body = " + message.getChild('body').getText());  
            }  
        });  
    });  

在接收端接收到的消息内容如下:

    <message to='ly@h.com' from='lf@h.com/14713727713775752643754' type='chat'><body>fighting!</body><thread>376fd9b5-c908-4b04-a7ae-9e3bc6f07265</thread><userinfo xmlns='extension'><name>lf</name></userinfo></message>

其中接到的消息message,可以使用"message.from"获取到消息的发送人,使用"message.getChild('body')“方法获取到消息内容。我在Android端发送消息时又新增了名为"userinfo"的标签,又在其中加入了name标签,android端消息代码如下:

    Message message = new Message("lf@h.com");  
    message.addBody(null, "I must be willing to fight!");  
    message.addSubject("favorite color", "red");  
    DefaultExtensionElement defaultExtensionElement = new DefaultExtensionElement(  
    "userinfo","extension");  
    defaultExtensionElement.setValue("name","ly");  
    message.addExtension(defaultExtensionElement);

ly@h.com用户向lf@h.com回复了一条"I must be willing to fight!"并在name标签中署名ly。具体接收到的消息如下:

    <message from="ly@h.com/Smack" to="lf@h.com" xml:lang="en" id="gMXeR-15" type="chat" xmlns:stream="http://etherx.jabber.org/streams"><subject xml:lang="favorite color">red</subject><body>I must be willing to fight!</body><thread>8902450d-4624-484c-8b0e-454db8243750</thread><userinfo xmlns="extension"><name>ly</name></userinfo></message>

以上并非全部node.js代码,但是关键方法已经给出,用兴趣的朋友可以试试。如有不足之处,请大家不吝指教!

相关文章

  • node.js使用node-xmpp进行xmpp通信

    博主CSDN昵称:守护者ly,欢迎大家前去指点 最近写了一个Node.js使用node-xmpp的例子,实现了登录...

  • IOS 问题合集 面试

    是否使用过XMPP,XMPP的实现? 环信是一个即时通信的服务提供商 环信使用的是XMPP协议,它是再XMPP的基...

  • smack使用指南(译)(一)Smack概述

    Smack是一个用于与XMPP服务器进行通信的库,用于执行实时通信,包括即时消息和群聊。 主要优势 使用起来非常简...

  • 即时通讯

    1、是否使用过XMPP,XMPP的实现原理 1、XMPP是一个即时通讯的协议,它规范了用于即时通信在网络上数据传输...

  • iOS 即时通信系列之XMPP搭建本地服务器

    说明:本文主要阐述如何使用XAMPP、Openfire工具基于XMPP搭建本地服务器、实现即时通信。 即时通信 什...

  • 专利

    基于xmpp通信和定位功能,使得所有使用飞常准的用户都可以加好友并可以聊天,点击好友添加根据地理位置进行添加,对于...

  • 即时通讯

    XMPP 一、 实现原理 XMPP是一个即时通讯的协议,是建立在Socket通信基础上的,它规范了用于即时通信...

  • iOS开发即时通讯环境搭建-XMPP

    即时通信是一个实时通信系统,允许两人或多人使用网络实时的传递文字,消息,文件,语音与视频交流。 关于XMPP XM...

  • iOS-进阶整理10 - 即时通信1 XMPP openfire

    即时通信(Instant Messaging),就是扣扣,微信它有多种实现方式,如XMPP、环信、融云等。XMPP...

  • XMPP通信原理

    本文章对XMPP通信原理做基本的介绍,不讲具体代码。在面试的时候会经常听到:能介绍一下XXXX原理吗?当然面试不一...

网友评论

    本文标题:node.js使用node-xmpp进行xmpp通信

    本文链接:https://www.haomeiwen.com/subject/hroavttx.html