Skip to content

Commit 13a2f45

Browse files
authored
Merge pull request #127 from jumpserver/pr@dev@feat_refresh_other_tab_active_time
feat: refresh other tab last input active time
2 parents a40cb36 + 7fa126b commit 13a2f45

8 files changed

Lines changed: 57 additions & 7 deletions

File tree

backend/framework/src/main/java/org/jumpserver/chen/framework/session/Session.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
import java.nio.file.Path;
1414
import java.sql.Connection;
1515
import java.sql.SQLException;
16+
import java.time.LocalDateTime;
1617
import java.util.Locale;
1718
import java.util.Map;
1819

1920
public interface Session {
2021

22+
void refreshLastActiveTime();
23+
24+
LocalDateTime getLastActiveTime();
25+
2126
boolean canUpload();
2227

2328
boolean canDownload();

backend/framework/src/main/java/org/jumpserver/chen/framework/session/controller/impl/BaseController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jumpserver.chen.framework.session.controller.impl;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.jumpserver.chen.framework.session.SessionManager;
45
import org.jumpserver.chen.framework.session.controller.Controller;
56
import org.jumpserver.chen.framework.session.controller.dialog.Dialog;
67
import org.jumpserver.chen.framework.session.controller.message.Message;
@@ -40,11 +41,20 @@ public void handlePacket(Packet packet) {
4041
case "dialog_event":
4142
this.onDialogEvent((String) packet.getData());
4243
break;
44+
case "input_active":
45+
this.refreshLastActiveTime();
46+
break;
4347
default:
4448
break;
4549
}
4650
}
4751

