From cd8242aecdce355d68c4c22ea298b389fafa2576 Mon Sep 17 00:00:00 2001 From: Zhixing Han Date: Wed, 22 Mar 2023 22:04:57 +0800 Subject: [PATCH] first commit --- .browserslistrc | 3 + .editorconfig | 17 +++++ .eslintrc.js | 14 ++++ .gitignore | 5 ++ .npmrc | 1 + README.md | 186 +++++++++++++++++++++++++++++++++++++++++++++++ babel.config.js | 6 ++ jest.config.js | 6 ++ package.json | 45 ++++++++++++ rollup.config.js | 26 +++++++ src/constants.ts | 2 + src/endpoint.ts | 51 +++++++++++++ src/guest.ts | 51 +++++++++++++ src/iframe.ts | 69 ++++++++++++++++++ src/index.ts | 2 + src/socket.ts | 167 ++++++++++++++++++++++++++++++++++++++++++ src/types.ts | 90 +++++++++++++++++++++++ src/utils.ts | 45 ++++++++++++ tsconfig.json | 8 ++ 19 files changed, 794 insertions(+) create mode 100644 .browserslistrc create mode 100644 .editorconfig create mode 100644 .eslintrc.js create mode 100644 .gitignore create mode 100644 .npmrc create mode 100644 README.md create mode 100644 babel.config.js create mode 100644 jest.config.js create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 src/constants.ts create mode 100644 src/endpoint.ts create mode 100644 src/guest.ts create mode 100644 src/iframe.ts create mode 100644 src/index.ts create mode 100644 src/socket.ts create mode 100644 src/types.ts create mode 100644 src/utils.ts create mode 100644 tsconfig.json diff --git a/.browserslistrc b/.browserslistrc new file mode 100644 index 0000000..0223254 --- /dev/null +++ b/.browserslistrc @@ -0,0 +1,3 @@ +>0.2% +not dead +not op_mini all diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4432917 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# General settings for whole project +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true + +# Matches the exact files either package.json or .travis.yml +[{package.json,.travis.yml}] +indent_size = 2 \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..d5481b1 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,14 @@ +module.exports = { + extends: [ + 'airbnb-base', + 'airbnb-typescript/base', + ], + parserOptions: { + project: './tsconfig.json', + }, + rules: { + 'object-curly-newline': ['error', {'multiline': true }], + '@typescript-eslint/indent': ['error', 4], + '@typescript-eslint/object-curly-spacing': ['error', 'never'], + }, +}; diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2566ae5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.DS_Store + +node_modules/ +dist/ +package-lock.json diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..7549542 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npmmirror.com diff --git a/README.md b/README.md new file mode 100644 index 0000000..34a784f --- /dev/null +++ b/README.md @@ -0,0 +1,186 @@ +# msgio + +If you are familiar with `socket.io`, you already know how to use this package. + +`socket.io` is based on WebSocket. + +`msgio` is based on postMessage. + + +## Install + +### npm + +```js +npm install --save msgio +``` + +### <script> + +```js + + +``` + +## Usage + +### Iframe + +#### In your Main App +```js +import {Iframe} from 'msgio'; + +const iframe = document.getElementById('my-iframe'); + +const onLoad = e => { + const io = new Iframe(e.target); + + io.on('connect', socket => { + // listening some events from the guest + socket.on('my_custom_event', data => { + // data: {x: 1, y: 2} + // any code + }); + + // emit events to the guest + socket.emit('greeting', 'hello world!'); + + // expose functions to the guest + socket.func('my_sync_fn', (a1, a2, ...rest) => { + // a1: 1 + // a2: 2 + // rest: 3, 4, 5 + // any code + // you can throw as usual! + }); + + // you can expose aysnc functions too. + socket.func('my_async_fn', (a1, a2, ...rest) => { + // a1: 'a' + // a2: 'b' + // rest: 'c', 'd', 'e' + return new Promise((resolve, reject) => { + // any code + }); + }); + + // call some functions the guest provides. + socket.call('hi', 'guest', '!') + .then(result => { + // as you call the 'hi' function with args 'guest' and '!' + // the resule may be 'hello host!', who knows! + // any code + }) + .catch(error => { + // the guest may throw error to you + }); + }); +}; + +iframe.addEventListener('load', onLoad); +``` + +#### In your Iframe +```js +import {Guest} from 'msgio'; + +// You must provide the host's origin. It's all. +const io = new Guest({host: 'http://localhost:8080'}); + +// Like in your main app +io.on('connect', socket => { + // the host will use this to set the