@wuespace/vertx-mock-server
Install using
npm install @wuespace/vertx-mock-server
You can access these members by importing the module:
import { member } from '@wuespace/vertx-mock-server';
class MockServer
type ErrorContent
interface OnInit
interface OnConnection
interface OnMessage
interface OnClosedConnection
interface OnClose
interface SendCallbackArgs
interface PublishCallbackArgs
type CallbackArgs
type Callback
type CallbackId
interface ListenOptions
interface VertxMockServerOptions
MockServer
class
MockServer
A mock server for a Vert.x WebSocket Event Bus bridge
to JavaScript clients like @wuespace/vertx-event-bus#EventBus
.
constructor
function
(
options
?
:
Partial<VertxMockServerOptions>
)
:
any
Builds a new Vert.x mock server.
@param:
options - options to further configure the mock server
@see:
@see:
@example:
const logger = new Logger({
loggers: [ChalkLogger(chalk)]
});
const moduleLogger = logger.getComponentLogger('MockServer');
const server = new MockServer({ logger: moduleLogger });
server.listen();
listen (...)
function listen
(
options
?
:
Partial<ListenOptions>
)
:
void
Starts the Vert.x mock server, listening for incoming connections.
@param:
options - options to configure the Vert.x mock server in the listen process
@see:
@example:
const server = new MockServer();
server.listen({
port: 12345,
hostname: 'localhost'
}); // listen on port 12345 on 'localhost'
close (...)
function close
(
)
:
Promise<void>
Stops the server from accepting new connections and closes currently active connections.
@returns:
a promise which resolves when the server is closed
@see:
@example:
const server = new MockServer();
server.listen();
// some code later
server.close().then(() => console.log('Closed'));
protected send (...)
function send
(
channel
:
ChannelAddress
,
content
:
JsonSerializable
)
:
void
Sends a message to the specified channel.
@param:
channel - the channel the message gets sent to
@param:
content - the message content that gets sent over the specified channel
@see:
@see:
@see:
@example:
const CHANNEL = 'my-channel';
class MyMockServer extends MockServer implements OnInit, OnClose {
private iteration: number = 0;
private intervalId: any = undefined;
onInit() {
// send and increment
this.intervalId = setInterval(() => {
this.send(CHANNEL, this.iteration);
this.iteration = this.iteration + 1;
}, 1000);
}
onClose() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
}
}
const server = new MyMockServer();
server.listen();
protected sendError (...)
function sendError
(
error
:
ErrorContent
)
:
void
Sends an error message to the specified channel.
@param:
error - the message that gets sent over the specified channel
@see:
@see:
@see:
@see:
@example:
const CHANNEL = 'my-channel';
class MyMockServer extends MockServer implements OnInit, OnClose {
private id: CallbackId = 0;
onInit() {
this.id = this.register(CHANNEL, () => {
this.sendError({
failureCode: 1,
failureType: 'Message received',
message: 'Message was received'
});
});
}
onClose() {
this.unregister(this.id);
}
}
const server = new MyMockServer();
server.listen();
protected register (...)
function register
(
channel
:
ChannelAddress
,
callback
:
Callback
)
:
CallbackId
Registers a callback to a specified channel. The callback gets called when a new message arrives on this channel.
@param:
channel - the channel the callback gets registered to
@param:
callback - the callback to register
@see:
@see:
@see:
@example:
const CHANNEL = 'my-channel';
class MyMockServer extends MockServer implements OnInit, OnClose {
private id: CallbackId = 0;
onInit() {
this.id = this.register(CHANNEL, () => {
this.send(CHANNEL, 'pong');
});
}
onClose() {
this.unregister(this.id);
}
}
const server = new MyMockServer();
server.listen();
protected unregister (...)
function unregister
(
id
:
CallbackId
)
:
void
Unregisters/Removes a callback from a channel
with the given callback id from the MockServer.register
function.
@param:
id - the callback id returned by the MockServer.register
function
@see:
@see:
@example:
const CHANNEL = 'my-channel';
class MyMockServer extends MockServer implements OnInit, OnClose {
private id: CallbackId = 0;
onInit() {
this.id = this.register(CHANNEL, () => {
this.send(CHANNEL, 'pong');
});
}
onClose() {
this.unregister(this.id);
}
}
const server = new MyMockServer();
server.listen();
protected getConnections (...)
function getConnections
(
)
:
Array<Connection>
Get all currently active SockJS connections to the Vert.x mock server.
@returns:
all current SockJS connections
@example:
const CHANNEL = 'my-channel';
class MyMockServer extends MockServer implements OnInit, OnClose {
private id: CallbackId = 0;
onInit() {
this.id = this.register(CHANNEL, args => {
if (args.type === 'send') args.respond(this.getConnections().length);
});
}
onClose() {
this.unregister(this.id);
}
}
const server = new MyMockServer();
server.listen();
ErrorContent
type ErrorContent
Alias for:
Pick<
ErrorMessage,
'failureCode' | 'failureType' | 'message'
>
The contents of an error message.
OnInit
interface
OnInit
Defines an event handler that gets called when the Vert.x mock server is set up and ready to handle incoming connections.
@see:
onInit (...)
function onInit
(
)
:
void
An event handler which gets called when a the Vert.x mock server is set up and ready to handle incoming connections.
OnConnection
interface
OnConnection
Defines an event handler that gets called
when a new connection with a client is established
and all MockServer.register
calls have taken place.
@see:
onConnection (...)
function onConnection
(
connection
:
Connection
)
:
void
An event handler which gets called
when a new connection with a client is established
and all MockServer.register
calls have taken place.
@param:
connection - the established connection with the client
OnMessage
interface
OnMessage
Defines an event handler that gets called when a messages arrives at the Vert.x mock server from any connected client.
@see:
onMessage (...)
function onMessage
(
message
:
Message
)
:
boolean | void
An event handler which gets called when a new message arrives at the event bus.
@param:
message - the received message
OnClosedConnection
interface
OnClosedConnection
Defines an event handler that gets called when a connection to a client has closed.
@see:
onClosedConnection (...)
function onClosedConnection
(
connection
:
Connection
)
:
void
An event handler which gets called when a connection to a client has closed.
@param:
connection - the closed connection with the client
OnClose
interface
OnClose
Defines an event handler that gets called when the Vert.x mock server closed all still active connections and will shutdown now.
@see:
onClose (...)
function onClose
(
)
:
void
An event handler which gets called when the Vert.x mock server closed all still active connections and will shutdown now.
SendCallbackArgs
interface
SendCallbackArgs
extends BaseCallbackArgs
The arguments of a send callback which gets called
when the Vert.x mock server receives
a @wuespace/telestion-client-types#SendMessage
.
type
Type:
'send'
No docs provided
respond
Type:
(response: JsonSerializable) => void
A callback to respond to the sent message.
@param:
response - the response message to send back
PublishCallbackArgs
interface
PublishCallbackArgs
extends BaseCallbackArgs
The arguments of a publish callback which gets called
when the Vert.x mock server receives
a @wuespace/telestion-client-types#PublishMessage
.
type
Type:
'publish'
No docs provided
CallbackArgs
type CallbackArgs
Alias for:
SendCallbackArgs | PublishCallbackArgs
The arguments of a callback which gets called
when the Vert.x mock server receives
a @wuespace/telestion-client-types#SendMessage
or a @wuespace/telestion-client-types#PublishMessage
from a client.
Callback
type Callback
Alias for:
(args: CallbackArgs) => void
The callback which gets called
when the Vert.x mock server receives
a @wuespace/telestion-client-types#SendMessage
or a @wuespace/telestion-client-types#PublishMessage
from a client.
CallbackId
type CallbackId
Alias for:
number
The id of a registered callback.
It can be registered with MockServer.register
and unregistered with this value in MockServer.unregister
.
ListenOptions
interface
ListenOptions
Options to configure the Vert.x mock server in the listen process.
port
Type:
number
The port on which the server listens.
Defaults to 9870
.
hostname
Type:
string
The hostname on which the server listens.
Defaults to '0.0.0.0'
.
VertxMockServerOptions
interface
VertxMockServerOptions
Options to configure the Vert.x mock server on creation.
prefix
Type:
string
The prefix on which the event bus will be available.
Defaults tp '/bridge'
.
logger
Type:
ComponentLogger
The logging instance the mock server uses to print out connection and debug information.
Notice: By default, the mock server does not print anything to console.