XMPP环境搭建常见错误

(1)首先安装openfire时,不要忘记开启脚本语法: 

                 

         (2)建立用户安装客户端进行测试,我使用的是Psi不过这个不要忘记设置host,在Connetion指定手动连接host

         (3)openfire在重启时候可能出现log找不到错误,使用管理员账号运行就好了。。

           (4)下面是连接的设置部分:

 首先下载Strophe.js使用里面的Examples的例子就行了。。。

              在apache中设置反向代理:        

开启proxy proxy_http 模块,在文件最后添加以下代码:
ProxyRequests Off
ProxyPass /http-bind http://127.0.0.1:7070/http-bind/
ProxyPassReverse /http-bind http://127.0.0.1:7070/http-bind/
        (5)设置Bosh_SERVICE = ‘/http-bind'

现在可以启动basic.html查看到如下

       (6)建立测试通信程序: 修改basic.html如下所示。。。

           

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Strophe.js Echobot Example</title>
    <script language='javascript'      type='text/javascript'  src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'></script>
    <script language='javascript'      type='text/javascript'  src='../strophe.js'></script>
    <script language='javascript'      type='text/javascript'  src='basic.js'></script>
    <style>
        body {font-size:9pt}
        #his {width:620px; height:200px;overflow:auto; border: 1px dashed #000000;folat:left;}
        #ros {width:620px; height:100px;overflow:auto; border: 1px dashed #000000;folat:left;}
        .editbox,input[type="text"],input[type="password"] {
            background: #ffffff;
            border: 1px solid #b7b7b7;
            color: #003366;
            cursor: text;
            font-family: "arial";
            font-size: 9pt;
            height: 18px;
            padding: 1px;
        }
        .multibox {
            background: #f8f8f8;
            border-bottom: #B7B7B7 1px solid;
            border-left: #B7B7B7 1px solid;
            border-right: #B7B7B7 1px solid;
            border-top: #B7B7B7 1px solid;
            color: #000000;
            cursor: text;
            font-family: "Arial";
            font-size: 9pt;
            padding: 1px;
        }
    </style>
</head>
<body>
    <p></p>
    <div id='login'>
        <form name='cred'>
          <label for='jid'>JID:</label>
          <input type='text' id='jid' value="user1@localhost" />
          <label for='pass'>Password:</label>
          <input type='password' id='pass' value="123456"/>
          <input type='button' id='connect' value='connect' />
        </form>
    </div>
    <p></p>
    <div id="his"></div>
        <p></p>
        <div id="inputarea">
            <label for='tojid'>TOJID:</label>
            <input type='text' id='tojid' value="admin@testserver"/>
            <br/>
            <textarea id="msg" rows="3" cols="120" class="multibox"></textarea>
            <input type="button" id="send" value="send" />
            <input type="button" id="roster" value="roster" />
        </div>
    <hr />
    <div id='log'></div>
</body>
</html>
    修改basic.js代码如下

var BOSH_SERVICE = '/http-bind';
var connection = null;
var toId=null;
var fromId=null;
           
Strophe.log = function (level, msg) {
    //$('#log').append('<div></div>').append(document.createTextNode(msg));
};
           
function rawInput(data)
{
    log('RECV: ' + data);
}
           
function rawOutput(data)
{
    log('SENT: ' + data);
}
           
           
function log(msg)
{
    //$('#log').append('<div></div>').append(document.createTextNode(msg));
}
           
function onConnect(status)
{
    if (status == Strophe.Status.CONNECTING) {
        log('Strophe is connecting.');
    } else if (status == Strophe.Status.CONNFAIL) {
        log('Strophe failed to connect.');
        $('#connect').get(0).value = 'connect';
    } else if (status == Strophe.Status.DISCONNECTING) {
        log('Strophe is disconnecting.');
    } else if (status == Strophe.Status.DISCONNECTED) {
        log('Strophe is disconnected.');
        $('#connect').get(0).value = 'connect';
    } else if (status == Strophe.Status.CONNECTED) {
        log('Strophe is connected.');
        connection.addHandler(onMessage, null, 'message', null, null,  null);
        //connection.addHandler(onRoster, null , 'iq' , null , null , null); 
                   
        connection.send($pres().tree());
    }
}
           
/*
 * 消息处理
 */
function onMessage(msg) {  
    to = msg.getAttribute('from');
    var from = msg.getAttribute('from');
    var type = msg.getAttribute('type');
    var elems = msg.getElementsByTagName('body');
           
    if (type == "chat" && elems.length > 0) {
        var body = elems[0];
        appendToHis(new Date().toLocaleTimeString() + '  ' + from + ' say: ' + Strophe.getText(body));
    }
    return true;
}
           
function appendToHis(msg){
    $('#his').append('<div>' + msg + '</div>');
    $('#his').attr("scrollTop", $('#his').attr("scrollHeight"));
}
           
$(document).ready(function () {
    connection = new Strophe.Connection(BOSH_SERVICE);
    connection.rawInput = rawInput;
    connection.rawOutput = rawOutput;
           
    $('#connect').bind('click', function () {
        var button = $('#connect').get(0);
        if (button.value == 'connect') {
            button.value = 'disconnect';
            //获取用户名和密码
            fromId = $('#jid').val();
            toId = $('#tojid').val();
            //连接服务器
            connection.connect($('#jid').get(0).value,
                       $('#pass').get(0).value,
                       onConnect);
        } else {
            button.value = 'connect';
            connection.disconnect();
        }
    });
                   
    $('#send').bind('click', function () {
        msg=$('#msg').val();
           
        toId = $('#tojid').val();
        var reply = $msg({to: toId, from: fromId , type: 'chat'}).cnode(Strophe.xmlElement('body', '' ,msg));
        connection.send(reply.tree());
           
        appendToHis(new Date().toLocaleTimeString() + "  Me:  "  + msg);
        $('#msg').val('');
    });
           
    $('#msg').keypress(function(e){
        if(e.which==13){
            $('#send').click();
        }
    });      
});
现在就可以使用网页打开。。。两个客户端就可以通信了。。可以打开log查看输出信息



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值