Mercurial > xmpp-account-manager
annotate client.js @ 3:5aa1bf7154b0
Add a simple PEP node viewer and editor.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 22 Dec 2018 02:23:38 +0100 |
parents | d6df73b466f6 |
children | 5e97e1808a35 |
rev | line source |
---|---|
0 | 1 'use strict'; |
2 | |
3 document.addEventListener('DOMContentLoaded', function () { | |
1
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
4 let connection = null; |
0 | 5 |
6 const jid_element = document.getElementById('jid'); | |
7 const pass_element = document.getElementById('pass'); | |
8 const connect_button = document.getElementById('connect'); | |
9 | |
10 const connected_div = document.getElementById('connected'); | |
11 | |
12 const avatar_img = document.getElementById('avatar'); | |
13 | |
1
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
14 function rawInput(data) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
15 { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
16 console.log('RECV', data); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
17 } |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
18 |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
19 function rawOutput(data) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
20 { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
21 console.log('SENT', data); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
22 } |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
23 |
0 | 24 connect_button.addEventListener('click', function (evt) { |
25 if (connect_button.value == 'connect') { | |
1
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
26 const jid = jid_element.value; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
27 getBOSHService(jid).then((bosh_service) => { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
28 connection = new Strophe.Connection(bosh_service); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
29 connection.rawInput = rawInput; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
30 connection.rawOutput = rawOutput; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
31 connection.connect(jid, |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
32 pass_element.value, |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
33 onConnect); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
34 }); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
35 } else if (connection != null) { |
0 | 36 connection.disconnect(); |
37 } | |
38 evt.preventDefault(); | |
39 }); | |
40 | |
1
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
41 function getBOSHService(jid) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
42 { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
43 return new Promise((resolve, reject) => { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
44 const [nodepart, domainpart] = jid.split('@', 2); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
45 //const url = 'https://' + domainpart + '/.well-known/host-meta'; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
46 const url = '/.well-known/host-meta'; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
47 const xhr = new XMLHttpRequest(); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
48 xhr.onload = function (evt) { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
49 const xml = evt.target.responseXML; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
50 const links = parseXPath(xml, './xrd:XRD/xrd:Link', XPathResult.ORDERED_NODE_ITERATOR_TYPE); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
51 let bosh_service = null; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
52 while (true) { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
53 const link = links.iterateNext(); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
54 if (!link) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
55 break; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
56 if (link.getAttributeNS(null, 'rel') == 'urn:xmpp:alt-connections:xbosh') { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
57 bosh_service = link.getAttributeNS(null, 'href'); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
58 break; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
59 } |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
60 // TODO: also support WebSocket. |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
61 } |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
62 console.log('bosh_service', bosh_service); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
63 resolve(bosh_service); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
64 }; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
65 xhr.open('GET', url); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
66 xhr.send(); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
67 }); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
68 } |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
69 |
0 | 70 function onConnect(status) |
71 { | |
72 if (status == Strophe.Status.CONNECTING) { | |
73 console.log('Strophe is connecting.'); | |
74 connect_button.value = 'disconnect'; | |
75 jid_element.disabled = true; | |
76 pass_element.disabled = true; | |
77 } else if (status == Strophe.Status.CONNFAIL) { | |
78 console.log('Strophe failed to connect.'); | |
79 connect_button.value = 'connect'; | |
80 jid_element.disabled = false; | |
81 pass_element.disabled = false; | |
82 } else if (status == Strophe.Status.DISCONNECTING) { | |
83 console.log('Strophe is disconnecting.'); | |
84 } else if (status == Strophe.Status.DISCONNECTED) { | |
85 console.log('Strophe is disconnected.'); | |
86 connect_button.value = 'connect'; | |
87 jid_element.disabled = false; | |
88 pass_element.disabled = false; | |
89 } else if (status == Strophe.Status.CONNECTED) { | |
90 console.log('Strophe is connected.'); | |
91 onConnected(); | |
92 } | |
93 } | |
94 | |
95 function onConnected() | |
96 { | |
97 connected_div.hidden = false; | |
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
98 initPEP(connection); |
0 | 99 initNickname(connection); |
100 initAvatar(connection); | |
101 } | |
102 }); |