📘
Edge Matrix Documentation
中文
中文
  • 🌐EMC Network
    • EMC 概述
    • 核心产品
      • 公链
        • 链网络信息
        • 共识机制 PoAW
        • 区块参数
        • 交易Performance
        • JSON RPC
        • 链桥
          • 链桥合约
          • 链桥工作机制
          • 支持的跨链资产对
        • Dex
      • EMC Hub
        • EMC Protocol
        • 工作方式及拓扑图
    • EMC DAO
      • DAO 架构:去中心化治理原则与流程
      • 提案与投票机制:代币持有者如何参与决策
      • 社区基金运作:资源分配与社区激励规则
    • 代币经济模型
      • 代币分配
      • 代币用途
      • 产品收入模式
    • 未来规划与发展
    • 常见问题 (FAQ)
Powered by GitBook
On this page
  • Packages
  • 基础用例
  • CommonJS and ESmodules
  1. Openverse

EMC JS SDK

这是 ethereumjs-monorepo 项目说明。

Packages

package
npm
keywords

utility create private key

rlp encode keccak256 sign

websocket

http

create subject scribe subject send message

edgematrixjs for browser

基础用例

CommonJS and ESmodules

npm install --save @edgematrixjs/rtc

Create Subject

import { Http } from '@edgematrixjs/http';
import { RTC } from '@edgematrixjs/rtc';
const chatId = 2;
const privateKey = '';
const httpsUrl = 'https://oregon.edgematrix.xyz';
const emHttp = new Http({ baseURL: httpsUrl });
const rtc = new RTC();
const { _result, hash } = await rtc.createSubject(chainId, privateKey, emHttp);
//_result === 0 is success, the 'hash' is subject

Subscribe Subject

import { EmSocket } from '@edgematrixjs/socket';
import { RTC } from '@edgematrixjs/rtc';
//connect method implement follow in test/index.spec.ts
const wssUrl = 'wss://oregon.edgematrix.xyz/edge_ws';
const handleAction = ({ action, event }) => {};
const { _result: _socketResult, emSocket, event } = await connect({ network: wssUrl, callback: handleAction });
if (_socketResult !== 0) {
  throw new Error('socket is error');
}
const rtc = new RTC();
const chatId = 2;
const application = 'edge_chat';
const subject = 'Your Subject';
const content = 'Your Content';
const params = { subject, application, content, chainId };
const { _result } = await rtc.subscribe(params, privateKey, emSocket);
//_result === 0 is success

Send Message

import { Http } from '@edgematrixjs/http';
import { RTC } from '@edgematrixjs/rtc';
const httpsUrl = 'https://oregon.edgematrix.xyz';
const emHttp = new Http({ baseURL: httpsUrl });
const rtc = new RTC();
const params = {
  subject: globalSubject,
  application: 'edge_chat',
  content: JSON.stringify({ data: 'test send message' }),
  //When the "To" parameter is empty, everyone who subscribed to the subject will receive your message
  //to?: {TargetPublicKey},
  chainId,
};
const { _result } = await rtc.sendMessage(params, privateKey, emHttp);
//_result === 0 is success

Browser

<script src="packages/client/dist/bundle.js"></script>
<script>
  const privateKey = '0xb22be9c19b61adc1d8e89a1dae0346ed274ac9fa239c06286910c29f9fee59d3';
  const subject = '0x8eeb338239ada22d81ffb7adc995fe31a4d1dc2d701bc8a58fffe5b53e14281e';
  const httpsUrl = 'https://oregon.edgematrix.xyz';
  const wssUrl = 'wss://oregon.edgematrix.xyz/edge_ws';

  window.onload = function () {
    const Http = edgematrixjs.Http;
    const RTC = edgematrixjs.RTC;
    const ws = new edgematrixjs.EmSocket({ url: wssUrl });
    //set open listener
    ws.setOpenListener((event) => {
      const rtc = new RTC();
      const subscribe = { subject: subject, application: 'edge_chat', content: 'subject', chainId: 2 };

      rtc.subscribe(subscribe, privateKey, ws).then((resp) => {
        //send message
        const http = new Http({ baseURL: httpsUrl });
        const messageContent = { data: 'hello' };
        const message = {
          subject: subject,
          application: 'edge_chat',
          chainId: 2,
          content: JSON.stringify(messageContent),
        };
        rtc.sendMessage(message, privateKey, http);
      });
    });

    //set message listener
    ws.addMessageListener((event) => {
      const data = JSON.parse(event.data) || {};
      const params = data.params || {};
      const result = params.result || {};
      const from = result.From;
      const type = result.Type;
      let content = { data: '' };
      try {
        content = JSON.parse(result.Content);
      } catch (e) {}
      console.info(`recerved message `, content);
    });
    //connect socket
    ws.connect();
  };
</script>

Last updated 1 year ago

👐
@edgematrixjs/util
npm package
@edgematrixjs/tx
npm package
@edgematrixjs/socket
npm package
@edgematrixjs/http
npm package
@edgematrixjs/rtc
npm package
@edgematrixjs/client
npm package