Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ws.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_http
3/// This program demonstrate WebSocket usage with THttpServer
4/// Custom ws.htm page is loaded and regularly sends messages to server
5///
6/// \macro_code
7///
8/// \author Sergey Linev
9
10#include "THttpServer.h"
11#include "THttpWSHandler.h"
12#include "THttpCallArg.h"
13#include "TString.h"
14#include "TSystem.h"
15#include "TDatime.h"
16#include "TTimer.h"
17
18#include <cstdio>
19
20class TUserHandler : public THttpWSHandler {
21 public:
22 UInt_t fWSId{0};
23 Int_t fServCnt{0};
24
25 TUserHandler(const char *name = nullptr, const char *title = nullptr) : THttpWSHandler(name, title) {}
26
27 // load custom HTML page when open correspondent address
28 TString GetDefaultPageContent() override { return "file:ws.htm"; }
29
30 Bool_t ProcessWS(THttpCallArg *arg) override
31 {
32 if (!arg || (arg->GetWSId()==0)) return kTRUE;
33
34 // printf("Method %s\n", arg->GetMethod());
35
36 if (arg->IsMethod("WS_CONNECT")) {
37 // accept only if connection not established
38 return fWSId == 0;
39 }
40
41 if (arg->IsMethod("WS_READY")) {
42 fWSId = arg->GetWSId();
43 printf("Client connected %d\n", fWSId);
44 return kTRUE;
45 }
46
47 if (arg->IsMethod("WS_CLOSE")) {
48 fWSId = 0;
49 printf("Client disconnected\n");
50 return kTRUE;
51 }
52
53 if (arg->IsMethod("WS_DATA")) {
54 TString str;
55 str.Append((const char *)arg->GetPostData(), arg->GetPostDataLength());
56 printf("Client msg: %s\n", str.Data());
57 TDatime now;
58 SendCharStarWS(arg->GetWSId(), Form("Server replies:%s server counter:%d", now.AsString(), fServCnt++));
59 return kTRUE;
60 }
61
62 return kFALSE;
63 }
64
65 /// per timeout sends data portion to the client
66 Bool_t HandleTimer(TTimer *) override
67 {
68 TDatime now;
69 if (fWSId) SendCharStarWS(fWSId, Form("Server sends data:%s server counter:%d", now.AsString(), fServCnt++));
70 return kTRUE;
71 }
72
73};
74
75void ws()
76{
77 THttpServer *serv = new THttpServer("http:8090");
78
79 TUserHandler *handler = new TUserHandler("name1", "title1");
80
81 serv->Register("/folder1", handler);
82
83 const char *addr = "http://localhost:8090/folder1/name1/";
84
85 printf("Starting browser with URL address %s\n", addr);
86 printf("In browser content of ws.htm file should be loaded\n");
87 printf("Please be sure that ws.htm is provided in current directory\n");
88
89 if (gSystem->InheritsFrom("TMacOSXSystem"))
90 gSystem->Exec(Form("open %s", addr));
91 else if (gSystem->InheritsFrom("TWinNTSystem"))
92 gSystem->Exec(Form("start %s", addr));
93 else
94 gSystem->Exec(Form("xdg-open %s &", addr));
95
96 // when connection will be established, data will be send to the client
97 TTimer *tm = new TTimer(handler, 3700);
98 tm->Start();
99}
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
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
Contains arguments for single HTTP call.
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
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
Bool_t Register(const char *subfolder, TObject *obj)
Register object in subfolder.
Class for user-side handling of websocket with THttpServer.
virtual Bool_t ProcessWS(THttpCallArg *arg)=0
Int_t SendCharStarWS(UInt_t wsid, const char *str)
Send string via given websocket id.
virtual TString GetDefaultPageContent()
Provides content of default web page for registered web-socket handler Can be content of HTML page or...
virtual Bool_t HandleTimer(TTimer *timer)
Execute action in response of a timer timing out.
Definition TObject.cxx:493
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:525
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
TString & Append(const char *cs)
Definition TString.h:576
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:640
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:213