Permalink
Browse files

Merge pull request #3 from ajacksified/master

Demo Fixes
  • Loading branch information...
2 parents f47a0cd + 963808b commit b897da92b2c89ea3112e95ce762ef45cce936508 Superfeedr.com committed Jun 26, 2011
Showing with 179 additions and 121 deletions.
  1. +1 −1 demo/demo.js
  2. +48 −27 demo/index.html
  3. +10 −12 demo/strophe.js
  4. +120 −81 demo/strophedemo.js
View
@@ -44,4 +44,4 @@ $(function() {
WsDemo.send($('#phrase').val());
$('#phrase').val('');
});
-});
+});
View
@@ -1,31 +1,52 @@
<!DOCTYPE html>
<html>
-<head>
- <title>Websockets With Ejabberd And Strophe</title>
- <!-- http://dev.w3.org/html5/websockets/#the-websocket-interface -->
-</head>
-<body>
-<h1>Ejabberd strophe demo</h1>
- <div id="connect">
- <button id="buttonConnect">Connect</button>
- State: <span id="connstate" style="font-weight:bold;"></span>
- </div>
- <div id='disconnect'>
- <button id="buttonClose">Disconnect</button>
- </div>
- <hr/>
- <div id="connected">
- <input id='phrase' type='text'/>
- <input id='buttonSend' class='button' type='submit' name='connect' value='Send'/>
- </div>
- <hr/>
- <div id="msgs"></div>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
- <script type="text/javascript">
- // If googleapis failed, load locally
- !window.jQuery && document.write(unescape('%3Cscript src="jquery.js"%3E%3C/script%3E'))
- </script>
- <script src='strophe.js'></script>
- <script src='strophedemo.js'></script>
+ <head>
+ <title>Websockets With Ejabberd And Strophe</title>
+ <!-- http://dev.w3.org/html5/websockets/#the-websocket-interface -->
+ </head>
+ <body>
+ <h1>Ejabberd strophe demo</h1>
+ <form id="connect">
+ <label for="server">Server:</label> <input id="server" />
+ <br />
+
+ <label for="un">Username:</label> <input id="un" /> @ <input id="un_server" />
+ <br />
+
+ <label for="un">Password:</label> <input id="pw" type="password" />
+ <br />
+
+ <button id="buttonConnect" type="submit">Connect</button>
+ State: <span id="connstate" style="font-weight:bold;"></span>
+ </form>
+
+ <div id='disconnect'>
+ <button id="buttonClose">Disconnect</button>
+ </div>
+
+ <hr/>
+
+ <form id="sendMessage">
+ <label for="to">To:</label> <input id="to" /> @ <input id="to_server" />
+ <br />
+
+ <label for="phrase">Message:</label> <input id='phrase' type='text'/>
+ <br />
+
+ <button id='buttonSend' class='button' type='submit' name='connect'>Send</button>
+ </form>
+
+ <hr/>
+
+ <div id="msgs"></div>
+
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
+ <script type="text/javascript">
+ // If googleapis failed, load locally
+ !window.jQuery && document.write(unescape('%3Cscript src="jquery.js"%3E%3C/script%3E'))
+ </script>
+ <script src='strophe.js'></script>
+ <script src='strophedemo.js'></script>
</body>
</html>
+
View
@@ -1633,8 +1633,8 @@ Strophe.TimedHandler.prototype = {
*/
Strophe.Connection = function (service)
{
- if(service.protocol) {
- this.protocol = service.protocol;
+ if(service.proto) {
+ this.protocol = service.proto;
}
else {
console.log("Warning : this syntax will be deprecated to leave room for othe protocols. Please use new Strophe.Connection({proto : new Strophe.Bosh(BOSH_SERVICE)})" )
@@ -3479,12 +3479,12 @@ Strophe.Websocket.prototype = {
*/
connect: function(connection) {
if(!this.socket) {
- this.connection = connection;
- this.socket = new WebSocket(this.service, "xmpp");
- this.socket.onopen = this._onOpen.bind(this);
- this.socket.onerror = this._onError.bind(this);
- this.socket.onclose = this._onClose.bind(this);
- this.socket.onmessage = this._onMessage.bind(this);
+ this.connection = connection;
+ this.socket = new WebSocket(this.service, "xmpp");
+ this.socket.onopen = this._onOpen.bind(this);
+ this.socket.onerror = this._onError.bind(this);
+ this.socket.onclose = this._onClose.bind(this);
+ this.socket.onmessage = this._onMessage.bind(this);
}
},
@@ -3493,7 +3493,7 @@ Strophe.Websocket.prototype = {
*/
disconnect: function() {
this.connection.xmlOutput(this._endStream());
- this.connection.rawOutput(this._endStream());
+ this.connection.rawOutput(this._endStream());
this.socket.send(this._endStream())
this.socket.close(); // Close the socket
},
@@ -3539,7 +3539,7 @@ Strophe.Websocket.prototype = {
*/
_onOpen: function() {
this.connection.xmlOutput(this._startStream());
- this.connection.rawOutput(this._startStream());
+ this.connection.rawOutput(this._startStream());
this.socket.send(this._startStream());
},
@@ -3587,6 +3587,4 @@ Strophe.Websocket.prototype = {
_endStream:function() {
return "</stream:stream>";
}
-
}
-
View
@@ -1,85 +1,124 @@
var StropheDemo = function() {
- var connection = null;
- var obj = {
- go: function() {
- Strophe.log = function(level, log) {
- console.log("Logger:"+log);
- };
- console.log("connecting...");
- connection = new Strophe.Connection({proto : new Strophe.Websocket("ws://localhost:5288/ws-xmpp")});
- connection.rawInput = function(log) {
- console.log("RECV:"+log);
- };
- connection.rawOutput = function(log) {
- console.log("SENT:"+log);
- };
- connection.connect("localhost",
- null,
- function(cond, status) {
- console.log("Connection handler:");
- if (cond === 0) {
- console.log("Error");
- $('#connstate').html('ERROR');
- } else if (cond === 1) {
- console.log("Connecting");
- $('#connstate').html('Connecting');
- } else if (cond === 2) {
- console.log("Connection Failed");
- $('#connstate').html('Connection Failed');
- } else if (cond === 3) {
- console.log("Authenticating");
- $('#connstate').html('Authenticating');
- } else if (cond === 4) {
- console.log("Auth Failed");
- $('#connstate').html('Auth Failed');
- } else if (cond === 5) {
- console.log("Connected");
- $('#connstate').html('Connected');
- } else if (cond === 6) {
- console.log("disconnected");
- $('#connstate').html('Disconnected');
- } else if (cond === 7) {
- console.log("disconnecting");
- $('#connstate').html('Disconnecting');
- } else if (cond === 8) {
- console.log("Attached");
- $('#connstate').html('Attached');
- }
- if (status) {
- console.log(status);
- }
- return true;
- });
- connection.addHandler(function(stanza) {
- console.log("Stanza handler.");
- console.log(stanza);
- return true;
- }, null, 'message', null, null, null);
- },
- close: function() {
- connection.disconnect();
- },
- send: function(message) {
- var msg = $msg({to: "thepug@localhost",
- from: connection.jid,
- type: "chat"})
- .c("body",
- {xmlns: Strophe.NS.CLIENT}).t(message);
- msg.up().c("x", {xmlns: "jabber:x:event"}).c("composing");
- connection.send(msg);
+ var connection = null;
+ var obj = {
+ go: function(un, pw) {
+ Strophe.log = function(level, log) {
+ console.log("Logger:"+log);
+ };
+
+ console.log("connecting...");
+
+ var server = $("#server").val();
+ connection = new Strophe.Connection({proto : new Strophe.Websocket("ws://" + server + ":5288/ws-xmpp")});
+
+ connection.rawInput = function(log) {
+ var xml = $($.parseXML(log));
+
+ if(xml.find("body").length){
+ var from = xml.find("message").attr("from").split("/")[0];
+ var msg = xml.find("body").text();
+ $("#msgs").prepend("<p><strong>" + from + "</strong>: " + msg + "</p>");
}
- };
- return obj;
+ };
+
+ connection.rawOutput = function(log) {
+ console.log("SENT:"+log);
+ };
+
+ connection.connect(un, pw, function(cond, status) {
+ console.log("Connection handler:");
+ if (cond === 0) {
+ console.log("Error");
+ $('#connstate').html('ERROR');
+ } else if (cond === 1) {
+ console.log("Connecting");
+ $('#connstate').html('Connecting');
+ } else if (cond === 2) {
+ console.log("Connection Failed");
+ $('#connstate').html('Connection Failed');
+ } else if (cond === 3) {
+ console.log("Authenticating");
+ $('#connstate').html('Authenticating');
+ } else if (cond === 4) {
+ console.log("Auth Failed");
+ $('#connstate').html('Auth Failed');
+ } else if (cond === 5) {
+ console.log("Connected");
+ $('#connstate').html('Connected');
+ console.log("sending presence tree...");
+ connection.send($pres().tree());
+ } else if (cond === 6) {
+ console.log("disconnected");
+ $('#connstate').html('Disconnected');
+ } else if (cond === 7) {
+ console.log("disconnecting");
+ $('#connstate').html('Disconnecting');
+ } else if (cond === 8) {
+ console.log("Attached");
+ $('#connstate').html('Attached');
+ }
+
+ if (status) {
+ console.log(status);
+ }
+
+ return true;
+ });
+
+ connection.addHandler(function(stanza) {
+ console.log("Stanza handler.");
+ console.log(stanza);
+ return true;
+ }, null, 'message', null, null, null);
+ },
+
+ close: function() {
+ connection.disconnect();
+ },
+
+ setPresence: function(){
+ connection.send($pres().tree());
+ },
+
+ send: function(message, to) {
+ var msg = $msg({to: to, from: connection.jid, type: "chat"}).c("body", {xmlns: Strophe.NS.CLIENT}).t(message);
+ msg.up().c("x", {xmlns: "jabber:x:event"}).c("composing");
+ connection.send(msg);
+ }
+ };
+
+ return obj;
}();
+
$(function() {
- $('#buttonConnect').click(function(event) {
- StropheDemo.go();
- });
- $('#buttonClose').click(function(event) {
- StropheDemo.close();
- });
- $('#buttonSend').click(function(event) {
- StropheDemo.send($('#phrase').val());
- $('#phrase').val('');
- });
-});
+ $('#connect').submit(function(e) {
+ e.preventDefault();
+ var un = $('#un').val() + "@" + $('#un_server').val();
+ var pw = $('#pw').val();
+ StropheDemo.go(un, pw);
+ });
+
+ $('#buttonClose').click(function(e) {
+ StropheDemo.close();
+ });
+
+ $('#sendMessage').submit(function(e) {
+ e.preventDefault();
+
+ var msg = $("#phrase").val();
+ var to = $("#to").val() + "@" + $('#to_server').val();
+ var from = $('#un').val() + "@" + $('#un_server').val();
+ StropheDemo.send(msg, to);
+ $('#phrase').val('');
+ $("#msgs").prepend("<p><strong>" + from + "</strong>: " + msg + "</p>");
+ });
+
+ $("#server").bind("change", function(){
+ var server = $(this).val();
+ var unServer = $("#un_server");
+ var toServer = $("#to_server");
+ if((unServer.val() == server && toServer.val() == server) || (!unServer.val() && !toServer.val())){
+ $('#un_server, #to_server').val($(this).val());
+ }
+ });
+});

0 comments on commit b897da9

Please sign in to comment.