Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main(std::string /*url*/)
- {
- stream_->accept(release_context());
- }
- #include <fstream>
- #include <iostream>
- #include "server_sent_events.h"
- #include <cppcms/application.h>
- #include <cppcms/applications_pool.h>
- #include <cppcms/service.h>
- #include <cppcms/http_context.h>
- #include <cppcms/http_response.h>
- #include <cppcms/url_dispatcher.h>
- #include <cppcms/url_mapper.h>
- #include <booster/aio/deadline_timer.h>
- #include <booster/system_error.h>
- using namespace std;
- class MyApp: public cppcms::application
- {
- private:
- booster::shared_ptr<sse::state_stream> stream_;
- booster::aio::deadline_timer tm_;
- double price_;
- public:
- MyApp(cppcms::service &srv);
- void showStatus();
- void wait();
- void on_timer();
- };
- MyApp::MyApp(cppcms::service &service) : cppcms::application(service), tm_(service.get_io_service()), price_(1.0)
- {
- dispatcher().assign("/showstatus",&MyApp::showStatus,this);
- mapper().assign("Show Status","/showstatus");
- mapper().root("/robot");
- stream_ = sse::state_stream::create(service.get_io_service());
- wait();
- }
- /*
- void MyApp::main(std::string)
- {
- cout << "MAIN CALLED" << endl;
- stream_->accept(release_context());
- }
- */
- void MyApp::showStatus()
- {
- ifstream fileStream("showstatus.html");
- stringstream buffer;
- buffer << fileStream.rdbuf();
- string page = buffer.str();
- response().out() << page.c_str();
- stream_->accept(release_context());
- }
- void MyApp::wait()
- {
- tm_.expires_from_now(booster::ptime::from_number(double(rand())/RAND_MAX + 0.01));
- tm_.async_wait([=](booster::system::error_code const &e){
- if(!e) {
- on_timer();
- wait();
- }
- >> });
- }
- void MyApp::on_timer()
- {
- price_ += double(rand()) / RAND_MAX * 2.0 - 1;
- if(price_ <= 0.01)
- price_ = 0.01;
- std::ostringstream ss;
- ss << price_;
- stream_->update(ss.str());
- }
- int main(int argc, char** argv)
- {
- try
- {
- cppcms::service service(argc, argv);
- booster::intrusive_ptr<MyApp> server = new MyApp(service);
- service.applications_pool().mount(server);
- service.run();
- }
- catch(std::exception const &e)
- {
- std::cerr << e.what() << std::endl;
- return 1;
- }
- return 0;
- }
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <!-- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> -->
- <title>Chat Room</title>
- </head>
- <body>
- <h1>Stock Price</h1>
- <script type="text/javascript">
- function read_data() {
- var stream = new EventSource('/ticker');
- stream.onmessage = function(e){
- console.log(e);
- console.log(e.type);
- document.getElementById('price').innerHTML=e.data;
- };
- stream.onerror = function(e){
- console.log(e);
- };
- }
- read_data();
- </script>
- <p>Price:<span id="price"></span></p>
- </body>
- {
- "service" : {
- "api" : "http",
- "port" : 8080
- },
- "http" : {
- "script_names" : [ "/robot", "/ticker" ]
- },
- "file_server" : {
- "enable" : true,
- "document_root" : "."
- },
- }
- ssetester: ssetester.o server_sent_events.o
- g++ -o ./ssetester ssetester.o server_sent_events.o -Wall -Werror -Wextra -fexceptions -ggdb -g3 -fPIE -pthread -L/usr/local/lib -lcppcms -lbooster
- server_sent_events.o: server_sent_events.cpp
- 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
- ssetester.o: ssetester.cpp
- 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