Created
September 29, 2014 16:48
-
-
Save developerworks/842fbc5b6092b3c5823e to your computer and use it in GitHub Desktop.
Strophe.js Client Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | |
<meta http-equiv="Content-Language" content="zh-CN"/> | |
<title>Strophe.js Basic Example</title> | |
<script src='jquery.min.js'></script> | |
<script src='../strophe.js'></script> | |
<script src='basic.js'></script> | |
<script src="basic_lab.js"></script> | |
</head> | |
<body> | |
<div id='login' style='text-align: center'> | |
<form name='cred'> | |
<label for='jid'>JID:</label> | |
<input type='text' id='jid' value="root@xmpp.hezhiqiang.info"> | |
<label for='pass'>Password:</label> | |
<input type='password' id='pass' value="loader"> | |
<input type='button' id='connect' value='connect'> | |
<input type="button" id="query" value="query"/> | |
<input type="button" id="getlast" value="getlast"/> | |
<input type="button" id="online" value="online"/> | |
<input type="button" id="composing" value="composing"/> | |
<input type="button" id="paused" value="paused"/> | |
<input type="button" id="active" value="active"/> | |
<input type="button" id="inactive" value="inactive"/> | |
<input type="button" id="gone" value="gone"/> | |
</form> | |
</div> | |
<hr> | |
<div id='log'></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formatXml(xml) { | |
var formatted = ''; | |
var reg = /(>)(<)(\/*)/g; | |
xml = xml.replace(reg, '$1\r\n$2$3'); | |
var pad = 0; | |
jQuery.each(xml.split('\r\n'), function (index, node) { | |
var indent = 0; | |
if (node.match(/.+<\/\w[^>]*>$/)) { | |
indent = 0; | |
} else if (node.match(/^<\/\w/)) { | |
if (pad != 0) { | |
pad -= 1; | |
} | |
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) { | |
indent = 1; | |
} else { | |
indent = 0; | |
} | |
var padding = ''; | |
for (var i = 0; i < pad; i++) { | |
padding += ' '; | |
} | |
formatted += padding + node + '\r\n'; | |
pad += indent; | |
}); | |
xml_escaped = formatted.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/ /g, ' ').replace(/\n/g, ''); | |
return xml_escaped; | |
} | |
// BOSH服务端点 | |
//var BOSH_SERVICE = 'http://xmpp.hezhiqiang.info:5280/http-bind' | |
//var BOSH_SERVICE = 'ws://xmpp.hezhiqiang.info:5280/ws-xmpp/' | |
var BOSH_SERVICE = 'ws://xmpp.hezhiqiang.info:5288' | |
// 连接 | |
var connection = null; | |
function log(msg, sent) { | |
if (sent) { | |
$('#log').prepend("<pre>" + msg + "</pre>"); | |
$('#log').prepend("<span style='color: green;background-color: white;'>SENT</span>:\n"); | |
} else { | |
$('#log').prepend("<pre>" + msg + "</pre>"); | |
$('#log').prepend("<span style='color: orangered;background-color: white;'>RECV</span>:\n"); | |
} | |
} | |
function rawInput(data) { | |
log(formatXml(data), false); | |
} | |
function rawOutput(data) { | |
log(formatXml(data), true); | |
} | |
// 连接事件处理 | |
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.disconnect(); | |
} | |
} | |
$(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'; | |
// 连接: | |
connection.connect( | |
$('#jid').get(0).value, // Jabber 标识 (用户名 Full JID) | |
$('#pass').get(0).value,// 密码 | |
onConnect | |
); | |
} else { | |
button.value = 'connect'; | |
connection.disconnect(); | |
} | |
}); | |
$('#query').bind('click', function () { | |
query(connection, 'info', 'xmpp.hezhiqiang.info', 'config'); | |
// query(connection, 'items','xmpp.hezhiqiang.info', 'config/acls'); | |
// var query = $iq({to: 'xmpp.hezhiqiang.info', from: 'user2@xmpp.hezhiqiang.info', type: 'get', id: '12345678'}) | |
// .c('query', {xmlns: 'http://jabber.org/protocol/disco#info'}); | |
// connection.send(query.tree()); | |
}); | |
$('#getlast').bind('click', function () { | |
get_last(); | |
}); | |
$('#online').bind('click', function () { | |
var button = $('#online').get(0); | |
if (button.value == 'online') { | |
button.value = 'offline'; | |
online(); | |
} else { | |
button.value = 'online'; | |
offline(); | |
} | |
}); | |
$('#composing').bind('click', function () { | |
Chat.changeStatus(CHAT_STATUS.COMPOSING); | |
}); | |
$('#paused').bind('click', function () { | |
Chat.changeStatus(CHAT_STATUS.PAUSED); | |
}); | |
$('#gone').bind('click', function () { | |
Chat.changeStatus(CHAT_STATUS.GONE); | |
}); | |
$('#inactive').bind('click', function () { | |
Chat.changeStatus(CHAT_STATUS.INACTIVE); | |
}); | |
$('#active').bind('click', function () { | |
Chat.changeStatus(CHAT_STATUS.ACTIVE); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var XMLNS = { | |
CHATSTATUS: 'chatstatus', | |
COMMANDS: 'commands', | |
DISCO_INFO: 'disco#info', | |
DISCO_ITEMS: 'disco#items', | |
/* | |
activity | |
address | |
amp | |
bookmarks | |
bytestreams | |
caps | |
chatstates | |
commands | |
compress | |
disco | |
feature-neg | |
file-transfer | |
geoloc | |
http-auth | |
httpbind | |
ibb | |
jabber_client | |
jabber_iq_last | |
jabber_iq_oob | |
jabber_iq_privacy | |
jabber_iq_register | |
jabber_iq_roster | |
jabber_iq_rpc | |
jabber_iq_version | |
jabber_server | |
jabber_server_dialback | |
jabber_x_conference | |
jabber_x_data | |
jabber_x_oob | |
jidescaping | |
linklocal | |
mood | |
muc | |
nick | |
offline | |
pep | |
pubsub | |
rc | |
rosterx | |
rsm | |
shim | |
si | |
sipub | |
soap | |
streams | |
tune | |
urn_ietf_params_xml_ns_xmpp-bind | |
urn_ietf_params_xml_ns_xmpp-e2e | |
urn_ietf_params_xml_ns_xmpp-sasl | |
urn_ietf_params_xml_ns_xmpp-session | |
urn_ietf_params_xml_ns_xmpp-stanzas | |
urn_ietf_params_xml_ns_xmpp-streams | |
urn_ietf_params_xml_ns_xmpp-tls | |
urn_xmpp_archive | |
urn_xmpp_attention | |
urn_xmpp_avatar | |
urn_xmpp_blocking | |
urn_xmpp_bob | |
urn_xmpp_captcha | |
urn_xmpp_delay | |
urn_xmpp_features_rosterver | |
urn_xmpp_jingle | |
urn_xmpp_jingle_apps_rtp | |
urn_xmpp_jingle_transports_ibb | |
urn_xmpp_jingle_transports_ice-udp | |
urn_xmpp_jingle_transports_raw-udp | |
urn_xmpp_jingle_transports_s5b | |
urn_xmpp_langtrans | |
urn_xmpp_media-element | |
urn_xmpp_pie | |
urn_xmpp_ping | |
urn_xmpp_receipts | |
urn_xmpp_sec-label | |
urn_xmpp_sm | |
urn_xmpp_ssn | |
urn_xmpp_time | |
urn_xmpp_xbosh | |
waitinglist | |
xdata-layout | |
xdata-validate | |
xhtml-im | |
*/ | |
}; | |
function getId() { | |
var t = new Date().getTime(); | |
return 'id-' + t; | |
} | |
var session = { | |
user: 'root', | |
domain: 'xmpp.hezhiqiang.info' | |
}; | |
var jid = session.user + '@' + session.domain; | |
/** | |
* Execute a Disco query | |
*/ | |
var query = function query(connection, type, to, node) { | |
var stanza = $iq({ | |
type: 'get', | |
id: getId(), | |
to: to || session.domain, | |
from: jid | |
}); | |
var query_attributes = { | |
xmlns: 'http://jabber.org/protocol/disco#' + type | |
}; | |
if (node) { | |
query_attributes.node = node; | |
} | |
stanza.c('query', query_attributes); | |
connection.send(stanza.tree()); | |
}; | |
var query_info = function () { | |
var stanza = $iq({ | |
type: 'get', | |
id: getId(), | |
to: session.domain, | |
from: jid | |
}).c('query', {xmlns: 'http://jabber.org/protocol/disco#info'}); | |
connection.send(stanza.tree()); | |
}; | |
var query_items = function () { | |
var stanza = $iq({ | |
type: 'get', | |
id: getId(), | |
to: session.domain, | |
from: jid | |
}).c('query', {xmlns: 'http://jabber.org/protocol/disco#items'}); | |
connection.send(stanza.tree()); | |
}; | |
var get_cputime = function(){ | |
var stanza = $iq({ | |
type: 'get', | |
id: getId(), | |
to: session.domain, | |
from: jid | |
}).c('query', {xmlns: 'ejabberd:cputime'}); | |
connection.send(stanza.tree()); | |
}; | |
var query_protocal = function (name) { | |
var stanza = $iq({ | |
from: jid, | |
to: 'hezhiqiang@xmpp.hezhiqiang.info', | |
id: connect.getUniqueId(), | |
type: 'set' | |
}).c('query', {xmlns: 'http://jabber.org/protocal/bytestreams', sid: 'dv917fb4', mode: 'tcp'}) | |
.c('streamhost', {jid: jid, host: '192.168.8.104', port: ''}); | |
connection.send(stanza.tree()); | |
}; | |
var get_room_members = function () { | |
var stanza = $iq({ | |
type: 'get', | |
from: jid, | |
to: session.domain | |
}).c('query', {xmlns: 'http://jabber.org/protocol/muc#member-list'}); | |
connection.send(stanza.tree()); | |
}; | |
var get_commands = function (connection) { | |
var stanza = $iq({ | |
type: 'get', | |
to: session.domain, | |
from: jid, | |
id: getId() | |
}).c('query', {xmlns: 'http://jabber.org/protocol/disco#info', node: 'http://jabber.org/protocol/commands'}); | |
connection.send(stanza.tree()); | |
}; | |
// 获取好友列表 | |
var get_roster = function () { | |
var iq = $iq({ | |
type: 'get', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:roster'}); | |
connection.send(iq.tree()); | |
}; | |
// 删除好友 | |
var remove_contact = function (name) { | |
var iq = $iq({ | |
type: 'set', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:roster'}).c('item', {jid: name + '@' + session.domain, subscription: 'remove'}); | |
connection.send(iq.tree()); | |
}; | |
// 添加好友 | |
var add_contact = function (name) { | |
var iq = $iq({ | |
type: 'set', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:roster'}).c('item', {jid: name + '@' + session.domain, name: '名字' + name}); | |
connection.send(iq.tree()); | |
}; | |
// 更新好友信息 | |
var update_contact1 = function (name) { | |
var iq = $iq({ | |
type: 'set', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:roster'}).c('item', {jid: name + '@' + session.domain, name: '名字' + name}); | |
connection.send(iq.tree()); | |
}; | |
// 更新好友分组 | |
var update_contact2 = function (name) { | |
var iq = $iq({ | |
type: 'set', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:roster'}).c('item', {jid: name + '@' + session.domain, name: '名字' + name}).c('group').t('myFriends'); | |
connection.send(iq.tree()); | |
}; | |
// 更新联系人信息,如果传递了组,同时更新组信息 | |
var set_contact = function (name, group) { | |
var iq = $iq({ | |
type: 'set', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:roster'}).c('item', {jid: name + '@' + session.domain, name: '名字' + name}); | |
if (group !== undefined) { | |
iq.c('group').t(group); | |
} | |
connection.send(iq.tree()); | |
}; | |
var get_version = function () { | |
var iq = $iq({ | |
type: 'get', | |
// id: getId(), | |
to: session.domain | |
// from: session.user + '@' + session.domain | |
}).c('query', {xmlns: 'jabber:iq:version'}); | |
connection.send(iq.tree()); | |
}; | |
var exec_command = function (node, action) { | |
var iq = $iq({ | |
type: 'set', | |
id: getId(), | |
to: session.domain, | |
from: jid | |
}).c('command', {xmlns: 'http://jabber.org/protocol/commands', node: node, action: action}); | |
connection.send(iq.tree()); | |
}; | |
var get_last = function () { | |
var iq = $iq({ | |
type: 'get', | |
id: getId() | |
}).c('query', {xmlns: 'jabber:iq:last'}); | |
connection.send(iq.tree()); | |
}; | |
var get_server_command_list = function () { | |
var iq = $iq({ | |
type: 'get', | |
from: jid, | |
to: session.domain | |
}).c('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: 'http://jabber.org/protocol/commands'}); | |
connection.send(iq.tree()); | |
}; | |
var get_node = function (node) { | |
var iq = $iq({ | |
type: 'get', | |
from: jid, | |
to: session.domain | |
}).c('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: node}); | |
connection.send(iq.tree()); | |
}; | |
var admin_get_user_statistics = function () { | |
var iq = $iq({ | |
type: 'get', | |
id: getId(), | |
to: session.domain, | |
from: jid | |
}).c('command', {xmlns: 'http://jabber.org/protocol/commands', action: 'execute', node: 'http://jabber.org/protocol/admin#user-stats'}); | |
connection.send(iq.tree()); | |
}; | |
var subscribe = function (type, to) { | |
connection.send($pres({type: type, from: jid, to: to, xmlns: 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams'}).tree()); | |
}; | |
// 在线状态 | |
var online = function () { | |
var elementShow = Strophe.xmlElement('show', {}, 'chat'); | |
var elementStatus = Strophe.xmlElement('status', {}, '我在线,来戳我嘛'); | |
var presence = $pres({from: jid, xmlns: 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', version: '1.0'}) | |
.cnode(elementShow).up() | |
.cnode(elementStatus); | |
connection.send(presence.tree()); | |
}; | |
var offline = function () { | |
connection.send($pres({from: jid, type: 'unavailable'}).tree()); | |
}; | |
var away = function () { | |
var elementShow = Strophe.xmlElement('show', {}, 'away'); | |
var elementStatus = Strophe.xmlElement('status', {}, 'away'); | |
var presense = $pres({from: jid}) | |
.cnode(elementShow).up() | |
.cnode(elementStatus); | |
connection.send(presense.tree()); | |
}; | |
var xa = function () { | |
var elementShow = Strophe.xmlElement('show', {}, 'xa'); | |
var elementStatus = Strophe.xmlElement('status', {}, '我不在电脑旁了,稍后回来...'); | |
var presense = $pres({from: jid}) | |
.cnode(elementShow).up() | |
.cnode(elementStatus); | |
connection.send(presense.tree()); | |
}; | |
var busy = function () { | |
var elementShow = Strophe.xmlElement('show', {}, 'dnd'); | |
var elementStatus = Strophe.xmlElement('status', {}, 'do not disturb'); | |
var presense = $pres({from: jid}) | |
.cnode(elementShow).up() | |
.cnode(elementStatus); | |
connection.send(presense.tree()); | |
}; | |
var changeOnlineStatus = function (show, status) { | |
switch (show) { | |
case 'chat': | |
online(); | |
break; | |
case 'away': | |
away(); | |
break; | |
case 'xa': | |
xa(); | |
break; | |
case 'dnd': | |
busy(); | |
break; | |
} | |
} | |
/* | |
<!--对方正在键入--> | |
<message from="hezhiqiang@xmpp.hezhiqiang.info/hezhiqiang-2" | |
to="root@xmpp.hezhiqiang.info" type="chat" id="purple8185477c" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" | |
version="1.0"> | |
<composing xmlns="http://jabber.org/protocol/chatstates"/> | |
</message> | |
<!--暂停键入--> | |
<message from="hezhiqiang@xmpp.hezhiqiang.info/hezhiqiang-2" | |
to="root@xmpp.hezhiqiang.info" type="chat" id="purple81854775" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" | |
version="1.0"> | |
<paused xmlns="http://jabber.org/protocol/chatstates"/> | |
</message> | |
*/ | |
var CHAT_STATUS = { | |
ACTIVE: 'active', | |
COMPOSING: 'composing', | |
GONE: 'gone', | |
INACTIVE: 'inactive', | |
PAUSED: 'paused' | |
}; | |
var Chat = { | |
changeStatus: function (status) { | |
var msg = $msg({from: jid, to: 'hezhiqiang@xmpp.hezhiqiang.info', type: 'chat', xmlns: 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', version: '1.0'}) | |
.c(status, {xmlns: 'http://jabber.org/protocol/chatstates'}); | |
connection.send(msg.tree()); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment