1.2.1 • Published 2 years ago
ws-event-bus v1.2.1
ws-event-bus
介绍
开发时使用的event bus;利用ws服务器作为中转。
安装教程
npm i ws -D
npm i ws-event-bus -D
使用说明
浏览器端:
html:
<body>
<button id="btn1">btn1</button>
<button id="btn2">btn2</button>
</body>
js(需要打包):
import WSS from 'ws-event-bus'
function getClient(id) {
let ws = new WSS({
id,
url: 'ws://localhost:8080/'
})
ws.on('message', (data) => {
console.log(id + ' get message >>>>', data)
})
ws.on('wait', (data,response) => {
console.log(id + ' wait >>>>', response)
})
return ws
}
let w1 = getClient('w1')
let w2 = getClient('w2')
function click1(){
w1.send('this is w1+', {
to: 'w2'
}).then(res => {
console.log('w1 get reply')
})
}
function click2(){
w2.reply(w2.waiting[0],'value')
}
document.getElementById('btn1').onclick = click1
document.getElementById('btn2').onclick = click2
server:
const WSS = require('ws-event-bus/dist/server.js').default
const path = require('path')
let server = new Server({
port:8080
})
server.static(path.resolve(process.cwd()),'./');//静态资源
server.start()
nodejs:
const WSS = require('ws-event-bus').default
function getClient(id) {
let ws = new WSS({
id,
url: 'ws://localhost:8080/'
})
ws.on('message', (data) => {
console.log(id + ' get message>>>>', data)
})
ws.on('wait', (data, response) => {
console.log(id + ' wait >>>>', response)
})
return ws
}
let w1 = getClient('w1')
let w2 = getClient('w2')
function click1() {
w1.send('this is w1+', {
to: 'w2'
}).then(res => {
console.log('w1 sucewss++++', res)
})
}
function click2() {
w2.reply(w2.waiting[0],'value')
}
click1()
setTimeout(() => {
click2()
}, 3000)
setTimeout(() => {}, 100000)
1.2.0
2 years ago
1.2.1
2 years ago
1.1.23
2 years ago
1.1.22
2 years ago
1.1.21
2 years ago
1.1.20
2 years ago
1.1.19
2 years ago
1.1.18
2 years ago
1.1.17
2 years ago
1.1.16
2 years ago
1.1.15
2 years ago
1.1.14
2 years ago
1.1.13
2 years ago
1.1.12
2 years ago
1.1.11
2 years ago
1.1.10
2 years ago
1.1.9
2 years ago
1.1.8
2 years ago
1.1.7
2 years ago
1.1.6
2 years ago
1.1.5
2 years ago
1.1.4
2 years ago
1.1.3
2 years ago
1.1.2
2 years ago
1.1.1
2 years ago
1.1.0
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago