import { getBaseUrl } from "./baseurl.js";
export function getConnectSocket(func) {
return new Promise(resolve => {
wx.showModal({
title: "进来",
content: "进来调接口",
confirmText: "确定",
showCancel: false,
});
let baseURL = getBaseUrl("socket");
const socket = wx.connectSocket({
url: baseURL,
header: {
"content-type": "application/json",
},
success: () => {
console.log("创建连接成功");
wx.showModal({
title: "创建连接成功",
content: "创建连接成功创建连接成功",
confirmText: "确定",
showCancel: false,
});
},
fail: err => {
console.log("创建连接失败:", err);
wx.showModal({
title: "创建连接失败",
content: err,
confirmText: "确定",
showCancel: false,
});
},
});
socket.onOpen(res => {
console.log("✅ WebSocket 已连接", res);
wx.showModal({
title: "提示",
content: "连接成功",
confirmText: "确定",
showCancel: false,
});
});
socket.onMessage(res => {
console.log("✅ WebSocket 已连接 9999onMessage", res);
try {
const data = JSON.parse(res.data);
console.log("✅ WebSocket 已连接 6666onMessage", data);
func(data);
} catch (error) {
func(res.data);
console.log("✅ WebSocket 已连接 6666onMessage", error);
wx.showModal({
title: "提示",
content: "消息解析失败",
confirmText: "确定",
showCancel: false,
});
}
});
socket.onClose(res => {
console.log("✅ WebSocket 已连接 onClose", res);
});
socket.onError(res => {
console.log("✅ WebSocket onError", res);
wx.showModal({
title: "出错",
content: res,
confirmText: "确定",
showCancel: false,
});
});
});
}
以上代码在体验版连接,消息接受都正常,但是发到生产版本不能连接成功,该怎么处理,什么原因
域名配了吗,体验版开调试了?