Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. void main(std::string /*url*/)
  2. {
  3. stream_->accept(release_context());
  4. }
  5.  
  6. #include <fstream>
  7. #include <iostream>
  8. #include "server_sent_events.h"
  9. #include <cppcms/application.h>
  10. #include <cppcms/applications_pool.h>
  11. #include <cppcms/service.h>
  12. #include <cppcms/http_context.h>
  13. #include <cppcms/http_response.h>
  14. #include <cppcms/url_dispatcher.h>
  15. #include <cppcms/url_mapper.h>
  16. #include <booster/aio/deadline_timer.h>
  17. #include <booster/system_error.h>
  18.  
  19. using namespace std;
  20.  
  21. class MyApp: public cppcms::application
  22. {
  23. private:
  24. booster::shared_ptr<sse::state_stream> stream_;
  25. booster::aio::deadline_timer tm_;
  26. double price_;
  27.  
  28. public:
  29. MyApp(cppcms::service &srv);
  30. void showStatus();
  31. void wait();
  32. void on_timer();
  33. };
  34.  
  35.  
  36. MyApp::MyApp(cppcms::service &service) : cppcms::application(service), tm_(service.get_io_service()), price_(1.0)
  37. {
  38. dispatcher().assign("/showstatus",&MyApp::showStatus,this);
  39. mapper().assign("Show Status","/showstatus");
  40.  
  41. mapper().root("/robot");
  42.  
  43. stream_ = sse::state_stream::create(service.get_io_service());
  44. wait();
  45. }
  46.  
  47.  
  48. /*
  49. void MyApp::main(std::string)
  50. {
  51. cout << "MAIN CALLED" << endl;
  52. stream_->accept(release_context());
  53. }
  54. */
  55.  
  56.  
  57. void MyApp::showStatus()
  58. {
  59. ifstream fileStream("showstatus.html");
  60. stringstream buffer;
  61. buffer << fileStream.rdbuf();
  62. string page = buffer.str();
  63. response().out() << page.c_str();
  64. stream_->accept(release_context());
  65. }
  66.  
  67.  
  68. void MyApp::wait()
  69. {
  70. tm_.expires_from_now(booster::ptime::from_number(double(rand())/RAND_MAX + 0.01));
  71. tm_.async_wait([=](booster::system::error_code const &e){
  72. if(!e) {
  73. on_timer();
  74. wait();
  75. }
  76. >> });
  77. }
  78.  
  79. void MyApp::on_timer()
  80. {
  81. price_ += double(rand()) / RAND_MAX * 2.0 - 1;
  82. if(price_ <= 0.01)
  83. price_ = 0.01;
  84. std::ostringstream ss;
  85. ss << price_;
  86. stream_->update(ss.str());
  87. }
  88.  
  89.  
  90. int main(int argc, char** argv)
  91. {
  92. try
  93. {
  94. cppcms::service service(argc, argv);
  95. booster::intrusive_ptr<MyApp> server = new MyApp(service);
  96. service.applications_pool().mount(server);
  97. service.run();
  98. }
  99. catch(std::exception const &e)
  100. {
  101. std::cerr << e.what() << std::endl;
  102. return 1;
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  109. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  110. <html>
  111. <head>
  112. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  113. <!-- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> -->
  114. <title>Chat Room</title>
  115. </head>
  116. <body>
  117. <h1>Stock Price</h1>
  118. <script type="text/javascript">
  119.  
  120. function read_data() {
  121. var stream = new EventSource('/ticker');
  122. stream.onmessage = function(e){
  123. console.log(e);
  124. console.log(e.type);
  125. document.getElementById('price').innerHTML=e.data;
  126. };
  127.  
  128. stream.onerror = function(e){
  129. console.log(e);
  130. };
  131. }
  132.  
  133. read_data();
  134. </script>
  135. <p>Price:<span id="price"></span></p>
  136. </body>
  137.  
  138. {
  139. "service" : {
  140. "api" : "http",
  141. "port" : 8080
  142. },
  143. "http" : {
  144. "script_names" : [ "/robot", "/ticker" ]
  145. },
  146. "file_server" : {
  147. "enable" : true,
  148. "document_root" : "."
  149. },
  150. }
  151.  
  152. ssetester: ssetester.o server_sent_events.o
  153. g++ -o ./ssetester ssetester.o server_sent_events.o -Wall -Werror -Wextra -fexceptions -ggdb -g3 -fPIE -pthread -L/usr/local/lib -lcppcms -lbooster
  154.  
  155. server_sent_events.o: server_sent_events.cpp
  156. g++ -Wall -Werror -Wextra -fexceptions -ggdb -g3 -fPIE -std=gnu++0x -I/usr/local/include/cppcms -c server_sent_events.cpp -o server_sent_events.o
  157.  
  158. ssetester.o: ssetester.cpp
  159. g++ -Wall -Werror -Wextra -fexceptions -ggdb -g3 -fPIE -std=gnu++0x -I/usr/local/include/cppcms -c ssetester.cpp -o ssetester.o
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement