HTML and CSS Reference
In-Depth Information
Listing 4-16. Setting the Client Response
<iq type="result" id="86-14" to="localhost"
from "websocketuser@localhost/cc9fd219" />
To handle pings in Strophe.js, we need to register a function to handle all iq stanzas
with the urn:xmpp:ping namespace and type="get" (see Listing 4-17). As in the previous
steps, we do this by registering a handler on the connection object. The handler code
builds the appropriate response and sends it back to the server.
Listing 4-17. Registering a Handler for iq Stanzas
function pingHandler(ping) {
var pingId = ping.getAttribute("id");
var from = ping.getAttribute("from");
var to = ping.getAttribute("to");
var pong = $iq({type: "result", "to": from, id: pingId, "from": to});
// Indicate that this handler should be called repeatedly
return true;
Listing 4-18 shows how the handler is registered.
Listing 4-18. Registered addHandler
connection.addHandler(pingHandler, "urn:xmpp:ping", "iq", "get");
Completed Chat Application
Listing 4-19 shows the finished, end-to-end chat application, complete with pings and pongs.
Listing 4-19. Final Version of chat_app.js
// Log messages to the output area
var output = document.getElementById("output");
function log(message) {
var line = document.createElement("div");
line.textContent = message;
function connectHandler(cond) {
if (cond == Strophe.Status.CONNECTED) {
Search WWH ::

Custom Search