Publish/Discover
This commit is contained in:
+54
-2
@@ -2,9 +2,13 @@ import {
|
|||||||
Container,
|
Container,
|
||||||
AuthenticationRequest,
|
AuthenticationRequest,
|
||||||
AuthenticationResponse,
|
AuthenticationResponse,
|
||||||
Message
|
Message,
|
||||||
|
PublishRequest,
|
||||||
|
PublishResponse,
|
||||||
|
DiscoverRequest,
|
||||||
|
DiscoverResponse
|
||||||
} from './Protocol.js'
|
} from './Protocol.js'
|
||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
|
|
||||||
const WebSocket = (typeof window === 'undefined' ? import('ws'): window.WebSocket)
|
const WebSocket = (typeof window === 'undefined' ? import('ws'): window.WebSocket)
|
||||||
|
|
||||||
@@ -94,6 +98,49 @@ export class Client extends EventEmitter {
|
|||||||
return this.#address
|
return this.#address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #publish(port, flags) {
|
||||||
|
const response = await this.#protoRequest(PublishRequest.create({
|
||||||
|
port,
|
||||||
|
flags,
|
||||||
|
publish: true
|
||||||
|
}))
|
||||||
|
|
||||||
|
const publish = PublishResponse.toObject(response.PublishResponse)
|
||||||
|
|
||||||
|
if(publish.status !== 0) {
|
||||||
|
throw new Error(`Unexpected response: ${publish.status}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async #unpublish(port) {
|
||||||
|
const response = await this.#protoRequest(PublishRequest.create({
|
||||||
|
port,
|
||||||
|
flags: [],
|
||||||
|
publish: false
|
||||||
|
}))
|
||||||
|
|
||||||
|
const unpublish = PublishResponse.toObject(response.PublishRespons)
|
||||||
|
|
||||||
|
if(unpublish.status !== 0) {
|
||||||
|
throw new Error(`Unexpected response: ${unpublish.status}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async discover(flags, limit = 0) {
|
||||||
|
const response = await this.#protoRequest(DiscoverRequest.create({
|
||||||
|
flags,
|
||||||
|
limit
|
||||||
|
}))
|
||||||
|
|
||||||
|
const discover = DiscoverResponse.toObject(response.DiscoverResponse, { longs: String })
|
||||||
|
|
||||||
|
if(discover.status !== 0) {
|
||||||
|
throw new Error(`Unexpected response: ${discover.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return discover.peers.map(sv => { return { ...sv, address: BigInt(sv.address) } })
|
||||||
|
}
|
||||||
|
|
||||||
listen(port, callback) {
|
listen(port, callback) {
|
||||||
this.on('raw', container => {
|
this.on('raw', container => {
|
||||||
if(container.message === 'Message') {
|
if(container.message === 'Message') {
|
||||||
@@ -101,5 +148,10 @@ export class Client extends EventEmitter {
|
|||||||
callback(BigInt(message.address), message.data)
|
callback(BigInt(message.address), message.data)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
publish: flags => this.#publish(port, flags),
|
||||||
|
unpublish: flags => this.#unpublish(port)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -19,7 +19,7 @@ export const [
|
|||||||
PublishResponse,
|
PublishResponse,
|
||||||
DiscoverRequest,
|
DiscoverRequest,
|
||||||
DiscoverResponse
|
DiscoverResponse
|
||||||
] = import_proto(protocol20, [
|
] = import_proto(encoders, [
|
||||||
'Container',
|
'Container',
|
||||||
'AuthenticationRequest',
|
'AuthenticationRequest',
|
||||||
'AuthenticationResponse',
|
'AuthenticationResponse',
|
||||||
|
|||||||
Reference in New Issue
Block a user