|
|
@@ -115,28 +115,30 @@ class FrontendState {
|
|
|
}
|
|
|
|
|
|
initWebSocket() {
|
|
|
- const host = window.location.hostname;
|
|
|
- const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
|
- const wsPort = window.location.port ? `:${window.location.port}` : ''; // 适应可能的端口变化
|
|
|
- const wsUrl = `${wsProtocol}//${host}${wsPort}`;
|
|
|
+ const host = window.location.hostname; //設定WebSocket host ip
|
|
|
+ const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; //設定protocol 如果是https 就用wss 反之ws
|
|
|
+ const wsPort = window.location.port ? `:${window.location.port}` : ''; //設定ws用的port
|
|
|
+ const wsUrl = `${wsProtocol}//${host}${wsPort}`; //拼起來就是完整的WebSocket囉
|
|
|
|
|
|
- this.ws = new WebSocket(wsUrl);
|
|
|
+ this.ws = new WebSocket(wsUrl); //設定完畢並開啟服務
|
|
|
|
|
|
- this.ws.onopen = () => {
|
|
|
- console.log('WebSocket connected');
|
|
|
+ this.ws.onopen = () => { //onopen ws服務開啟的時候會先執行的
|
|
|
+ console.log('WebSocket connected'); //提示ws已經連線成功
|
|
|
};
|
|
|
|
|
|
- this.ws.onmessage = (event) => {
|
|
|
- console.log('Message from server ', event.data);
|
|
|
- const data = JSON.parse(event.data); // 解析服务器发送的消息
|
|
|
+ this.ws.onmessage = (event) => { //這邊負責監聽比如從伺服端傳來的訊息
|
|
|
+ console.log('Message from server ', event.data); //log 從server端傳來的訊息
|
|
|
+ const data = JSON.parse(event.data); //json解析 data
|
|
|
|
|
|
- // 检查消息类型
|
|
|
- if (data.type === 'voteUpdate') {
|
|
|
- this.refreshVotesState(true); // 当收到投票更新消息时,刷新投票状态
|
|
|
+ if (data.type === 'voteUpdate') { //如果type為voteUpdate 更新vote結果 ps: 這是自己定義的
|
|
|
+ this.refreshVotesState(true);
|
|
|
+ }
|
|
|
+ if (data.type === 'testMessage') { //如果type為voteUpdate 更新vote結果 ps: 這是自己定義的
|
|
|
+ this.updateWSMessage(data.message);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- this.ws.onerror = (error) => {
|
|
|
+ this.ws.onerror = (error) => { //這邊負責紀錄傳送異常的訊息
|
|
|
console.error('WebSocket error', error);
|
|
|
};
|
|
|
}
|
|
|
@@ -169,7 +171,7 @@ class FrontendState {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async refreshWeatherState(updateView = true) {
|
|
|
+ async refreshWeatherState(updateView = true) {
|
|
|
let {
|
|
|
success,
|
|
|
data,
|
|
|
@@ -186,7 +188,7 @@ class FrontendState {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async refreshSessionState(updateView = true) {
|
|
|
+ async refreshSessionState(updateView = true) {
|
|
|
let {
|
|
|
success,
|
|
|
data,
|
|
|
@@ -220,6 +222,11 @@ class FrontendState {
|
|
|
this.weatherForecasts
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ async updateWSMessage(message) {
|
|
|
+ const wsMessageDiv = document.querySelector(".ws");
|
|
|
+ wsMessageDiv.textContent = message;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const fes = new FrontendState();
|
|
|
@@ -266,6 +273,8 @@ function updateErrorMessage(message) {
|
|
|
errorMessageDiv.textContent = message;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
const authform = document.querySelector("form.authform");
|
|
|
authform.addEventListener("click", handleAuthEvent);
|
|
|
|
|
|
@@ -297,5 +306,5 @@ const daysViewDiv = document.querySelector("section.days-view");
|
|
|
daysViewDiv.addEventListener("click", handleVoteEvent);
|
|
|
|
|
|
document.getElementById('refreshVotes').addEventListener('click', function () {
|
|
|
- fes.refreshVotesState(true);
|
|
|
+ fes.refreshVotesState(true);// 重新獲取並顯示最新的投票資訊 並更新VIEW
|
|
|
});
|