Logo ROOT   6.14/05
Reference Guide
ws.C
Go to the documentation of this file.
1 #include "THttpServer.h"
2 #include "THttpWSHandler.h"
3 #include "THttpCallArg.h"
4 #include "TString.h"
5 #include "TSystem.h"
6 #include "TDatime.h"
7 #include "TTimer.h"
8 
9 #include <cstdio>
10 
11 class TUserHandler : public THttpWSHandler {
12  public:
13  UInt_t fWSId;
14 
15  TUserHandler(const char *name = 0, const char *title = 0) : THttpWSHandler(name, title), fWSId(0) {}
16 
17  // load custom HTML page when open correpondent address
18  TString GetDefaultPageContent() { return "file:ws.htm"; }
19 
20  virtual Bool_t ProcessWS(THttpCallArg *arg)
21  {
22  if (!arg || (arg->GetWSId()==0)) return kTRUE;
23 
24  // printf("Method %s\n", arg->GetMethod());
25 
26  if (arg->IsMethod("WS_CONNECT")) {
27  // accept only if connection not established
28  return fWSId == 0;
29  }
30 
31  if (arg->IsMethod("WS_READY")) {
32  fWSId = arg->GetWSId();
33  return kTRUE;
34  }
35 
36  if (arg->IsMethod("WS_CLOSE")) {
37  fWSId = 0;
38  return kTRUE;
39  }
40 
41  if (arg->IsMethod("WS_DATA")) {
42  TString str = arg->GetPostDataAsString();
43  printf("Client msg: %s\n", str.Data());
44  TDatime now;
45  SendCharStarWS(arg->GetWSId(), Form("Server replies %s", now.AsString()));
46  return kTRUE;
47  }
48 
49  return kFALSE;
50  }
51 
52  /// per timeout sends data portion to the client
53  virtual Bool_t HandleTimer(TTimer *)
54  {
55  TDatime now;
56  if (fWSId) SendCharStarWS(fWSId, Form("Server sends data %s", now.AsString()));
57  return kTRUE;
58  }
59 
60 };
61 
62 void ws()
63 {
64  THttpServer *serv = new THttpServer("http:8090");
65 
66  TUserHandler *handler = new TUserHandler("name1","title1");
67 
68  serv->Register("/folder1", handler);
69 
70  const char *addr = "http://localhost:8090/folder1/name1/";
71 
72  printf("Starting browser with URL address %s\n", addr);
73  printf("In browser content of ws.htm file should be loaded\n");
74 
75  if (gSystem->InheritsFrom("TMacOSXSystem"))
76  gSystem->Exec(Form("open %s", addr));
77  else
78  gSystem->Exec(Form("xdg-open %s &", addr));
79 
80  // when connection will be established, data will be send to the client
81  TTimer *tm = new TTimer(handler, 3700);
82  tm->Start();
83 }
Bool_t IsMethod(const char *name) const
returns kTRUE if post method is used
Definition: THttpCallArg.h:129
UInt_t GetWSId() const
get web-socket id
Definition: THttpCallArg.h:108
void ws()
Definition: ws.C:62
Basic string class.
Definition: TString.h:131
bool Bool_t
Definition: RtypesCore.h:59
virtual void Start(Long_t milliSec=-1, Bool_t singleShot=kFALSE)
Starts the timer with a milliSec timeout.
Definition: TTimer.cxx:211
virtual Bool_t HandleTimer(TTimer *timer)
Execute action in response of a timer timing out.
Definition: TObject.cxx:411
TString GetPostDataAsString() const _R__DEPRECATED_618("Use other methods to access POST data")
returns post data as TString
Definition: THttpCallArg.h:141
void SendCharStarWS(UInt_t wsid, const char *str)
Send string via given websocket id.
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
virtual TString GetDefaultPageContent()
Provides content of default web page for registered web-socket handler Can be content of HTML page or...
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:661
const Bool_t kFALSE
Definition: RtypesCore.h:88
const char * AsString() const
Return the date & time as a string (ctime() format).
Definition: TDatime.cxx:101
virtual Bool_t ProcessWS(THttpCallArg *arg)=0
friend class THttpServer
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:37
const char * Data() const
Definition: TString.h:364