Skip to content

Commit 61210b3

Browse files
authored
Merge pull request #5 from tinystruct/master
Added the session timeout and meeting expiration event notification
2 parents cd06efc + 5552179 commit 61210b3

File tree

2 files changed

+109
-50
lines changed

2 files changed

+109
-50
lines changed

src/themes/smalltalk.view

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,18 @@
347347
$('#messages').append($li);
348348
$('#list').animate({scrollTop: $('#messages').height()}, 'fast');
349349
break;
350+
case 'session-timeout':
351+
$li = $("<li />");
352+
$li.html("System Notification:\t" + $data.user + " left from this conversation.");
353+
$('#messages').append($li);
354+
$('#list').animate({scrollTop: $('#messages').height()}, 'fast');
355+
break;
356+
case 'expired':
357+
$li = $("<li />");
358+
$li.html("System Notification:\tIt's an expired conversation, you have to refresh the current page or request a new meeting code.");
359+
$('#messages').append($li);
360+
$('#list').animate({scrollTop: $('#messages').height()}, 'fast');
361+
break;
350362
default:
351363
timer.setAction(function(){
352364
document.title = $_user + " is typing" + dotted;
@@ -369,26 +381,40 @@
369381
data: { cmd: cmd }
370382
}).done(function( msg ) {
371383
if(msg.error == "missing user") {
372-
if (user) {
373-
$.ajax({
374-
type: "POST",
375-
url: "[%LINK:talk/start%]/"+ user
376-
}).done(function( msg ) {});
377-
}
378-
else {
379-
$("#modal").modal();
380-
}
381-
}
382-
383-
if(msg.error == "expired") {
384-
if(confirm("The conversation is expired, Do you want to reload the conversation?")) {
385-
$.ajax({
386-
type: "POST",
387-
url: $('#link').text()
388-
}).done(function( msg ) {});
389-
}
390-
}
384+
if (user) {
385+
$.ajax({
386+
type: "POST",
387+
url: "[%LINK:talk/start%]/"+ user
388+
}).done(function( msg ) {});
389+
}
390+
else {
391+
$("#modal").modal();
392+
}
393+
}
391394
}).fail(function( msg ) {
395+
switch(msg.responseJSON.error) {
396+
case 'session-timeout':
397+
$li = $("<li />");
398+
$li.html("System Notification:\t" + $data.user + " left from this conversation.");
399+
$('#messages').append($li);
400+
$('#list').animate({scrollTop: $('#messages').height()}, 'fast');
401+
402+
if(confirm("Your session is timed out, Do you want to reload the conversation?")) {
403+
$.ajax({
404+
type: "POST",
405+
url: $('#link').text()
406+
}).done(function( msg ) {});
407+
}
408+
break;
409+
case 'expired':
410+
$li = $("<li />");
411+
$li.html("System Notification:\tIt's an expired conversation, you have to refresh the current page or request a new meeting code.");
412+
$('#messages').append($li);
413+
$('#list').animate({scrollTop: $('#messages').height()}, 'fast');
414+
break;
415+
default:break;
416+
}
417+
392418
console.log('Fail.');
393419
console.log(msg);
394420
});
@@ -413,13 +439,22 @@
413439
$.getJSON( "[%LINK:talk/update%]/{%meeting_code%}/{%session_id%}", function(data) {
414440
update(data);
415441
is_running = false;
416-
}).done(function() {
442+
}).done(function( msg ) {
417443
autoupdate();
418444
is_running = false;
419445
}).fail(function( msg ) {
420-
connection_timer.start();
421-
is_running = false;
422-
console.log(msg);
446+
msg = msg.responseJSON;
447+
if(msg.error == "expired") {
448+
command("expired");
449+
}
450+
else if(msg.error == "session-timeout") {
451+
command("session-timeout");
452+
}
453+
else {
454+
connection_timer.start();
455+
is_running = false;
456+
console.log(msg);
457+
}
423458
});
424459
}
425460
}
@@ -630,7 +665,7 @@
630665
<input id="attachment" name="attachment" type="file" class="file-loading btn-default" multiple="true" />
631666
<br />
632667
<input type="button" id="button" value="Send" class="btn btn-primary" />
633-
<!--<input type="button" id="test" value="Test" class="btn btn-secondary" />-->
668+
<input type="button" id="test" value="Test" class="btn btn-secondary" />
634669
</form>
635670
</div>
636671

@@ -673,7 +708,7 @@
673708

674709
<footer class="footer">
675710
<div class="container">
676-
<p class="text-muted"> © 2016 </p>
711+
<p class="text-muted"> © 2017 </p>
677712
</div>
678713
</footer>
679714

