Mercurial > eldonilo > blog
comparison blog.js @ 13:161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Drop BOSH support and add WebSockets support.
The server-side is untested, may be broken.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 03 Nov 2011 14:23:10 -0700 |
parents | 42d3f454d4b4 |
children |
comparison
equal
deleted
inserted
replaced
12:d67380657687 | 13:161d4ea1c3f8 |
---|---|
1 //'use strict'; | 1 //'use strict'; |
2 | 2 |
3 const BOSH_SERVICE = 'http://linkmauve.fr/http-bind/'; | 3 const WS_SERVICE = 'ws://plugsbee.com:5280/'; |
4 var conn = null; | 4 var conn = null; |
5 var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections. | 5 var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections. |
6 var password = 'blog'; | 6 var password = 'blog'; |
7 var service = 'psgxs.linkmauve.fr'; | 7 var service = 'psgxs.linkmauve.fr'; |
8 var node = 'blog'; | 8 var node = 'blog'; |
98 else | 98 else |
99 container.appendChild(article); | 99 container.appendChild(article); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 var convert = function(id, xml) { | 103 var convert = function(item) { |
104 var ns = xml['@xmlns']; | 104 if (item.ns in parsers) |
105 if (ns in parsers) | 105 return new parsers[item.ns](item); |
106 return new parsers[ns](id, xml); | 106 return new parsers[''](item); |
107 return new parsers[''](id, xml); | |
108 }; | 107 }; |
109 | 108 |
110 var parsePubSubEvent = function(stanza) { | 109 var onMessages = function(stanza) { |
111 var e = {}; | 110 var name = stanza.service+'/'+stanza.node; |
111 if (!received[name]) | |
112 received[name] = {}; | |
112 | 113 |
113 e.service = stanza.getAttribute('from'); | 114 var r = received[name]; |
114 | 115 |
115 var pubsub = stanza.getChild('event', ns.pse); | 116 for (var id in stanza.items) { |
116 if (!pubsub) { | 117 r[id] = convert(stanza.items[id]); |
117 pubsub = stanza.getChild('pubsub', ns.ps); | 118 updateMessage(name, id); |
118 if (!pubsub) | |
119 return; | |
120 } | |
121 e.ns = pubsub.getAttribute('xmlns'); | |
122 | |
123 var items = pubsub.getChild('items', e.ns); | |
124 if (!items) | |
125 return; | |
126 | |
127 e.node = items.getAttribute('node'); | |
128 items = items.getChildren('item', e.ns); | |
129 if (!items) | |
130 return; | |
131 | |
132 e.name = e.service + '/' + e.node; | |
133 | |
134 e.items = {}; | |
135 for (var i in items) { | |
136 var item = items[i]; | |
137 if (!item.getAttribute) | |
138 continue; | |
139 | |
140 var pl = item.getChild(); | |
141 if (!pl) | |
142 continue; | |
143 | |
144 var id = item.getAttribute('id'); | |
145 | |
146 e.items[id] = pl; | |
147 } | |
148 | |
149 return e; | |
150 } | |
151 | |
152 var onMessages = function(stanza) { | |
153 conn.addHandler(onMessages, null, 'message', null, null, null); | |
154 | |
155 stanza = xml2json(stanza); | |
156 var e = parsePubSubEvent(stanza); | |
157 | |
158 if (!received[e.name]) | |
159 received[e.name] = {}; | |
160 | |
161 for (var id in e.items) { | |
162 received[e.name][id] = convert(id, e.items[id]); | |
163 updateMessage(e.name, id); | |
164 } | 119 } |
165 } | 120 } |
166 | 121 |
167 var onInfo = function(stanza) { | 122 var onInfo = function(stanza) { |
168 console.log('info'); // TODO! | 123 console.log('info'); // TODO! |
169 /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0]; | 124 /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0]; |
170 var x = query.getElementsByTagNameNS(ns.data, 'x')[0]; | 125 var x = query.getElementsByTagNameNS(ns.data, 'x')[0]; |
171 var form = forms.parse(x);*/ | 126 var form = forms.parse(x);*/ |
172 } | 127 } |
173 | 128 |
174 var onSubscribed = function(stanza) { | 129 function onConnect() { |
175 var type = stanza.getAttribute('type'); | 130 conn.send('<presence/>'); |
176 if (type !== 'result') { | 131 // TODO: verify the subscription. |
177 messages.innerHTML = 'Error, impossible to retrieve messages.'; | 132 //conn.pubsubSubscribe(service, node); |
178 re = false; | 133 if (params.no === 'server') { |
179 conn.disconnect(); | 134 conn.pubsubItems(service, node, onMessages); |
135 conn.discoInfo(service, node, onInfo); | |
180 } | 136 } |
181 } | |
182 | 137 |
183 function onConnect(status) { | 138 /*if (status == Strophe.Status.CONNFAIL) { |
184 if (status == Strophe.Status.CONNFAIL) { | |
185 console.log('Failed to connect.'); | 139 console.log('Failed to connect.'); |
186 } else if (status == Strophe.Status.DISCONNECTING) { | 140 } else if (status == Strophe.Status.DISCONNECTING) { |
187 console.log('Disconnecting.'); | 141 console.log('Disconnecting.'); |
188 } else if (status == Strophe.Status.DISCONNECTED) { | 142 } else if (status == Strophe.Status.DISCONNECTED) { |
189 console.log('Disconnected.'); | 143 console.log('Disconnected.'); |
190 if (re) | 144 if (re) |
191 conn.connect(jid, password, onConnect); | 145 conn.connect(jid, password, onConnect); |
192 } else if (status == Strophe.Status.CONNECTED) { | 146 }*/ |
193 conn.addHandler(onMessages, null, 'message', null, null, null); | |
194 conn.send($pres().tree()); | |
195 conn.pubsub.subscribe(jid, service, node, undefined, onMessages, onSubscribed); | |
196 if (params.no === 'server') { | |
197 conn.pubsub.items(jid, service, node, onMessages); | |
198 conn.pubsub.info(jid, service, node, onInfo); | |
199 } | |
200 } | |
201 } | 147 } |
202 | 148 |
203 window.addEventListener('load', function () { | 149 window.addEventListener('load', function () { |
204 conn = new Strophe.Connection(BOSH_SERVICE); | 150 conn = new XMPP(WS_SERVICE); |
205 conn.connect(jid, password, onConnect); | 151 |
152 // Debug | |
153 conn.on('DOMOutput', function(stanza) { | |
154 console.log('out:', stanza); | |
155 }); | |
156 conn.on('DOMInput', function(stanza) { | |
157 console.log('in:', stanza); | |
158 }); | |
159 | |
160 conn.on('connected', onConnect); | |
161 conn.on('message', onMessages); | |
162 conn.connect(jid, password); | |
206 }, false); | 163 }, false); |
207 | 164 |
208 window.addEventListener('unload', function (e) { | 165 window.addEventListener('unload', function (e) { |
209 re = false; | 166 re = false; |
210 conn.disconnect(); | 167 conn.disconnect(); |