json-rpc-iframe
v2.0.2
Published
将iframe内外通信的postMessage封装成jsonRpc,
Downloads
291
Maintainers
Readme
json-rpc-iframe
将iframe内外通信的postMessage封装成jsonRpc,
rpc = new JsonRpcIframe(iframeWindow);
// 接收来自 iframe 的消息
rpc.registerMethod("click", (params) => {
const iframeResponse = document.getElementById("iframeResponse");
iframeResponse.textContent = "收到来自 iframe 的消息: " + params.time + ", " + params.message;
});
// 向 iframe 发送消息
function sendMessageToIframe() {
const message = document.getElementById("messageToIframe").value;
rpc.sendRequest("click", { time: Date.now(), message: message });
}
const rpc = new JsonRpcIframe(window.parent);
// 接收来自父页面的消息
rpc.registerMethod("click", (params) => {
const parentMessage = document.getElementById("parentMessage");
parentMessage.textContent = "收到来自父页面的消息: " + params.time + ", " + params.message;
});
// 向父页面发送消息
function sendMessageToParent() {
const message = document.getElementById("messageToParent").value;
rpc.sendRequest("click", { time: Date.now(), message: message });
}
页面结束需要销毁,会reject所有未处理的请求,
rpc.destroy();