w-comor-mqtt
Version:
A websocket communicator in nodejs and browser. Mapping functions from nodejs to other end points, like a simple RPC.
100 lines (83 loc) • 3.69 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-tw">
<head>
<meta http-equiv="content-type" content="text/html charset=UTF-8" />
<title>MQTT</title>
<script nomodule src="https://cdn.jsdelivr.net/npm/@babel/polyfill/dist/polyfill.min.js"></script>
<script src="https://unpkg.com/mqtt@5.5.0/dist/mqtt.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/wsemi/dist/wsemi.umd.js"></script>
<script src="dist/w-comor-mqtt-client.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/w-consolelog/dist/w-consolelog.umd.js"></script>
<script>
console.log = window['w-consolelog']
</script>
<script>
//opt
let opt = {
url: 'mqtt://localhost:8090', //for mqtt web server
token: '*',
open: function() {
console.log('client web: open')
},
close: function() {
console.log('client web: close')
},
error: function(err) {
console.log('client web: error:', err)
},
reconn: function() {
console.log('client web: reconn')
},
}
//WComorMqttClient
let WComorMqttClient = window['w-comor-mqtt-client']
new WComorMqttClient(opt)
.then(function(wo) {
console.log('client web: funcs: ', wo)
function core(ps) {
wo.group.plus(ps)
.then(function(r) {
console.log('client web: plus(' + JSON.stringify(ps) + ') = ', r)
})
.catch(function(err) {
console.log('client web: plus: catch: ', err)
})
wo.group.div(ps)
.then(function(r) {
console.log('client web: div(' + JSON.stringify(ps) + ') = ', r)
})
.catch(function(err) {
console.log('client web: div: catch: ', err)
})
wo.add(ps)
.then(function(r) {
console.log('client web: add(' + JSON.stringify(ps) + ') = ', r)
})
.catch(function(err) {
console.log('client web: add: catch: ', err)
})
wo.minu(ps)
.then(function(r) {
console.log('client web: minu(' + JSON.stringify(ps) + ') = ', r)
})
.catch(function(err) {
console.log('client web: minu: catch: ', err)
})
}
let i = 100
setInterval(function() {
i += 1
core({
p1: i,
p2: 10,
})
}, 1000)
})
.catch(function(err) {
console.log('client web: catch: ', err)
})
</script>
</head>
<body>
</body>
</html>