Skip to content

Commit

Permalink
feat: set auto refresh is on by default
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Nov 10, 2024
1 parent 03b3636 commit 0f39a36
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions web/src/components/MessagesTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
Popup,
Table,
} from 'semantic-ui-react';
import { API, openPage, showError, showSuccess, showWarning } from '../helpers';
import {
API,
openPage,
showError,
showInfo,
showSuccess,
showWarning,
} from '../helpers';

import { ITEMS_PER_PAGE } from '../constants';
import { renderTimestamp } from '../helpers/render';
Expand All @@ -20,7 +27,7 @@ function renderStatus(status) {
case 1:
return (
<Label basic color='olive'>
正在投递
正在发送
</Label>
);
case 2:
Expand Down Expand Up @@ -53,7 +60,7 @@ function renderStatus(status) {
const MessagesTable = () => {
const [messages, setMessages] = useState([]);
const [loading, setLoading] = useState(true);
const [autoRefresh, setAutoRefresh] = useState(false);
const [autoRefresh, setAutoRefresh] = useState(true);
const [autoRefreshSeconds, setAutoRefreshSeconds] = useState(10);
const autoRefreshSecondsRef = useRef(autoRefreshSeconds);
const [activePage, setActivePage] = useState(1);
Expand Down Expand Up @@ -125,14 +132,21 @@ const MessagesTable = () => {
showError(reason);
});
checkPermission().then();
const eventSource = new EventSource('/api/message/stream');
eventSource.onerror = (e) => {
showError('服务端消息推送流连接出错!');
};
eventSource.onmessage = (e) => {
let newMessage = JSON.parse(e.data);
insertNewMessage(newMessage);
const connectEventSource = () => {
const eventSource = new EventSource('/api/message/stream');
eventSource.onmessage = (e) => {
const newMessage = JSON.parse(e.data);
insertNewMessage(newMessage);
};
eventSource.onerror = () => {
showError('服务端消息推送流连接出错!即将重试...');
eventSource.close();
setTimeout(connectEventSource, 1000); // 1000ms
};
return eventSource;
};
const eventSource = connectEventSource();
showInfo('服务器消息推送流已连接,您将实时收到新消息');
return () => {
eventSource.close();
};
Expand Down

0 comments on commit 0f39a36

Please sign in to comment.