UNPKG

w-comor-mqtt

Version:

A websocket communicator in nodejs and browser. Mapping functions from nodejs to other end points, like a simple RPC.

901 lines (271 loc) 17.4 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Global - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> <script src="scripts/nav.js" defer></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav > <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#WComorMqttClient">WComorMqttClient</a></li><li><a href="global.html#WComorMqttServer">WComorMqttServer</a></li></ul> </nav> <div id="main"> <h1 class="page-title">Global</h1> <section> <header> <h2> </h2> </header> <article> <div class="container-overview"> <dl class="details"> </dl> </div> <h3 class="subsection-title">Methods</h3> <h4 class="name" id="WComorMqttClient"><span class="type-signature"></span>WComorMqttClient<span class="signature">(opt)</span><span class="type-signature"> &rarr; {Promise}</span></h4> <dl class="details"> <dt class="tag-description">Description:</dt> <dd class="tag-description"><ul class="dummy"><li><p>建立MQTT使用者(Node.js與Browser)端物件</p></li></ul></dd> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="WComorMqttClient.mjs.html">WComorMqttClient.mjs</a>, <a href="WComorMqttClient.mjs.html#line98">line 98</a> </li></ul></dd> </dl> <h5 class="h5-examples">Example</h5> <pre class="prettyprint"><code>import WComorMqttClient from 'w-comor-mqtt/dist/w-comor-mqtt-client.umd.js' //opt let opt = { url: 'mqtt://localhost:8080', token: '*', open: function() { console.log('client: open') }, close: function() { console.log('client: close') }, error: function(err) { console.log('client: error:', err) }, reconn: function() { console.log('client: reconn') }, } //WComorMqttClient new WComorMqttClient(opt) .then(function(wo) { console.log('client: funcs: ', wo) function core(ps) { wo.group.plus(ps) .then(function(r) { console.log('client: plus(' + JSON.stringify(ps) + ')=' + r) }) .catch(function(err) { console.log('client: plus: catch: ', err) }) wo.group.div(ps) .then(function(r) { console.log('client: div(' + JSON.stringify(ps) + ')=' + r) }) .catch(function(err) { console.log('client: div: catch: ', err) }) wo.add(ps) .then(function(r) { console.log(`client: add(${JSON.stringify(ps)})=${r}`) }) .catch(function(err) { console.log('client: add: catch: ', err) }) wo.minu(ps) .then(function(r) { console.log(`client: minu(${JSON.stringify(ps)})=${r}`) }) .catch(function(err) { console.log('client: minu: catch: ', err) }) } let i = 100 setInterval(function() { i += 1 core({ p1: i, p2: 10, }) }, 1000) }) .catch(function(err) { console.log('client: catch', err) })</code></pre> <h5 class="h5-parameters">Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>opt</code></td> <td class="type"> <span class="param-type">Object</span> </td> <td class="description last"><p>輸入設定參數物件</p> <h6>Properties</h6> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th>Default</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>url</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> <code>'mqtt://localhost:8080'</code> </td> <td class="description last"><p>輸入MQTT伺服器網址,預設為'mqtt://localhost:8080',若由瀏覽器Browser連線,則需連MQTT WebSocket伺服器</p></td> </tr> <tr> <td class="name"><code>token</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> <code>'*'</code> </td> <td class="description last"><p>輸入使用者認證用token,預設為'*'</p></td> </tr> <tr> <td class="name"><code>open</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="attributes"> </td> <td class="default"> </td> <td class="description last"><p>輸入監聽open函數</p></td> </tr> <tr> <td class="name"><code>close</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="attributes"> </td> <td class="default"> </td> <td class="description last"><p>輸入監聽close函數</p></td> </tr> <tr> <td class="name"><code>error</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="attributes"> </td> <td class="default"> </td> <td class="description last"><p>輸入監聽error函數</p></td> </tr> <tr> <td class="name"><code>reconn</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="attributes"> </td> <td class="default"> </td> <td class="description last"><p>輸入監聽reconn函數</p></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <h5 class="h5-returns">Returns:</h5> <div class="param-desc"> <p>回傳Promise,resolve為映射伺服器端可用函數之物件,各函數輸入皆為單一物件,各函數回傳皆為Promise,用resolve與reject處理回傳結果</p> </div> <dl class="param-type"> <dt> Type </dt> <dd> <span class="param-type">Promise</span> </dd> </dl> <h4 class="name" id="WComorMqttServer"><span class="type-signature"></span>WComorMqttServer<span class="signature">(opt)</span><span class="type-signature"></span></h4> <dl class="details"> <dt class="tag-description">Description:</dt> <dd class="tag-description"><ul class="dummy"><li><p>建立MQTT伺服器</p></li></ul></dd> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="WComorMqttServer.mjs.html">WComorMqttServer.mjs</a>, <a href="WComorMqttServer.mjs.html#line96">line 96</a> </li></ul></dd> </dl> <h5 class="h5-examples">Example</h5> <pre class="prettyprint"><code>import WComorMqttServer from 'w-comor-mqtt/dist/w-comor-mqtt-server.umd.js' function random(min, max) { return Math.floor(Math.random() * max) + min } let opt = { port: 8080, //for mqtt nodejs server portWeb: 8090, //for mqtt web server authenticate: function(token) { //使用token驗證使用者身份 return new Promise(function(resolve, reject) { setTimeout(function() { resolve(true) }, 1000) }) }, filterFuncs: function(token, funcs) { //使用token驗證使用者身份與過濾可用funcs return new Promise(function(resolve, reject) { funcs = funcs.filter(function(v) { return v.indexOf('Hide') &lt; 0 }) resolve(funcs) }) }, onClientChange: function(clients, opt) { console.log(`Server[port:${opt.port}][port for web:${opt.portWeb}] now clients: ${clients.length}`) }, funcs: { 'group.plus': function({ p1, p2 }) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(p1 * p2) }, random(100, 3000)) }) }, 'group.div': function({ p1, p2 }) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(p1 / p2) }, random(100, 3000)) }) }, 'add': function({ p1, p2 }) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(p1 + p2) }, random(100, 3000)) }) }, 'addHide': function({ p1, p2 }) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(p1 + p2) }, random(100, 3000)) }) }, 'minu': function({ p1, p2 }) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(p1 - p2) }, random(100, 3000)) }) }, }, } new WComorMqttServer(opt)</code></pre> <h5 class="h5-parameters">Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>opt</code></td> <td class="type"> <span class="param-type">Object</span> </td> <td class="description last"><p>輸入設定參數物件</p> <h6>Properties</h6> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th>Default</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>port</code></td> <td class="type"> <span class="param-type">Integer</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> <code>8080</code> </td> <td class="description last"><p>輸入MQTT提供給nodejs服務的port,預設8080</p></td> </tr> <tr> <td class="name"><code>portWeb</code></td> <td class="type"> <span class="param-type">Integer</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> <code>opt.port+10</code> </td> <td class="description last"><p>輸入MQTT提供給web服務的port,預設為opt.port+10,也就是8090</p></td> </tr> <tr> <td class="name"><code>authenticate</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="attributes"> </td> <td class="default"> </td> <td class="description last"><p>輸入使用者身份認證函數,供伺服器端驗證之用,函數會傳入使用者端連線之token參數,回傳為Promise,resolve(true)為驗證通過,resolve(false)為驗證不通過</p></td> </tr> <tr> <td class="name"><code>funcs</code></td> <td class="type"> <span class="param-type">Object</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> <code>{}</code> </td> <td class="description last"><p>輸入伺服器端供使用者端呼叫之函數物件,各key為函數名稱,對應value為函數本體。各函數之輸入需為單一物件,而各函數回傳皆為Promise,可通過resolve與reject回傳結果,預設{}</p></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 24 2024 22:20:17 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/polyfill.js"></script> <script src="scripts/linenumber.js"></script> </body> </html>