52+
53+
private void refreshLastActiveTime() {
54+
var session = SessionManager.getCurrentSession();
55+
session.refreshLastActiveTime();
56+
}
57+
4858
@Override
4959
public void sendFile(String fileKey) {
5060
this.packetIO.sendPacket("download", fileKey);

backend/framework/src/main/java/org/jumpserver/chen/framework/session/impl/BaseSession.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.Paths;
2222
import java.sql.Connection;
2323
import java.sql.SQLException;
24+
import java.time.LocalDateTime;
2425
import java.util.Locale;
2526
import java.util.Map;
2627
import java.util.concurrent.ConcurrentHashMap;
@@ -57,13 +58,21 @@ public class BaseSession implements Session {
5758

5859
private boolean enableAutoComplete = true;
5960

61+
@Getter
62+
private LocalDateTime lastActiveTime;
63+
6064

6165
public BaseSession(Datasource datasource, String remoteAddr) {
6266
this.datasource = datasource;
6367
this.remoteAddr = remoteAddr;
6468
}
6569

6670

71+
@Override
72+
public void refreshLastActiveTime() {
73+
this.lastActiveTime = LocalDateTime.now();
74+
}
75+
6776
@Override
6877
public boolean canUpload() {
6978
return true;

backend/framework/src/main/java/org/jumpserver/chen/framework/session/impl/JMSSession.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public class JMSSession extends BaseSession {
5151
private final long expireTime;
5252

5353

54-
private LocalDateTime lastActiveTime;
55-
5654
private LocalDateTime maxSessionEndTime;
5755
private int maxSessionEndHours;
5856
private LocalDateTime dynamicEndTime;
@@ -205,7 +203,7 @@ private void recordLifecycle(ServiceOuterClass.SessionLifecycleLogRequest.EventT
205203
}
206204

207205
private void startWaitIdleTime() {
208-
this.lastActiveTime = LocalDateTime.now();
206+
this.refreshLastActiveTime();
209207

210208
var token = SessionManager.getContextToken();
211209

@@ -224,7 +222,7 @@ private void startWaitIdleTime() {
224222
return;
225223
}
226224

227-
if (Math.abs(Duration.between(LocalDateTime.now(), this.lastActiveTime).toMinutes()) > this.maxIdleTimeDelta) {
225+
if (Math.abs(Duration.between(LocalDateTime.now(), this.getLastActiveTime()).toMinutes()) > this.maxIdleTimeDelta) {
228226
this.close("OverMaxIdleTimeError", "idle_disconnect", this.maxIdleTimeDelta);
229227
return;
230228
}
@@ -310,7 +308,7 @@ private void closeGateway() {
310308
@Override
311309
public SQLQueryResult withAudit(String command, QueryAuditFunction queryAuditFunction) throws SQLException, CommandRejectException {
312310
synchronized (this) {
313-
this.lastActiveTime = LocalDateTime.now();
311+
this.refreshLastActiveTime();
314312
}
315313
if (this.locked) {
316314
throw new CommandRejectException(MessageUtils.get("SessionLockedError"));

frontend/src/bus/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Vue from 'vue'
2+
3+
export const bus = new Vue()

frontend/src/components/Controller/index.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
v-if="dialogOptions.body && dialogOptions.bodyType==='html'"
1212
v-html="dialogOptions.body"
1313
/>
14-
<div v-else v-text="dialogOptions.body" />
14+
<div v-else v-text="dialogOptions.body"/>
1515

1616
<span v-if="dialogOptions && dialogOptions.buttons" slot="footer" class="dialog-footer" style="text-align: center">
1717
<el-button
@@ -27,6 +27,7 @@
2727
import { getUrlParams } from '@/utils/field'
2828
import { LunaEvent, MESSAGES } from '@/utils/luna'
2929
import { index } from '@/request'
30+
import { bus } from '@/bus'
3031
3132
export default {
3233
name: 'Controller',
@@ -49,8 +50,20 @@ export default {
4950
this.lunaEvent.init()
5051
this.auth()
5152
this.handleRenewLunaSession()
53+
this.handleInputActive()
5254
},
5355
methods: {
56+
handleInputActive() {
57+
bus.$on('input-active', () => {
58+
this.lunaEvent.sendEventToLuna(MESSAGES.INPUT_ACTIVE)
59+
})
60+
61+
this.lunaEvent.setInputActiveHandler(() => {
62+
console.log('receive other window input active message')
63+
this.sendPacket('input_active')
64+
})
65+
},
66+
5467
handleRenewLunaSession() {
5568
const lunaEvent = this.lunaEvent
5669

frontend/src/components/Main/Explore/QueryConsole/CodeEditor.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import 'codemirror/addon/edit/matchbrackets.js'
6464
import { getHints } from '@/api/resource'
6565
import SelectSnippetDialog from '@/components/Main/Explore/QueryConsole/SelectSnippetDialog.vue'
6666
import SaveSnippetDialog from '@/components/Main/Explore/QueryConsole/SaveSnippetDialog.vue'
67+
import { bus } from '@/bus'
6768
6869
const formatterMap = {
6970
'clickhouse': 'sql',
@@ -283,6 +284,9 @@ export default {
283284
this.cm = cm
284285
},
285286
onCmChange(cm, change) {
287+
// 向 luna 发送事件
288+
bus.$emit('input-active')
289+
286290
const { text, origin } = change[0]
287291
if (origin === '+input' && text[0].trim()) {
288292
if (this.autoComplete) {

frontend/src/utils/luna.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export const MESSAGES = {
44
CLOSE: 'CLOSE',
55
CONNECTED: 'CONNECTED',
66
KEYEVENT: 'KEYEVENT',
7-
MOUSEEVENT: 'MOUSEEVENT'
7+
MOUSEEVENT: 'MOUSEEVENT',
8+
INPUT_ACTIVE: 'INPUT_ACTIVE'
89
}
910

1011
export class LunaEvent {
@@ -25,9 +26,16 @@ export class LunaEvent {
2526
this.origin = event.origin
2627
this.sendEventToLuna(MESSAGES.PONG)
2728
break
29+
case MESSAGES.INPUT_ACTIVE:
30+
this.inputActiveHandler && this.inputActiveHandler()
31+
break
2832
}
2933
}
3034

35+
setInputActiveHandler(func) {
36+
this.inputActiveHandler = func
37+
}
38+
3139
sendEventToLuna(name, data) {
3240
data == null && (data = '')
3341
if (this.lunaId != null) {

0 commit comments

Comments
 (0)