Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
12 public:
15
16 TUserHandler(const char *name = nullptr, const char *title = nullptr) : THttpWSHandler(name, title) {}
17
18 // load custom HTML page when open correspondent address
19 TString GetDefaultPageContent() override { return "file:ws.htm"; }
20
22 {
23 if (!arg || (arg->GetWSId()==0)) return kTRUE;
24
25 // printf("Method %s\n", arg->GetMethod());
26
27 if (arg->IsMethod("WS_CONNECT")) {
28 // accept only if connection not established
29 return fWSId == 0;
30 }
31
32 if (arg->IsMethod("WS_READY")) {
33 fWSId = arg->GetWSId();
34 printf("Client connected %d\n", fWSId);
35 return kTRUE;
36 }
37
38 if (arg->IsMethod("WS_CLOSE")) {
39 fWSId = 0;
40 printf("Client disconnected\n");
41 return kTRUE;
42 }
43
44 if (arg->IsMethod("WS_DATA")) {
45 TString str;
46 str.Append((const char *)arg->GetPostData(), arg->GetPostDataLength());
47 printf("Client msg: %s\n", str.Data());
48 TDatime now;
49 SendCharStarWS(arg->GetWSId(), Form("Server replies:%s server counter:%d", now.AsString(), fServCnt++));
50 return kTRUE;
51 }
52
53 return kFALSE;
54 }
55
56 /// per timeout sends data portion to the client
58 {
59 TDatime now;
60 if (fWSId) SendCharStarWS(fWSId, Form("Server sends data:%s server counter:%d", now.AsString(), fServCnt++));
61 return kTRUE;
62 }
63
64};
65
66void ws()
67{
68 THttpServer *serv = new THttpServer("http:8090");
69
70 TUserHandler *handler = new TUserHandler("name1", "title1");
71
72 serv->Register("/folder1", handler);
73
74 const char *addr = "http://localhost:8090/folder1/name1/";
75
76 printf("Starting browser with URL address %s\n", addr);
77 printf("In browser content of ws.htm file should be loaded\n");
78 printf("Please be sure that ws.htm is provided in current directory\n");
79
80 if (gSystem->InheritsFrom("TMacOSXSystem"))
81 gSystem->Exec(Form("open %s", addr));
82 else if (gSystem->InheritsFrom("TWinNTSystem"))
83 gSystem->Exec(Form("start %s", addr));
84 else
85 gSystem->Exec(Form("xdg-open %s &", addr));
86
87 // when connection will be established, data will be send to the client
88 TTimer *tm = new TTimer(handler, 3700);
89 tm->Start();
90}
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
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 * AsString() const
Return the date & time as a string (ctime() format).
Definition TDatime.cxx:102
UInt_t GetWSId() const
get web-socket id
const void * GetPostData() const
return pointer on posted with request data
Long_t GetPostDataLength() const
return length of posted with request data
Bool_t IsMethod(const char *name) const
returns kTRUE if post method is used
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
Int_t SendCharStarWS(UInt_t wsid, const char *str)
Send string via given websocket id Returns -1 - in case of error, 0 - when operation was executed imm...
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:515
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
TString & Append(const char *cs)
Definition TString.h:564
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:656
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
virtual void Start(Long_t milliSec=-1, Bool_t singleShot=kFALSE)
Starts the timer with a milliSec timeout.
Definition TTimer.cxx:211
Bool_t ProcessWS(THttpCallArg *arg) override
Definition ws.C:21
Int_t fServCnt
Definition ws.C:14
Bool_t HandleTimer(TTimer *) override
per timeout sends data portion to the client
Definition ws.C:57
UInt_t fWSId
Definition ws.C:13
TString GetDefaultPageContent() override
Provides content of default web page for registered web-socket handler Can be content of HTML page or...
Definition ws.C:19
TUserHandler(const char *name=nullptr, const char *title=nullptr)
Definition ws.C:16
void ws()
Definition ws.C:66