Skip to content

Commit

Permalink
More updates on chat example
Browse files Browse the repository at this point in the history
  • Loading branch information
artyom-beilis committed Dec 8, 2011
1 parent 945e3ac commit f591a95
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 72 deletions.
8 changes: 8 additions & 0 deletions examples/json_rpc_chat/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class chat : public cppcms::rpc::json_rpc_server {
}
timer_.expires_from_now(booster::ptime::seconds(1));
timer_.async_wait(boost::bind(&chat::on_timer,booster::intrusive_ptr<chat>(this),_1));
std::cout << "Status: \n"
<< "Waiters: " << waiters_.size() << '\n'
<< "Messages:" << messages_.size() <<'\n'
<< "[";
for(size_t i=0;i<messages_.size();i++)
std::cout << messages_[i] << std::endl;
std::cout <<"]" << std::endl;
}

void get(unsigned from)
Expand Down Expand Up @@ -109,6 +116,7 @@ class chat : public cppcms::rpc::json_rpc_server {
for(size_t i=n;i<messages_.size();i++) {
ar.push_back(messages_[i]);
}
std::cout << "Response to client:" << v << std::endl;
return v;
}
private:
Expand Down
6 changes: 6 additions & 0 deletions examples/json_rpc_chat/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
},
"file_server" : {
"enable" : true,
"alias" : [
{
"url" : "/scripts" ,
"path" : "../../contrib/client_side/jsonrpc"
}
],
"document_root" : "."
},

Expand Down
134 changes: 62 additions & 72 deletions examples/json_rpc_chat/the_chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,75 @@
<script type="text/javascript" src="/scripts/json2.js"></script>
<script type="text/javascript" src="/scripts/jsonrpc.js"></script>
<title>Chat Room</title>
</head>
<body>
<h1>Chat room</h1>
<script type="text/javascript">
var message_count = 0;
var rpc = new JsonRPC('/rpc',['get'],['post']);
function on_new_data_read(messages)
{
}
function on_error(e)
{
document.GetElementById('error_message').innerHTML = e;
document.GetElementById('reconnect').disabled = false;
}
function post_failed(e)
{
document.GetElementById('error_message').innerHTML = e;
}

rpc.get.on_result = function(messages) {
var messagesHtml = document.GetElementById('messages');
for(message in messages) {
messagesHtml.innerHTML+='<dt>' + message.author +'</dt>' +
'<dd>' + message.message + '</dd>'
message_count++;
}
restart();
}
<script type="text/javascript">
message_count = 0;
rpc = new JsonRPC('/chat',['get'],['post']);

rpc.get.on_error = function(e) {
document.GetElementById('error_message').innerHTML = e;
document.GetElementById('reconnect').disabled = false;
}

rpc.post.on_error = function(e) {
document.GetElementById('error_message').innerHTML = e;
}
rpc.post.on_result = function() {
document.GetElementById
}

function restart()
{
rpc.get(message_count);
}
function make_error(what,e)
{
document.getElementById('error_message').innerHTML = what + ': ' + e.type +': ' + e.error;
}

function send_data() {
var kw = {
url : "/chat/post",
form : "theform"
};
dojo.xhrPost(kw);
dojo.byId("message").value="";
return false;
}
function read_data() {
dojo.xhrGet( {
url: "/chat/get/" + message_count,
timeout: 10000,
handleAs: "text",
load: function(response, ioArgs) {
dojo.byId("messages").innerHTML = response + '<br/>' + dojo.byId("messages").innerHTML;
rpc.get.on_result = function(messages) {
var messagesHtml = document.getElementById('messages');

for(var i=0;i<messages.length;i++) {
m=messages[i];
messagesHtml.innerHTML+='<dt>' + m.author +'</dt>' +
'<dd>' + m.message + '</dd>';
message_count++;
read_data();
return response;
},
error: function(response,ioArgs) {
read_data();
return response;
}
//messagesHtml.innerHTML += '<p>JSON: ' + JSON.stringify(messages) + '</p>' ;

});
}
dojo.addOnLoad(read_data);
</script>
restart();
}

rpc.get.on_error = function(e) {
make_error('Getting New Messages',e);
document.getElementById('reconnect').disabled = false;
}

rpc.post.on_error = function(e) {
make_error('Posting New Messages',e);
}
rpc.post.on_result = function() {
document.getElementById("message").value = '';
}

function restart()
{
rpc.get(message_count);
}
function reconnect_to_server()
{
message_count = 0;
document.getElementById('error_message').innerHTML = '';
restart();
return false;
}

function send_data() {
author = document.getElementById('author').value;
message = document.getElementById("message").value;
rpc.post(author,message);
return false;
}
</script>


</head>
<body onload='restart()'>
<h1>Chat room</h1>
<form id="theform" >
<input id="message" type="text" name="message" value="" />
<p>Name: <input id="author" type="text" value="" /></p>
<p>
Message: <input id="message" type="text" value="" /></p>
<input type="submit" value="Send" onclick="return send_data()"/>
<input disabled="disabled" id='reconnect' type='submit' value='Reconnect' onclick='return reconnect_to_server()'>
</p>
<p id='error_message'></p>
</form>
<div id="messages">
</div>
<dl id="messages">
</dl>
</body>

0 comments on commit f591a95

Please sign in to comment.