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
3 years ago
1.1.22
3 years ago
1.1.21
3 years ago
1.1.20
3 years ago
1.1.19
3 years ago
1.1.18
3 years ago
1.1.17
3 years ago
1.1.16
3 years ago
1.1.15
3 years ago
1.1.14
3 years ago
1.1.13
3 years ago
1.1.12
3 years ago
1.1.11
3 years ago
1.1.10
3 years ago
1.1.9
3 years ago
1.1.8
3 years ago
1.1.7
3 years ago
1.1.6
3 years ago
1.1.5
3 years ago
1.1.4
3 years ago
1.1.3
3 years ago
1.1.2
3 years ago
1.1.1
3 years ago
1.1.0
3 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago