Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ws.C File Reference

Detailed Description

This program demonstrate WebSocket usage with THttpServer Custom ws.htm page is loaded and regularly sends messages to server.

#include "THttpServer.h"
#include "THttpWSHandler.h"
#include "THttpCallArg.h"
#include "TString.h"
#include "TSystem.h"
#include "TDatime.h"
#include "TTimer.h"
#include <cstdio>
class TUserHandler : public THttpWSHandler {
public:
UInt_t fWSId{0};
Int_t fServCnt{0};
TUserHandler(const char *name = nullptr, const char *title = nullptr) : THttpWSHandler(name, title) {}
// load custom HTML page when open correspondent address
TString GetDefaultPageContent() override { return "file:ws.htm"; }
Bool_t ProcessWS(THttpCallArg *arg) override
{
if (!arg || (arg->GetWSId() == 0)) return kTRUE;
// printf("Method %s\n", arg->GetMethod());
if (arg->IsMethod("WS_CONNECT")) {
// accept only if connection not established
return fWSId == 0;
}
if (arg->IsMethod("WS_READY")) {
fWSId = arg->GetWSId();
printf("Client connected %d\n", fWSId);
return kTRUE;
}
if (arg->IsMethod("WS_CLOSE")) {
fWSId = 0;
printf("Client disconnected\n");
return kTRUE;
}
if (arg->IsMethod("WS_DATA")) {
TString str;
str.Append((const char *)arg->GetPostData(), arg->GetPostDataLength());
printf("Client msg: %s\n", str.Data());
TDatime now;
SendCharStarWS(arg->GetWSId(), TString::Format("Server replies:%s server counter:%d", now.AsString(), fServCnt++));
return kTRUE;
}
return kFALSE;
}
/// per timeout sends data portion to the client
{
TDatime now;
if (fWSId) SendCharStarWS(fWSId, TString::Format("Server sends data:%s server counter:%d", now.AsString(), fServCnt++));
return kTRUE;
}
};
void ws()
{
THttpServer *serv = new THttpServer("http:8090");
TUserHandler *handler = new TUserHandler("name1", "title1");
serv->Register("/folder1", handler);
const char *addr = "http://localhost:8090/folder1/name1/";
printf("Starting browser with URL address %s\n", addr);
printf("In browser content of ws.htm file should be loaded\n");
printf("Please be sure that ws.htm is provided in current directory\n");
if (gSystem->InheritsFrom("TMacOSXSystem"))
gSystem->Exec(TString::Format("open %s", addr));
else if (gSystem->InheritsFrom("TWinNTSystem"))
gSystem->Exec(TString::Format("start %s", addr));
else
gSystem->Exec(TString::Format("xdg-open %s &", addr));
// when connection will be established, data will be send to the client
TTimer *tm = new TTimer(handler, 3700);
tm->Start();
}
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:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
char name[80]
Definition TGX11.cxx:110
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
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:510
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:542
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & Append(const char *cs)
Definition TString.h:572
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:653
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
Author
Sergey Linev

Definition in file ws.C.