dp-open-websocket-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

dp-open-websocket-client

安装

npm install dp-open-websocket-client --save

使用

const DpWebSocketClient = require('dp-open-websocket-client');
 
const wsClient = new DpWebSocketClient({
  appKey: your_app_key,
  appSecret: 'your_app_secret',
  server: 'ws://openapi.dianping.com/message/websocket',
  type: 'ALL',
  version: '1.0.0',
  onMessage: function (msg) {
    // msg: Object,结构见下方
    console.log('received:', msg);
    // 返回 true,表示已经收到该消息
    return true;
  },
  onClose: function () {
    console.log('closed');
  },
  onError: function (error) {
    // error: 具体的错误堆栈信息
    console.log('error:', error);
  }
});
 
// 获取ws实例,具体实例参考 https://github.com/websockets/ws
const wsInstance = wsClient.getWsInstance();

说明

收到的消息体结构

export interface Msg {
    /** 
     * 消息id 
     */
    msg_id: string;
    /** 
     * 应用的 AppKey 
     */
    app_key: string;
    /** 
     * 消息事件的类型
     */
    type: string;
    /**
     * 消息内容,包含具体业务数据。相应结构请查看下消息类目->消息说明文档
     */
    msg: string;
    /**
     * 第三方商户id
     */
    app_shop_id: string;
    /**
     * 消息事件时间
     */
    version: number;
    /**
     * 重试次数
     */
    retry: number;
    /**
     * 是否为心跳消息
     */
    heartbeat: number;
    /**
     * 消息签名
     */
    sign: string;
}
 

Readme

Keywords

Package Sidebar

Install

npm i dp-open-websocket-client

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

7.93 kB

Total Files

6

Last publish

Collaborators

  • murmur
  • zoucong