|
|
@ -46,7 +46,7 @@ |
|
|
</template> |
|
|
</template> |
|
|
|
|
|
|
|
|
<script setup> |
|
|
<script setup> |
|
|
import { ref, onMounted, watch, nextTick, computed } from "vue"; |
|
|
|
|
|
|
|
|
import { ref, onMounted, onUnmounted, watch, nextTick, computed } from "vue"; |
|
|
import util from "../../common/util.js"; |
|
|
import util from "../../common/util.js"; |
|
|
import IndexCard from "../../components/IndexCard.vue"; |
|
|
import IndexCard from "../../components/IndexCard.vue"; |
|
|
import { useMarketSituationStore } from "../../stores/modules/marketSituation.js"; |
|
|
import { useMarketSituationStore } from "../../stores/modules/marketSituation.js"; |
|
|
@ -168,8 +168,274 @@ const getGlobalIndex = async () => { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// TCP相关响应式变量 |
|
|
|
|
|
import tcpConnection, { TCPConnection, TCP_CONFIG } from "@/api/tcpConnection.js"; |
|
|
|
|
|
const tcpConnected = ref(false); |
|
|
|
|
|
const connectionListener = ref(null); |
|
|
|
|
|
const messageListener = ref(null); |
|
|
|
|
|
// 初始化TCP监听器 |
|
|
|
|
|
const initTcpListeners = () => { |
|
|
|
|
|
// 创建连接状态监听器并保存引用 |
|
|
|
|
|
connectionListener.value = (status, result) => { |
|
|
|
|
|
tcpConnected.value = status === "connected"; |
|
|
|
|
|
console.log("TCP连接状态变化:", status, tcpConnected.value); |
|
|
|
|
|
|
|
|
|
|
|
// 显示连接状态提示 |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: status === "connected" ? "TCP连接成功" : "TCP连接断开", |
|
|
|
|
|
icon: status === "connected" ? "success" : "none", |
|
|
|
|
|
duration: 2000, |
|
|
|
|
|
}); |
|
|
|
|
|
// 如果连接,发送获取批量数据 |
|
|
|
|
|
if (status === "connected") { |
|
|
|
|
|
if (klineTab.value == 1) { |
|
|
|
|
|
sendTcpMessage("batch_real_time"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 创建消息监听器并保存引用 |
|
|
|
|
|
messageListener.value = (type, message, parsedArray) => { |
|
|
|
|
|
const messageObj = { |
|
|
|
|
|
type: type, |
|
|
|
|
|
content: message, |
|
|
|
|
|
parsedArray: parsedArray, |
|
|
|
|
|
timestamp: new Date().toLocaleTimeString(), |
|
|
|
|
|
direction: "received", |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 解析股票数据 |
|
|
|
|
|
parseStockData(message); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 注册监听器 |
|
|
|
|
|
tcpConnection.onConnectionChange(connectionListener.value); |
|
|
|
|
|
tcpConnection.onMessage(messageListener.value); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 连接TCP服务器 |
|
|
|
|
|
const connectTcp = () => { |
|
|
|
|
|
console.log("开始连接TCP服务器..."); |
|
|
|
|
|
tcpConnection.connect(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 断开TCP连接 |
|
|
|
|
|
const disconnectTcp = () => { |
|
|
|
|
|
console.log("断开TCP连接..."); |
|
|
|
|
|
tcpConnection.disconnect(); |
|
|
|
|
|
tcpConnected.value = false; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 发送TCP消息 |
|
|
|
|
|
const sendTcpMessage = (command) => { |
|
|
|
|
|
let messageData; |
|
|
|
|
|
let messageDataArray = []; |
|
|
|
|
|
if (command == "batch_real_time") { |
|
|
|
|
|
messageDataArray = globalIndexArray.value.map((item) => item.stockCode); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (command) { |
|
|
|
|
|
// 实时行情推送 |
|
|
|
|
|
case "real_time": |
|
|
|
|
|
messageData = { |
|
|
|
|
|
command: "real_time", |
|
|
|
|
|
stock_code: "SH.000001", |
|
|
|
|
|
}; |
|
|
|
|
|
break; |
|
|
|
|
|
// 初始化获取行情历史数据 |
|
|
|
|
|
case "init_real_time": |
|
|
|
|
|
messageData = { |
|
|
|
|
|
command: "init_real_time", |
|
|
|
|
|
stock_code: "SH.000001", |
|
|
|
|
|
}; |
|
|
|
|
|
break; |
|
|
|
|
|
case "stop_real_time": |
|
|
|
|
|
messageData = { |
|
|
|
|
|
command: "stop_real_time", |
|
|
|
|
|
}; |
|
|
|
|
|
break; |
|
|
|
|
|
// 股票列表 |
|
|
|
|
|
case "stock_list": |
|
|
|
|
|
messageData = { |
|
|
|
|
|
command: "stock_list", |
|
|
|
|
|
}; |
|
|
|
|
|
break; |
|
|
|
|
|
case "batch_real_time": |
|
|
|
|
|
messageData = { |
|
|
|
|
|
command: "batch_real_time", |
|
|
|
|
|
stock_codes: ["SH.000001", "SH.000002"], |
|
|
|
|
|
}; |
|
|
|
|
|
break; |
|
|
|
|
|
case "help": |
|
|
|
|
|
messageData = { |
|
|
|
|
|
command: "help", |
|
|
|
|
|
}; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
if (!messageData) { |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: "命令不存在", |
|
|
|
|
|
icon: "none", |
|
|
|
|
|
duration: 1000, |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} else { |
|
|
|
|
|
try { |
|
|
|
|
|
// 发送消息 |
|
|
|
|
|
const success = tcpConnection.send(messageData); |
|
|
|
|
|
if (success) { |
|
|
|
|
|
console.log("home发送TCP消息:", messageData); |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: "消息发送成功", |
|
|
|
|
|
icon: "success", |
|
|
|
|
|
duration: 1000, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error("发送TCP消息时出错:", error); |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: "消息发送失败", |
|
|
|
|
|
icon: "none", |
|
|
|
|
|
duration: 1000, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 获取TCP连接状态 |
|
|
|
|
|
const getTcpStatus = () => { |
|
|
|
|
|
const status = tcpConnection.getConnectionStatus(); |
|
|
|
|
|
uni.showModal({ |
|
|
|
|
|
title: "TCP连接状态", |
|
|
|
|
|
content: `当前状态: ${status ? "已连接" : "未连接"}`, |
|
|
|
|
|
showCancel: false, |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
let isMorePacket = { |
|
|
|
|
|
batch_real_time: false, |
|
|
|
|
|
}; |
|
|
|
|
|
let receivedMessage; |
|
|
|
|
|
const findJsonPacket = (message, command) => { |
|
|
|
|
|
let jsonStartIndex = 0; |
|
|
|
|
|
let jsonEndIndex = message.indexOf(command); |
|
|
|
|
|
let jsonStartCount = 0; |
|
|
|
|
|
let jsonEndCount = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < message.length - 1; ++i) { |
|
|
|
|
|
if (message[i] == "{") { |
|
|
|
|
|
jsonStartCount++; |
|
|
|
|
|
if (jsonStartCount == 2) { |
|
|
|
|
|
jsonStartIndex = i; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
for (let i = message.indexOf(command); i >= 0; --i) { |
|
|
|
|
|
if (message[i] == "}" || i == jsonStartIndex) { |
|
|
|
|
|
jsonEndCount++; |
|
|
|
|
|
if (jsonEndCount == 1) { |
|
|
|
|
|
jsonEndIndex = i; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查JSON字符串是否有效 |
|
|
|
|
|
if (jsonStartIndex >= jsonEndIndex) { |
|
|
|
|
|
return { error: true }; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { json: JSON.parse(message.substring(jsonStartIndex, jsonEndIndex + 1)) }; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 解析TCP股票数据 |
|
|
|
|
|
const parseStockData = (message) => { |
|
|
|
|
|
try { |
|
|
|
|
|
console.log("进入parseStockData, message类型:", typeof message); |
|
|
|
|
|
|
|
|
|
|
|
let parsedMessage; |
|
|
|
|
|
// 如果isMorePacket是true,说明正在接受分包数据,无条件接收 |
|
|
|
|
|
// 如果message是字符串且以{开头,说明是JSON字符串,需要解析 |
|
|
|
|
|
// 如果不属于以上两种情况,说明是普通字符串,不预解析 |
|
|
|
|
|
if (message.includes("欢迎连接到股票数据服务器")) { |
|
|
|
|
|
console.log("服务器命令列表,不予处理"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if (message.includes("real_time")) { |
|
|
|
|
|
let startIndex = 0; |
|
|
|
|
|
let endIndex = message.length; |
|
|
|
|
|
for (let i = 0; i < message.length - 1; ++i) { |
|
|
|
|
|
if (message[i] == "{") { |
|
|
|
|
|
startIndex = i; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
for (let i = message.length - 1; i >= 0; --i) { |
|
|
|
|
|
if (message[i] == "}") { |
|
|
|
|
|
endIndex = i; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
parsedMessage = JSON.parse(message.substring(startIndex, endIndex + 1)); |
|
|
|
|
|
console.log("实时数据解析", parsedMessage); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
}else { |
|
|
|
|
|
// 没有通过JSON解析判断,说明不是需要的数据 |
|
|
|
|
|
console.log("不是需要的数据,不做处理"); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error("解析TCP股票数据失败:", error.message); |
|
|
|
|
|
console.error("错误详情:", error); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 移除TCP监听器 |
|
|
|
|
|
const removeTcpListeners = () => { |
|
|
|
|
|
if (connectionListener.value) { |
|
|
|
|
|
tcpConnection.removeConnectionListener(connectionListener.value); |
|
|
|
|
|
connectionListener.value = null; |
|
|
|
|
|
console.log("已移除TCP连接状态监听器"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (messageListener.value) { |
|
|
|
|
|
tcpConnection.removeMessageListener(messageListener.value); |
|
|
|
|
|
messageListener.value = null; |
|
|
|
|
|
console.log("已移除TCP消息监听器"); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const startTcp = () => { |
|
|
|
|
|
try { |
|
|
|
|
|
removeTcpListeners(); |
|
|
|
|
|
disconnectTcp(); |
|
|
|
|
|
initTcpListeners(); |
|
|
|
|
|
connectTcp(); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error("建立连接并设置监听出错:", error); |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: "建立连接并设置监听", |
|
|
|
|
|
icon: "none", |
|
|
|
|
|
duration: 1500, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => { |
|
|
|
|
|
sendTcpMessage("stop_real_time"); |
|
|
|
|
|
removeTcpListeners(); |
|
|
|
|
|
disconnectTcp(); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
onMounted(async () => { |
|
|
onMounted(async () => { |
|
|
await getGlobalIndex(); |
|
|
await getGlobalIndex(); |
|
|
|
|
|
initTcpListeners(); |
|
|
|
|
|
await nextTick(); |
|
|
|
|
|
// 开始连接 |
|
|
|
|
|
startTcp(); |
|
|
// 状态栏高度 |
|
|
// 状态栏高度 |
|
|
iSMT.value = uni.getSystemInfoSync().statusBarHeight; |
|
|
iSMT.value = uni.getSystemInfoSync().statusBarHeight; |
|
|
|
|
|
|
|
|
|