使用方法
import createSocket from "./socket";
createSocket({
key: `1_${orderNumber}`, //key值
host: __path+'/', //访问网址
onmessage(data) {
console.log("push", data); //返回数据
}
});
js插件
import ReconnectingWebSocket from "reconnecting-websocket";
const createSocket = function({ key, host, onmessage }) {
try {
const path = host.replace("http:", "");
const webSocketPath = "ws:" + path + "websocket";
let webSocket = new ReconnectingWebSocket(webSocketPath);
webSocket.debug = true;
webSocket.timeoutInterval = 5400;
window.onbeforeunload = function() {
webSocket.close();
};
webSocket.onopen = function() {
webSocket.send(key);
};
webSocket.onmessage = event => {
const push = JSON.parse(event.data);
if (process.env.NODE_ENV !== "production") {
console.log("push >>", push);
}
onmessage(push);
};
} catch (e) {
//not support webSocket
}
};
export default createSocket;