HTML and CSS Reference
In-Depth Information
Listing 4-4. Initial 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;
output.appendChild(line);
}
function connectHandler(cond) {
if (cond == Strophe.Status.CONNECTED) {
log("connected");
connection.send($pres());
}
}
var url = "ws://localhost:5280/";
var connection = null;
var connectButton = document.getElementById("connectButton");
connectButton.onclick = function() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
connection = new Strophe.Connection(
{proto: new Strophe.Websocket(url)});
connection.connect(username, password, connectHandler);
}
Be aware that this example requires the user to enter his or her credentials. In
production, it is very important to make sure that credentials are not sent across the
network unencrypted. Actually, it is far better to not send credentials across the network
at all. See Chapter 7 for information about using encryption and authentication for
WebSocket. If your chat application is part of a larger suite of web applications, you'll
likely want to use a single sign-on mechanism, especially if you are building a chat widget
for a larger site or if your users authenticate with external credentials.
If everything goes according to plan, you should see “connected” logged onto
the page. If so, you have successfully logged a user into a chat server using XMPP over
WebSocket. You should see the connected user has come online in the roster UI of the
other XMPP client that you left connected earlier (see Figure 4-7 ).
 
Search WWH ::




Custom Search