/*
* 参考《XMPP高级编程+使用JavaScript和jQuery》第三章例子。
* 我修改了作者的XMPP服务器到本地的openfire。JavaScript跨域请求,使用Nginx代理。另外,添加了些备注笔记。
*
* 几个概念
*
* 1) BOSH(Bidirectional streams Over Synchronous HTTP,在同步HTTP之上传送双向数据流),是一种XMPP的长轮询桥接技术
* 2) Comet(反向HTTP),同上也是长轮询一种
* 3) 很多XMPP服务器都内置了对BOSH的支持,这种服务叫做“BOSH连接管理器”,通常以URL http://example.com:5280/http-bind或者http://example.com:5280/xmpp-httpbind来提供服务。
* 4) 书上的连接管理器的测试URL为http://bosh.metajack.im:5280/xmpp-httpbind
*
*/
//命名空间对象(存储应用程序状态和全局变量)
var myNamespace = {
some_global: 0,
another_global: true,
my_adder: function(x, y){
return x+y;
}
};
// 创建自己的命名空间对象Hello
var Hello = {
connection: null,
start_time: null,
// 输出日志
log: function(msg){
$('#log').append(" "+msg+" ");
},
// 定义打招呼用的节内容,并连接server。连接成功以后调用handler操作。
send_ping: function(to){
var ping = $iq({
to: to,
type: "get",
id: "ping1"
}).c("ping", {xmlns: "urn:xmpp:ping"});
Hello.log("Sending ping to "+to+".");
Hello.start_time = (new Date()).getTime();
Hello.connection.send(ping);
},
// 连接后的handler方法
handle_pong: function(iq){
var elapsed = (new Date()).getTime() - Hello.start_time;
Hello.log("Received pong from server in "+elapsed+"ms");
Hello.connection.disconnect();
return false;//不销毁
}
}
// XMPP连接通过Strophe.Connection对象管理
$(document).ready(function(){
// 控制出发按钮,就出现id为login_dialog的div
$('#login_dialog').dialog({
autoopen: true,
draggable: false,
modal: true,
title: 'Connect to XMPP',
buttons: {
"Connect": function(){
$(document).trigger('connect', {//启动connect自定义事件,传入jid和password
jid: $('#jid').val(),
password: $('#password').val()
}); $('#password').val('');// 一旦触发,密码口令被清空
$(this).dialog('close');
}
}
});
// 1) 处理XMPP的connect事件,即创建Strophe.Connection对象并调用connect()方法。
// 2) 提供一个能够相应连接状态变化的回调函数
$(document).bind('connect', function(ev, data){
// var conn = new Strophe.Connection("http://bosh.metajack.im:5280/xmpp-httpbind");
var conn = new Strophe.Connection("http://timelyxyzmacbookpro.local:7070/http-bind/");
conn.connect(data.jid, data.password, function(status){
if(status === Strophe.Status.CONNECTED){
$(document).trigger('connected');
}else if(status === Strophe.Status.DISCONNECTED){
$(document).trigger('disconnected');
}
});
Hello.connection = conn;
}); $(document).bind('connected', function(){
Hello.log("Connection established.");// 通知用户 Hello.connection.addHandler(Hello.handle_pong, null, "iq", null, "ping1");// handler在send_ping里的send完成之后立马执行,此处只是提前声明handler
var domain = Strophe.getDomainFromJid(Hello.connection.jid);
console.log(domain);
Hello.send_ping(domain);
}); $(document).bind('disconnected', function(){
Hello.log("Connection terminated.");
Hello.connection = null;// 释放已销毁的connection对象引用
});
});

strophe与openfire模拟的XMPP简单hello程序的更多相关文章

  1. iOS中XMPP简单聊天实现 好友和聊天

    版权声明本文由陈怀哲首发自简书:http://www.jianshu.com/users/9f2e536b78fd/latest_articles;微信公众号:陈怀哲(chenhuaizhe2016) ...

  2. iOS开发拓展篇-XMPP简单介绍

    iOS开发拓展篇-XMPP简单介绍 一.即时通讯简单介绍 1.简单说明 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双 ...

  3. iOS开发——网络编程OC篇&(一)XMPP简单介绍与准备

    XMPP简单介绍与准备 一.即时通讯简单介绍 1.简单说明 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈 ...

  4. 自己模拟的一个简单的web服务器

    首先我为大家推荐一本书:How Tomcat Works.这本书讲的很详细的,虽然实际开发中我们并不会自己去写一个tomcat,但是对于了解Tomcat是如何工作的还是很有必要的. Servlet容器 ...

  5. Linux——模拟实现一个简单的shell(带重定向)

    进程的相关知识是操作系统一个重要的模块.在理解进程概念同时,还需了解如何控制进程.对于进程控制,通常分成1.进程创建  (fork函数) 2.进程等待(wait系列) 3.进程替换(exec系列) 4 ...

  6. python练习笔记——编写一个装饰器,模拟登录的简单验证

    编写一个装饰器,模拟登录的简单验证(至验证用户名和密码是否正确) 如果用户名为 root 密码为 123则正确,否则不正确.如果验证不通过则不执行被修饰函数 #编写一个装饰器,模拟登录的简单验证 #只 ...

  7. ACEXML解析XML文件——简单示例程序

    掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...

  8. 用python开发简单ftp程序

    根据alex老师视频开发的简单ftp程序,只能实现简单的get功能 ftp客户端程序: #!/usr/bin/env python #_*_ coding:utf-8 _*_ import socke ...

  9. 关于SIGSLOT的一个简单的程序

    废话少说直接看代码即可,这只是一个简单的程序,可以帮我们简单地明白SIGSLOT是怎么回事.至于深入研究自己去百度吧. #include "sigslot.h" using nam ...

随机推荐

  1. 绑定当前对象例子——Tag="{Binding}"

    <TreeView Margin="10,5,0,0" HorizontalAlignment="Left"  VerticalAlignment=&qu ...

  2. hdu 1536/ hdu 1944 S-Nim(sg函数)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. 51nod 1672 贪心/队列

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 1672 区间交 基准时间限制:1 秒 空间限制:131072 K ...

  4. 51nod 1010 stl/数论/二分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 1010 只包含因子2 3 5 基准时间限制:1 秒 空间限制:1 ...

  5. SQLyog中的计算适合的数据类型

    可能使用的数据库工作比较杂吧(机器上有toad.PLSQL Developer.Navicat.SQLyog等).并非是觉得那种都不好用,而是觉得有适合大部分需求的,但也有工具在某一方面特别方便的. ...

  6. pycharm配置PyQt5,以及创建第一个项目

    认你已经安装好了pycharm,也正确安装了PyQt5 否则,请移步https://www.cnblogs.com/longbigbeard/p/9628102.html来安装PyQt5 下一步,To ...

  7. 关于js序列化时间的方法

    var time = new Date(); var otime = getMyDate(time); //将毫秒转换成 年月日+时分秒 格式的 (1970-01-11 00:00:00) funct ...

  8. Eclipse插件开发_异常_01_java.lang.RuntimeException: No application id has been found.

    一.异常现象 在运行RCP程序时,出现 java.lang.RuntimeException: No application id has been found. at org.eclipse.equ ...

  9. JIRA 的安装和使用

    需要的软件 6.0.3-x32.exe jira_6.x_language_zh_CN.jar jira_crack.zip http://pan.baidu.com/s/1dEbpJc1 (从网盘下 ...

  10. SpringCloud教程 | 第五篇: 路由网关(zuul)

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...