src/tinystruct/examples/smalltalk.java

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public String command() {
163163

164164
return this.save(meetingCode, builder);
165165
}
166-
166+
response.setStatus(403);
167167
return "{ \"error\": \"expired\" }";
168168
}
169169

@@ -173,25 +173,28 @@ public String save() {
173173
response.setContentType("application/json");
174174

175175
final Object meetingCode = request.getSession().getAttribute("meeting_code");
176-
final String sessionId = request.getSession().getId();
177-
if ( meetingCode != null && sessions.get(meetingCode) != null && sessions.get(meetingCode).contains(sessionId)) {
178-
String message;
179-
if ((message = request.getParameter("text")) != null && !message.isEmpty()) {
180-
String[] agent = request.getHeader("User-Agent").split(" ");
181-
this.setVariable("browser", agent[agent.length - 1]);
182-
183-
final SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d h:m:s");
184-
final Builder builder = new Builder();
185-
builder.put("user", request.getSession().getAttribute("user"));
186-
builder.put("time", format.format(new Date()));
187-
builder.put("message", filter(message));
188-
builder.put("session_id", sessionId);
189-
190-
return this.save(meetingCode, builder);
176+
if (this.meetings.containsKey(meetingCode)) {
177+
final String sessionId = request.getSession().getId();
178+
if ( meetingCode != null && sessions.get(meetingCode) != null && sessions.get(meetingCode).contains(sessionId)) {
179+
String message;
180+
if ((message = request.getParameter("text")) != null && !message.isEmpty()) {
181+
String[] agent = request.getHeader("User-Agent").split(" ");
182+
this.setVariable("browser", agent[agent.length - 1]);
183+
184+
final SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d h:m:s");
185+
final Builder builder = new Builder();
186+
builder.put("user", request.getSession().getAttribute("user"));
187+
builder.put("time", format.format(new Date()));
188+
builder.put("message", filter(message));
189+
builder.put("session_id", sessionId);
190+
191+
return this.save(meetingCode, builder);
192+
}
191193
}
192194
}
193-
194-
return "{}";
195+
196+
response.setStatus(403);
197+
return "{ \"error\": \"expired\" }";
195198
}
196199

197200
public String update() throws ApplicationException, IOException {
@@ -201,14 +204,27 @@ public String update() throws ApplicationException, IOException {
201204
if (meetingCode != null) {
202205
return this.update(meetingCode.toString(), sessionId);
203206
}
204-
return "";
207+
final HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
208+
response.setContentType("application/json");
209+
response.setStatus(403);
210+
return "{ \"error\": \"expired\" }";
205211
}
206212

207213
public String update(String meetingCode, String sessionId) throws ApplicationException, IOException {
208-
if (sessions.get(meetingCode) != null && sessions.get(meetingCode).contains(sessionId)) {
209-
return this.update(sessionId);
214+
if (this.meetings.containsKey(meetingCode)) {
215+
if(sessions.get(meetingCode) != null && sessions.get(meetingCode).contains(sessionId)) {
216+
return this.update(sessionId);
217+
}
218+
final HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
219+
response.setContentType("application/json");
220+
response.setStatus(403);
221+
return "{ \"error\": \"session-timeout\" }";
210222
}
211-
return "";
223+
224+
final HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");
225+
response.setContentType("application/json");
226+
response.setStatus(403);
227+
return "{ \"error\": \"expired\" }";
212228
}
213229

214230
public String upload() throws ApplicationException {
@@ -307,16 +323,24 @@ public void sessionCreated(HttpSessionEvent arg0) {
307323
public void sessionDestroyed(HttpSessionEvent arg0) {
308324
Object meetingCode = arg0.getSession().getAttribute("meeting_code");
309325
if ( meetingCode != null ) {
326+
final SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d h:m:s");
327+
final Builder builder = new Builder();
328+
builder.put("user", null);
329+
builder.put("time", format.format(new Date()));
330+
builder.put("cmd", "expired");
331+
this.save(meetingCode, builder);
332+
310333
Queue<Builder> messages;
311334
List<String> session_ids;
312-
synchronized (meetings) {
313-
if((session_ids = this.sessions.get(meetingCode)) != null)
314-
{
335+
synchronized (meetings) {
336+
if((session_ids = this.sessions.get(meetingCode)) != null) {
315337
session_ids.remove(arg0.getSession().getId());
316338
}
339+
317340
if ((messages = meetings.get(meetingCode)) != null) {
318341
messages.remove(meetingCode);
319342
}
343+
320344
meetings.notifyAll();
321345
}
322346

0 commit comments

Comments
 (0)