Commit d44d19e6 by 张立顺

fix websocket连接bug修复

1 parent 913954e6
...@@ -108,6 +108,7 @@ import { ...@@ -108,6 +108,7 @@ import {
resolveDivisionTestMerchantDivisions, resolveDivisionTestMerchantDivisions,
normalizeDivisionTestPayload normalizeDivisionTestPayload
} from '../utils/resolveDivisionTestOrderCode' } from '../utils/resolveDivisionTestOrderCode'
import { buildWebSocketUrl } from '../utils/buildWebSocketUrl'
import DivisionTestStepper from './divisionTestDialog/DivisionTestStepper.vue' import DivisionTestStepper from './divisionTestDialog/DivisionTestStepper.vue'
import DivisionTestStep1Pay from './divisionTestDialog/DivisionTestStep1Pay.vue' import DivisionTestStep1Pay from './divisionTestDialog/DivisionTestStep1Pay.vue'
import DivisionTestStep2Allocate from './divisionTestDialog/DivisionTestStep2Allocate.vue' import DivisionTestStep2Allocate from './divisionTestDialog/DivisionTestStep2Allocate.vue'
...@@ -386,8 +387,14 @@ export default { ...@@ -386,8 +387,14 @@ export default {
}, },
initWs() { initWs() {
if (!this.wsUrl || this.socket) return if (!this.wsUrl || this.socket) return
this.socket = new WebSocket('ws://' + this.wsUrl) const url = buildWebSocketUrl(this.wsUrl)
this.socket.onmessage = this.getMessage if (!url) return
try {
this.socket = new WebSocket(url)
this.socket.onmessage = this.getMessage
} catch (err) {
console.error('[FinancialCalculationDialog] initWs', err)
}
}, },
teardownWs() { teardownWs() {
if (this.socket) { if (this.socket) {
......
export function buildWebSocketUrl(rawUrl, pageProtocol) {
if (!rawUrl || typeof rawUrl !== "string") return ""
let url = rawUrl.trim()
const secure =
pageProtocol != null
? pageProtocol === "https:"
: typeof window !== "undefined" && window.location.protocol === "https:"
if (/^wss?:\/\//i.test(url)) {
if (secure && /^ws:\/\//i.test(url)) {
return "wss://" + url.slice(5)
}
return url
}
const scheme = secure ? "wss://" : "ws://"
return scheme + url.replace(/^\/\//, "")
}
import { buildWebSocketUrl } from "@/views/systemConfig/merchant/financialRules/utils/buildWebSocketUrl"
describe("buildWebSocketUrl", () => {
it("uses wss on https pages for host-only url", () => {
expect(buildWebSocketUrl("host/path?query", "https:")).toBe(
"wss://host/path?query"
)
})
it("uses ws on http pages for host-only url", () => {
expect(buildWebSocketUrl("host/path", "http:")).toBe("ws://host/path")
})
it("upgrades ws:// to wss:// on https pages", () => {
expect(buildWebSocketUrl("ws://host/path", "https:")).toBe("wss://host/path")
})
it("keeps wss:// on https pages", () => {
expect(buildWebSocketUrl("wss://host/path", "https:")).toBe("wss://host/path")
})
it("returns empty string for invalid input", () => {
expect(buildWebSocketUrl("", "https:")).toBe("")
expect(buildWebSocketUrl(null, "https:")).toBe("")
})
it("strips leading // before adding scheme", () => {
expect(buildWebSocketUrl("//host/path", "http:")).toBe("ws://host/path")
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!