1.2.1 • Published 2 years ago

@suplink/message v1.2.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

概述

WEB端跨域通信库,支持订阅模式和请求响应模式。

使用指南

示例

订阅模式

import { Message } from '@suplink/message'

const message = new Message({ origin: '*' });

// 发送
message.dispatch({ id: 'test01', data: 'test' });

// 订阅
message.subscribe((e) => {
    console.log(e.data)
});

请求响应模式

// == 客户端请求
import { Client } from '@suplink/message'

const client = new Client({ origin: '*' });

client.request('/path/test', 'request').then((response) => {
    // 显示 request:response
    console.log(response.data);
});

// == 服务端响应
import { Server } from '@suplink/message'

const server = new Server();

server.router('/path/test', (request) => {
    return `${request.data}:response`;
})

API说明

客户端Client

用于跟服务端通信

constructor

参数

参数类型说明默认必须
originstring通信originwindow.location.origin
targetobject目标窗口对象

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });

request

向服务端发送请求

参数

参数类型说明默认必须
pathstring请求路径
dataany请求参数
configobject请求配置,同构造函数参数

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });
client.request('/path', { id: 1 }).then(res => {
    console.log(res, 'res');
});

cancel

取消与服务端通信

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });
client.request('/path', { id: 1 }).then(res => {
    console.log(res, 'res');
});
client.cancel();

resume

恢复与服务端通信

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });
client.request('/path', { id: 1 }).then(res => {
    console.log(res, 'res');
});
client.cancel();
client.resume();

服务端Server

用于接收并处理客户端发送过来的请求

constructor

参数

同客户端

案例

import { Server } from '@suplink/message';

const server = new Server();

router

服务端路由,用于接收客户端特定路径请求并返回响应内容

参数

参数类型说明默认必须
pathstring路由匹配路径,当前为精准匹配
handlerfunction路由匹配后的处理函数,入参为请求体,返回响应体

案例

import { Server } from '@suplink/message';

const server = new Server();

server.router('/path', (req) => {
    console.log(req, 'req');
    const { id } = req.data;
    return { result: `id: ${id}` }
});

cancel

取消与客户端通信

resume

恢复与客户端通信

消息通信Message

基于postMessage的基础通信类,为Client和Server的基类

constructor

参数

参数类型说明默认必须
originstring通信originwindow.location.origin
targetobject目标窗口对象

dispatch

发送消息

参数

参数类型说明默认必须
messageDataMessageData要发送的消息体
configobject发送配置,同构造函数参数

MessageData

属性类型说明默认必须
typestring消息体类型
idstring消息体ID
dataany消息体数据

案例

import { Message } from '@suplink/message';

const message = new Message();

message.dispatch({ type: 'request', id: 'test', data: 'test' });

subscribe

订阅消息

参数

参数类型说明默认必须
listenerfunction监听器

返回

参数类型说明默认必须
listenerIdstring监听器ID,传给unsubscribe用于取消订阅

案例

import { Message } from '@suplink/message';

const message = new Message();

const subscribeId = message.subscribe((e) => {
    console.log(e)
});

unsubscribe

取消订阅

参数

参数类型说明默认必须
listenerIdstring/string[]监听器ID或ID列表

案例

import { Message } from '@suplink/message';

const message = new Message();

const subscribeId = message.subscribe((e) => {
    console.log(e)
});

message.unsubscribe(subscribeId);

init

初始化。注意默认实例化时已初始化

setConfig

动态更改实例配置

connect

建立通信连接

disconnect

断开通信连接

close

关闭通信连接

1.1.1

2 years ago

1.1.0

2 years ago

1.2.1

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago