forked from resgateio/resgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
132 lines (119 loc) · 4.23 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Resgate - Client Session Example</title>
<script src="https://unpkg.com/resclient@latest/dist/resclient.min.js"></script>
</head>
<body>
<h3>Resgate Client Session Example</h3>
<p>The client lets you log in with a user session to see a counter updated from <i>tickerService.js</i>.
<p>The session will not be lost on client reload (F5) or from a restart of Resgate.</p>
<p>Resgate will stop sending updates as soon as the client is logged out.</p>
<hr>
<div id="login-panel" style="display: none">
<div>Available usernames / passwords are:
<ul>
<li><code>admin / admin</code></li>
<li><code>guest / guest</code></li>
<li><code>jane / jane</code></li>
<li><code>john / john</code></li>
</ul>
</div>
<label>Username: <input id="input-username" /></label>
<label>Password: <input id="input-password" /></label>
<button onclick="login()">Login</button>
</div>
<div id="logout-panel" style="display: none">
You are logged in:
<ul>
<li><span>ID: </span><span id="userid">1</span></li>
<li><span>Name: </span><span id="name">John Doe</span></li>
<li><span>Role: </span><span id="role">user</span></li>
</ul>
<button id="btn-logout" onclick="logout()">Logout</button>
</div>
<hr>
<div id="message">Login to see the counter</div>
<script>
const ResClient = resclient.default;
let client = new ResClient('ws://localhost:8080');
let message = document.getElementById('message');
let reloginTimer = null;
let loadTickerModel = (function() {
let modelPromise = null;
return function() {
// Only load model once
if (modelPromise) return;
// Get the model from the service.
modelPromise = client.get('ticker.model').then(model => {
let setText = () => message.textContent = "Seconds since service start: " + model.seconds;
model.on('change', setText);
setText();
}).catch(err => {
message.textContent = err.message;
modelPromise = null;
});
};
}());
function startReloginTimer() {
clearTimeout(reloginTimer);
// Relogin after 1 min = 60000 ms
reloginTimer = setTimeout(relogin, 60000);
}
function updateUserInfo(user) {
// No user ID means we are not logged in
if (!user.id) {
document.getElementById('login-panel').style.display = "block";
document.getElementById('logout-panel').style.display = "none";
relogin();
} else {
document.getElementById('login-panel').style.display = "none";
document.getElementById('logout-panel').style.display = "block";
document.getElementById('userid').textContent = user.id;
document.getElementById('name').textContent = user.name;
document.getElementById('role').textContent = user.role;
loadTickerModel();
}
}
function login() {
let username = document.getElementById('input-username').value;
let password = document.getElementById('input-password').value;
client.authenticate('session', 'login', { username, password }).then(result => {
localStorage.setItem("reloginKey", result.reloginKey);
startReloginTimer();
}).catch(err => {
message.textContent = err.message;
});
}
function logout() {
client.authenticate('session', 'logout').catch(err => {
message.textContent = err.message;
});
}
function relogin() {
let reloginKey = localStorage.getItem('reloginKey');
// Quick escape if we have no relogin key
if (!reloginKey) return;
// Try to relogin with key
return client.authenticate('session', 'relogin', { reloginKey }).then(result => {
localStorage.setItem("reloginKey", result.reloginKey);
startReloginTimer();
}).catch(err => {
message.textContent = err.message;
localStorage.removeItem("reloginKey");
});
}
// Add relogin callback when client (re)connects
client.setOnConnect(relogin);
// Get the connection user model
// It uses special tag {cid} which Resgate will replace with the actual connection ID
client.get('session.user.{cid}').then(user => {
user.on('change', () => updateUserInfo(user));
updateUserInfo(user);
}).catch(err => {
message.textContent = err.message;
});
</script>
</body>
</html>