2025-07-09 10:40:56 +08:00
|
|
|
|
export const namespaced = true
|
|
|
|
|
|
|
|
|
|
|
|
export const state = () => ({
|
|
|
|
|
|
socket: null,
|
|
|
|
|
|
isConnected: false,
|
|
|
|
|
|
messages: [],
|
|
|
|
|
|
notices: [],
|
|
|
|
|
|
count: "0"
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
export const mutations = {
|
2025-08-28 18:02:01 +08:00
|
|
|
|
WEBSOCKET_INIT(state, url) {
|
2025-07-09 10:40:56 +08:00
|
|
|
|
state.socket = new WebSocket(url);
|
2025-08-28 18:02:01 +08:00
|
|
|
|
state.socket.onopen = function() {
|
2025-07-09 10:40:56 +08:00
|
|
|
|
console.log("WebSocket连接成功");
|
|
|
|
|
|
};
|
2025-08-28 18:02:01 +08:00
|
|
|
|
// 注意:这里不再直接处理 onmessage,而是在 action 中处理
|
|
|
|
|
|
state.socket.onerror = function() {
|
2025-07-09 10:40:56 +08:00
|
|
|
|
console.log("WebSocket连接发生错误");
|
|
|
|
|
|
};
|
2025-08-28 18:02:01 +08:00
|
|
|
|
state.socket.onclose = function(e) {
|
2025-07-09 10:40:56 +08:00
|
|
|
|
console.log("connection closed (" + e.code + ")");
|
|
|
|
|
|
};
|
|
|
|
|
|
},
|
2025-08-28 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
SET_SOCKET_ONMESSAGE(state, handler) {
|
|
|
|
|
|
// 设置 WebSocket 的消息处理函数
|
|
|
|
|
|
if (state.socket) {
|
|
|
|
|
|
state.socket.onmessage = handler;
|
|
|
|
|
|
}
|
2025-07-09 10:40:56 +08:00
|
|
|
|
},
|
2025-08-28 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
WEBSOCKET_SEND(state, msg) {
|
|
|
|
|
|
if (state.socket) {
|
|
|
|
|
|
state.socket.send(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
WEBSOCKET_CLOSE(state) {
|
2025-07-29 23:28:46 +08:00
|
|
|
|
if (state.socket) {
|
|
|
|
|
|
state.socket.close();
|
|
|
|
|
|
state.socket = null;
|
|
|
|
|
|
}
|
2025-08-04 17:43:57 +08:00
|
|
|
|
},
|
2025-08-28 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
SET_MESSAGE(state, msg) {
|
|
|
|
|
|
state.messages = msg;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
ADD_MESSAGE(state, message) {
|
|
|
|
|
|
// 使用展开运算符创建新数组而不是直接 push
|
|
|
|
|
|
state.messages = [...state.messages, message];
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
ADD_NOTICE(state, notice) {
|
|
|
|
|
|
// 使用展开运算符创建新数组而不是直接 push
|
|
|
|
|
|
state.notices = [...state.notices, notice];
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
SET_COUNT(state, count) {
|
|
|
|
|
|
state.count = count;
|
2025-07-09 10:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const actions = {
|
2025-08-28 18:02:01 +08:00
|
|
|
|
websocket_init({ commit, state }, url) {
|
|
|
|
|
|
commit('WEBSOCKET_INIT', url);
|
|
|
|
|
|
|
|
|
|
|
|
// 在 action 中设置 WebSocket 的 onmessage 回调
|
|
|
|
|
|
if (state.socket) {
|
|
|
|
|
|
state.socket.onmessage = function(e) {
|
|
|
|
|
|
console.log(e, '接收到的消息');
|
|
|
|
|
|
if (e.data.startsWith("C")) {
|
|
|
|
|
|
commit('SET_COUNT', e.data);
|
|
|
|
|
|
} else if (e.data.startsWith("系统通知")) {
|
|
|
|
|
|
commit('ADD_NOTICE', e.data);
|
|
|
|
|
|
} else if (e.data.startsWith("close")) {
|
|
|
|
|
|
console.log(e.data);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
commit('ADD_MESSAGE', JSON.parse(e.data));
|
|
|
|
|
|
console.log(state.messages);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2025-07-09 10:40:56 +08:00
|
|
|
|
},
|
2025-08-28 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
websocket_send({ commit }, msg) {
|
|
|
|
|
|
commit('WEBSOCKET_SEND', msg);
|
2025-07-09 10:40:56 +08:00
|
|
|
|
},
|
2025-08-28 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
websocket_close({ commit }) {
|
|
|
|
|
|
commit('WEBSOCKET_CLOSE');
|
2025-08-04 17:43:57 +08:00
|
|
|
|
},
|
2025-08-28 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
set_message({ commit }, msg) {
|
|
|
|
|
|
commit('SET_MESSAGE', msg);
|
2025-07-09 10:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|