Skip to content
Snippets Groups Projects
Commit 2b26a73b authored by Sergey Linev's avatar Sergey Linev Committed by Philippe Canal
Browse files

http: update websocket tutorial

parent a489011f
No related branches found
No related tags found
No related merge requests found
...@@ -11,8 +11,9 @@ ...@@ -11,8 +11,9 @@
class TUserHandler : public THttpWSHandler { class TUserHandler : public THttpWSHandler {
public: public:
UInt_t fWSId; UInt_t fWSId;
Int_t fServCnt;
TUserHandler(const char *name = 0, const char *title = 0) : THttpWSHandler(name, title), fWSId(0) {} TUserHandler(const char *name = 0, const char *title = 0) : THttpWSHandler(name, title), fWSId(0), fServCnt(0) {}
// load custom HTML page when open correpondent address // load custom HTML page when open correpondent address
TString GetDefaultPageContent() { return "file:ws.htm"; } TString GetDefaultPageContent() { return "file:ws.htm"; }
...@@ -42,7 +43,7 @@ class TUserHandler : public THttpWSHandler { ...@@ -42,7 +43,7 @@ class TUserHandler : public THttpWSHandler {
TString str = arg->GetPostDataAsString(); TString str = arg->GetPostDataAsString();
printf("Client msg: %s\n", str.Data()); printf("Client msg: %s\n", str.Data());
TDatime now; TDatime now;
SendCharStarWS(arg->GetWSId(), Form("Server replies %s", now.AsString())); SendCharStarWS(arg->GetWSId(), Form("Server replies:%s server counter:%d", now.AsString(), fServCnt++));
return kTRUE; return kTRUE;
} }
...@@ -53,7 +54,7 @@ class TUserHandler : public THttpWSHandler { ...@@ -53,7 +54,7 @@ class TUserHandler : public THttpWSHandler {
virtual Bool_t HandleTimer(TTimer *) virtual Bool_t HandleTimer(TTimer *)
{ {
TDatime now; TDatime now;
if (fWSId) SendCharStarWS(fWSId, Form("Server sends data %s", now.AsString())); if (fWSId) SendCharStarWS(fWSId, Form("Server sends data:%s server counter:%d", now.AsString(), fServCnt++));
return kTRUE; return kTRUE;
} }
......
...@@ -11,20 +11,21 @@ ...@@ -11,20 +11,21 @@
<h3>Log protocol:</h3> <h3>Log protocol:</h3>
<div id="LogDiv"></div> <div id="LogDiv"></div>
</body> </body>
<script type='text/javascript'> <script type='text/javascript'>
var path = window.location.href; var path = window.location.href;
path = path.replace("http://", "ws://") + "root.websocket"; path = path.replace("http://", "ws://") + "root.websocket";
var cnt = 0;
function show(str) { function show(str) {
document.getElementById('LogDiv').insertAdjacentHTML( 'beforeend', '<text>' + str + '</text><br/>'); document.getElementById('LogDiv').insertAdjacentHTML( 'beforeend', '<text>' + str + '</text><br/>');
console.log(str); console.log(str);
} }
console.log('starting socket ' + path); console.log('starting socket ' + path);
var socket = new WebSocket(path); var socket = new WebSocket(path);
socket.onopen = function() { socket.onopen = function() {
show('websocket initialized'); show('websocket initialized');
this._ready = true; this._ready = true;
...@@ -34,7 +35,7 @@ ...@@ -34,7 +35,7 @@
socket.onmessage = function(e) { socket.onmessage = function(e) {
var msg = e.data; var msg = e.data;
if (typeof msg != 'string') return console.log("unsupported message kind: " + (typeof msg)); if (typeof msg != 'string') return console.log("unsupported message kind: " + (typeof msg));
show('get: ' + msg); show('get: ' + msg);
} }
...@@ -48,11 +49,11 @@ ...@@ -48,11 +49,11 @@
this._ready = false; this._ready = false;
show('websocket error' + err); show('websocket error' + err);
} }
// sends data every 5 seconds // sends data every 5 seconds
setInterval(function() { setInterval(function() {
if (socket._ready) socket.send("Client time " + new Date().toTimeString()); if (socket._ready) socket.send("Client time " + new Date().toTimeString() + " client counter:" + (cnt++));
}, 5000); }, 1000);
</script> </script>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment