2013-10-29 16:51:16 +01:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h2>DMVCFramework - Callback DEMO</h2>
|
|
|
|
<div class="beforelogin">
|
|
|
|
<input type="text" id="username" style="width: 300px" value="your username">
|
|
|
|
<input type="button" id="btnLogin" style="width: 100px" value="login">
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="afterlogin">
|
|
|
|
<div id="area" style="font-family: verdana; width: 400px; height: 500px; border: grey thin solid; overflow-y: scroll;">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<input type="text" id="txtMessage" style="width: 300px" value="Write your message...">
|
|
|
|
<input type="button" id="btnSend" style="width: 100px" value="send">
|
|
|
|
<br>
|
|
|
|
<input type="button" id="btnSendMultiple" style="width: 300px" value="send multiple">
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
|
|
|
<script>
|
|
|
|
function enterInChat(username) {
|
|
|
|
var res = false;
|
|
|
|
$.ajax({
|
2015-04-01 17:01:23 +02:00
|
|
|
url: "/messages/clients/" + username,
|
|
|
|
type: "POST",
|
2013-10-29 16:51:16 +01:00
|
|
|
async: false,
|
|
|
|
success: function(data) {
|
|
|
|
console.log(data);
|
|
|
|
res = true;
|
|
|
|
},
|
|
|
|
error: function(data) {
|
|
|
|
console.log(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//if (!res) return false;
|
|
|
|
|
|
|
|
$.ajax({
|
2015-04-01 17:01:23 +02:00
|
|
|
url: "/messages/subscriptions/topic/chat",
|
|
|
|
type: "POST",
|
2013-10-29 16:51:16 +01:00
|
|
|
async: false,
|
|
|
|
success: function(data) {
|
|
|
|
res = true;
|
|
|
|
console.log(data);
|
|
|
|
receive();
|
|
|
|
},
|
|
|
|
error: function(data) {
|
|
|
|
console.log(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function sendMessage(msg){
|
|
|
|
var msgobj = {
|
|
|
|
text: msg
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax({
|
2015-04-01 17:01:23 +02:00
|
|
|
url: "/messages/topic/chat",
|
2013-10-29 16:51:16 +01:00
|
|
|
data: JSON.stringify(msgobj),
|
|
|
|
processData: false,
|
|
|
|
type: "POST",
|
2015-04-01 17:01:23 +02:00
|
|
|
async: true,
|
2013-10-29 16:51:16 +01:00
|
|
|
success: function(data) {
|
|
|
|
console.log(data)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bindEvents(){
|
2015-04-01 17:01:23 +02:00
|
|
|
$('#username').val("user" + (1000 + Math.round((Math.random() * 1000)).toString()));
|
|
|
|
|
2013-10-29 16:51:16 +01:00
|
|
|
$('#btnSend').click(function(){
|
|
|
|
sendMessage($('#txtMessage').val())
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#btnSendMultiple').click(function(){
|
|
|
|
for (var i = 1; i <= 20; i++) {
|
|
|
|
sendMessage('message' + i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#btnLogin').click(function(){
|
|
|
|
var username = $('#username').val();
|
|
|
|
username = username.replace(" ","").replace(",","").replace("-",""); //only chars without spaces
|
|
|
|
if (username == 'yourusername' || username == '') {
|
|
|
|
alert('Please, enter your username');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (enterInChat(username)) {
|
|
|
|
$('.beforelogin').hide();
|
|
|
|
$('.afterlogin').show();
|
|
|
|
$('h2').append(' <span>(logged as ' + username + ')</span>');
|
|
|
|
} else {
|
|
|
|
alert('Problem on connecting. Retry please...');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function htmlEntities(str) {
|
|
|
|
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
function receive() {
|
|
|
|
$.ajax({
|
2015-04-01 17:01:23 +02:00
|
|
|
url: "/messages",
|
2013-10-29 16:51:16 +01:00
|
|
|
type: "GET",
|
|
|
|
async: true,
|
|
|
|
success: function(data) {
|
|
|
|
errcount = 0;
|
|
|
|
console.log(JSON.stringify(data));
|
|
|
|
var s, msg;
|
|
|
|
var area = $('#area');
|
|
|
|
for (var i = 0; i < data.messages.length; i++) {
|
|
|
|
msg = data.messages[i];
|
|
|
|
s = '<p><span>' +
|
|
|
|
'<i>(' + data.messages[i]._timestamp + ') - </i>' +
|
|
|
|
'<i>' + data.messages[i]._username + '</i>' +
|
|
|
|
'</span><br><span><b>' + htmlEntities(data.messages[i].message.text) + '</span></p>';
|
|
|
|
$(s).appendTo(area);
|
|
|
|
document.getElementById("area").scrollTop = document.getElementById("area").scrollHeight;
|
|
|
|
}
|
|
|
|
setTimeout(function(){receive()}, 10);
|
|
|
|
},
|
|
|
|
error: function(data) {
|
|
|
|
errcount++;
|
|
|
|
console.log("ERROR");
|
|
|
|
console.log(arguments);
|
|
|
|
if (errcount < 5)
|
|
|
|
setTimeout(function(){receive()}, 10);
|
|
|
|
else
|
|
|
|
alert('Errors... cannot connect...');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$(document).ready(function(){
|
|
|
|
$('.beforelogin').show();
|
|
|
|
$('.afterlogin').hide();
|
|
|
|
bindEvents();
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|