Connecting to the XMPP server

Wed, 10 Mar 2010 18:12:33 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 10 Mar 2010 18:12:33 +0000
changeset 12
7b0df9aad29c
parent 11
bf6a0898e613
child 13
7ae5a537d7f1

Connecting to the XMPP server

js/supportchat.js file | annotate | diff | comparison | revisions
--- a/js/supportchat.js	Wed Mar 10 12:03:47 2010 +0000
+++ b/js/supportchat.js	Wed Mar 10 18:12:33 2010 +0000
@@ -1,3 +1,52 @@
+/* Config */
+var LOGIN_DOMAIN = "anon.localhost";
+var BOSH_URL = "/http-bind";
+
+/*** Helper functions */
+
+/*** XMPP handling */
+var conn = null;
+
+/* Called by Strophe when status of connection changes
+   (from disconnected to connected, vice-versa, etc.)
+*/
+function handle_connection_status(status, err)
+{
+	if(status != Strophe.Status.CONNECTING && status != Strophe.Status.AUTHENTICATING)
+		set_busy_status(false);
+	else if(status != Strophe.Status.CONNECTED)
+		set_busy_status(true);
+	
+	if(err)
+		alert(err); //FIXME: Handle gracefully
+	
+	if(status == Strophe.Status.CONNECTED)
+	{
+		//FIXME: Join MUC
+		alert("Connected!");
+	}
+}
+
+/* Initiate the connection to the XMPP server */
+function start_connection()
+{
+	conn = new Strophe.Connection(BOSH_URL);
+	var ret = true;
+	try
+	{
+		conn.connect(LOGIN_DOMAIN, null, handle_connection_status, 50);
+	}
+	catch(e)
+	{
+		ret = false;
+	}
+	return ret;
+}
+
+
+/*** UI handling */
+
+/* Handle the user submitting the question form */
 function on_question_submit()
 {
 	var question_type = $("#support-question-type").val();
@@ -7,11 +56,22 @@
 	alert(question_name + " submitted a " + question_type + " question:\n " + question_text);
 }
 
+/* Update the UI according to whether we are
+   waiting on a network operation
+*/
+function set_busy_status(status)
+{
+	//FIXME
+}
+
 function display_ui()
 {
 	// Display pop-up, showing question form
-	$("#support-chat").show();
-	$("#support-question-submit").click(on_question_submit);
+	if(start_connection())
+	{
+		$("#support-chat").show();
+		$("#support-question-submit").click(on_question_submit);
+	}
 }
 
 $(display_ui); //FIXME (debugging)

mercurial