Mercurial > eldonilo > blog
comparison blog.js @ 4:f630f4b90564
Remove the old log system and use console.log instead.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 19 Sep 2011 01:51:12 -0700 |
parents | 82905edac9d8 |
children | 7ab6b48122af |
comparison
equal
deleted
inserted
replaced
3:2e2a2154efbb | 4:f630f4b90564 |
---|---|
26 service = params.jid; | 26 service = params.jid; |
27 | 27 |
28 if (params.node) | 28 if (params.node) |
29 node = params.node; | 29 node = params.node; |
30 | 30 |
31 var logs = document.getElementById('log'); | |
32 if (params.debug) | |
33 logs.parentNode.hidden = false; | |
34 | |
35 var received = {}; | 31 var received = {}; |
36 var messages = document.getElementById('messages'); | 32 var messages = document.getElementById('messages'); |
37 | |
38 function log(msg, error) { | |
39 var p = document.createElementNS(ns.xhtml, 'p'); | |
40 p.appendChild(document.createTextNode(msg)); | |
41 | |
42 if (!error) | |
43 p.setAttributeNS(null, 'class', 'error'); | |
44 | |
45 logs.appendChild(p); | |
46 } | |
47 | |
48 function rawInput(data) { | |
49 log('RECV: ' + data, 1); | |
50 } | |
51 | |
52 function rawOutput(data) { | |
53 log('SENT: ' + data, 1); | |
54 } | |
55 | 33 |
56 var html = function(name, id) { | 34 var html = function(name, id) { |
57 return received[name][id].html; | 35 return received[name][id].html; |
58 } | 36 } |
59 | 37 |
172 } | 150 } |
173 | 151 |
174 var onMessages = function(stanza) { | 152 var onMessages = function(stanza) { |
175 conn.addHandler(onMessages, null, 'message', null, null, null); | 153 conn.addHandler(onMessages, null, 'message', null, null, null); |
176 | 154 |
177 log('message'); | |
178 stanza = xml2json(stanza); | 155 stanza = xml2json(stanza); |
179 var e = parsePubSubEvent(stanza); | 156 var e = parsePubSubEvent(stanza); |
180 | 157 |
181 if (!received[e.name]) | 158 if (!received[e.name]) |
182 received[e.name] = {}; | 159 received[e.name] = {}; |
186 updateMessage(e.name, id); | 163 updateMessage(e.name, id); |
187 } | 164 } |
188 } | 165 } |
189 | 166 |
190 var onInfo = function(stanza) { | 167 var onInfo = function(stanza) { |
191 log('info'); | 168 console.log('info'); // TODO! |
192 /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0]; | 169 /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0]; |
193 var x = query.getElementsByTagNameNS(ns.data, 'x')[0]; | 170 var x = query.getElementsByTagNameNS(ns.data, 'x')[0]; |
194 var form = forms.parse(x);*/ | 171 var form = forms.parse(x);*/ |
195 } | 172 } |
196 | 173 |
197 var onSubscribed = function(stanza) { | 174 var onSubscribed = function(stanza) { |
198 log('subscribed'); | |
199 var type = stanza.getAttribute('type'); | 175 var type = stanza.getAttribute('type'); |
200 if (type !== 'result') { | 176 if (type !== 'result') { |
201 messages.innerHTML = 'Error, impossible to retrieve messages.'; | 177 messages.innerHTML = 'Error, impossible to retrieve messages.'; |
202 conn.disconnect(); | 178 conn.disconnect(); |
203 } | 179 } |
204 } | 180 } |
205 | 181 |
206 function onConnect(status) { | 182 function onConnect(status) { |
207 if (status == Strophe.Status.CONNECTING) { | 183 if (status == Strophe.Status.CONNFAIL) { |
208 log('Strophe is connecting.'); | 184 console.log('Failed to connect.'); |
209 } else if (status == Strophe.Status.CONNFAIL) { | |
210 log('Strophe failed to connect.'); | |
211 } else if (status == Strophe.Status.AUTHENTICATING) { | |
212 log('Strophe is authenticating.'); | |
213 } else if (status == Strophe.Status.DISCONNECTING) { | 185 } else if (status == Strophe.Status.DISCONNECTING) { |
214 log('Strophe is disconnecting.'); | 186 console.log('Disconnecting.'); |
215 } else if (status == Strophe.Status.DISCONNECTED) { | 187 } else if (status == Strophe.Status.DISCONNECTED) { |
216 log('Strophe is disconnected.'); | 188 console.log('Disconnected.'); |
217 if (re) | 189 if (re) |
218 conn.connect(jid, password, onConnect); | 190 conn.connect(jid, password, onConnect); |
219 } else if (status == Strophe.Status.CONNECTED) { | 191 } else if (status == Strophe.Status.CONNECTED) { |
220 log('Strophe is connected.'); | |
221 conn.addHandler(onMessages, null, 'message', null, null, null); | 192 conn.addHandler(onMessages, null, 'message', null, null, null); |
222 conn.send($pres().tree()); | 193 conn.send($pres().tree()); |
223 conn.pubsub.subscribe(jid, service, node, undefined, onMessages, onSubscribed); | 194 conn.pubsub.subscribe(jid, service, node, undefined, onMessages, onSubscribed); |
224 if (params.no === 'server') { | 195 if (params.no === 'server') { |
225 conn.pubsub.items(jid, service, node, onMessages); | 196 conn.pubsub.items(jid, service, node, onMessages); |
226 conn.pubsub.info(jid, service, node, onInfo); | 197 conn.pubsub.info(jid, service, node, onInfo); |
227 } | 198 } |
228 } else | 199 } |
229 log('Strophe is '+status+'.'); | |
230 } | 200 } |
231 | 201 |
232 window.addEventListener('load', function () { | 202 window.addEventListener('load', function () { |
233 conn = new Strophe.Connection(BOSH_SERVICE); | 203 conn = new Strophe.Connection(BOSH_SERVICE); |
234 conn.rawInput = rawInput; | |
235 conn.rawOutput = rawOutput; | |
236 conn.connect(jid, password, onConnect); | 204 conn.connect(jid, password, onConnect); |
237 }, false); | 205 }, false); |
238 | 206 |
239 window.addEventListener('unload', function (e) { | 207 window.addEventListener('unload', function (e) { |
240 re = false; | 208 re = false; |