/
basic-chat.html
92 lines (83 loc) · 3.04 KB
/
basic-chat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<html>
<head>
<title>Basic connection</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/freakySevi/jQuery-XMPP-plugin/master/jquery.xmpp.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#connectBut").click(function(){
var jid = $("#jid").val();
var password = $("#pass").val();
var logContainer = $("#log");
var contactList = $("#contacts");
//An example of bosh server. This site is working but it can change or go down.
//If you are going to have a production site, you must install your own BOSH server
var url ="http://bosh.metajack.im:5280/xmpp-httpbind";
$.xmpp.connect({url:url, jid: jid, password: password,
onConnect: function(){
logContainer.html("Connected");
$.xmpp.setPresence(null);
},
onPresence: function(presence){
var contact = $("<li>");
contact.append("<a href='javascript:void(0)'>"+ presence.from +"</a>");
contact.find("a").click(function(){
var id = MD5.hexdigest(presence.from);
var conversation = $("#"+id);
if(conversation.length == 0)
openChat({to:presence.from});
});
contactList.append(contact);
},
onDisconnect: function(){
logContainer.html("Disconnected");
},
onMessage: function(message){
var jid = message.from.split("/");
var id = MD5.hexdigest(message.from);
var conversation = $("#"+id);
if(conversation.length == 0){
openChat({to:message.from});
}
conversation = $("#"+id);
conversation.find(".conversation").append("<div>"+ jid[0] +": "+ message.body +"</div>");
},onError:function(error){
alert(error.error);
}
});
});
$("#disconnectBut").click(function(){
$.xmpp.disconnect();
});
});
function openChat(options){
var id = MD5.hexdigest(options.to);
var chat = $("<div style='border: 1px solid #000000; float:left' id='"+id+"'><div style='border: 1px solid #000000;'>Chat with "+options.to+"</div><div style='height:150px;overflow: auto;' class='conversation'></div><div><input type='text' /><button>Send</button></div></div>");
var input = chat.find("input");
var sendBut = chat.find("button");
var conversation = chat.find(".conversation");
sendBut.click(function(){
$.xmpp.sendMessage({to:options.to, body: input.val()});
conversation.append("<div>"+ $.xmpp.jid +": "+ input.val() +"</div>");
input.val("");
});
$("body").append(chat);
}
</script>
</head>
<body>
This example just connect notify when connected and show the connected contacts.
<br>
Jid <input type="text" id="jid"> (ej: maxpowel@gmail.com, alvaro.maxpowel@chat.facebook.com)
<br>
Password <input type="password" id="pass">
<br>
<button id="connectBut">Connect</button>
<button id="disconnectBut">Disconnect</button>
<br>
<div id="log">
</div>
<ul id="contacts">
</ul>
</body>
</html>