Mercurial > xmpp-account-manager
comparison client.js @ 1:d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 21 Dec 2018 23:44:18 +0100 |
parents | 2a8d4e8600d0 |
children | 5aa1bf7154b0 |
comparison
equal
deleted
inserted
replaced
0:2a8d4e8600d0 | 1:d6df73b466f6 |
---|---|
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 const BOSH_SERVICE = 'https://bosh.linkmauve.fr/'; | |
4 | |
5 function rawInput(data) | |
6 { | |
7 console.log('RECV', data); | |
8 } | |
9 | |
10 function rawOutput(data) | |
11 { | |
12 console.log('SENT', data); | |
13 } | |
14 | |
15 document.addEventListener('DOMContentLoaded', function () { | 3 document.addEventListener('DOMContentLoaded', function () { |
16 const connection = new Strophe.Connection(BOSH_SERVICE); | 4 let connection = null; |
17 connection.rawInput = rawInput; | |
18 connection.rawOutput = rawOutput; | |
19 | 5 |
20 const jid_element = document.getElementById('jid'); | 6 const jid_element = document.getElementById('jid'); |
21 const pass_element = document.getElementById('pass'); | 7 const pass_element = document.getElementById('pass'); |
22 const connect_button = document.getElementById('connect'); | 8 const connect_button = document.getElementById('connect'); |
23 | 9 |
24 const connected_div = document.getElementById('connected'); | 10 const connected_div = document.getElementById('connected'); |
25 | 11 |
26 const avatar_img = document.getElementById('avatar'); | 12 const avatar_img = document.getElementById('avatar'); |
27 | 13 |
14 function rawInput(data) | |
15 { | |
16 console.log('RECV', data); | |
17 } | |
18 | |
19 function rawOutput(data) | |
20 { | |
21 console.log('SENT', data); | |
22 } | |
23 | |
28 connect_button.addEventListener('click', function (evt) { | 24 connect_button.addEventListener('click', function (evt) { |
29 if (connect_button.value == 'connect') { | 25 if (connect_button.value == 'connect') { |
30 connection.connect(jid_element.value, | 26 const jid = jid_element.value; |
31 pass_element.value, | 27 getBOSHService(jid).then((bosh_service) => { |
32 onConnect); | 28 connection = new Strophe.Connection(bosh_service); |
33 } else { | 29 connection.rawInput = rawInput; |
30 connection.rawOutput = rawOutput; | |
31 connection.connect(jid, | |
32 pass_element.value, | |
33 onConnect); | |
34 }); | |
35 } else if (connection != null) { | |
34 connection.disconnect(); | 36 connection.disconnect(); |
35 } | 37 } |
36 evt.preventDefault(); | 38 evt.preventDefault(); |
37 }); | 39 }); |
40 | |
41 function getBOSHService(jid) | |
42 { | |
43 return new Promise((resolve, reject) => { | |
44 const [nodepart, domainpart] = jid.split('@', 2); | |
45 //const url = 'https://' + domainpart + '/.well-known/host-meta'; | |
46 const url = '/.well-known/host-meta'; | |
47 const xhr = new XMLHttpRequest(); | |
48 xhr.onload = function (evt) { | |
49 const xml = evt.target.responseXML; | |
50 const links = parseXPath(xml, './xrd:XRD/xrd:Link', XPathResult.ORDERED_NODE_ITERATOR_TYPE); | |
51 let bosh_service = null; | |
52 while (true) { | |
53 const link = links.iterateNext(); | |
54 if (!link) | |
55 break; | |
56 if (link.getAttributeNS(null, 'rel') == 'urn:xmpp:alt-connections:xbosh') { | |
57 bosh_service = link.getAttributeNS(null, 'href'); | |
58 break; | |
59 } | |
60 // TODO: also support WebSocket. | |
61 } | |
62 console.log('bosh_service', bosh_service); | |
63 resolve(bosh_service); | |
64 }; | |
65 xhr.open('GET', url); | |
66 xhr.send(); | |
67 }); | |
68 } | |
38 | 69 |
39 function onConnect(status) | 70 function onConnect(status) |
40 { | 71 { |
41 if (status == Strophe.Status.CONNECTING) { | 72 if (status == Strophe.Status.CONNECTING) { |
42 console.log('Strophe is connecting.'); | 73 console.log('Strophe is connecting.'); |