new JSONBottleInterface( [config])
Constructor
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
config |
JSONBottleInterface~config |
<optional> |
Interface configuration |
Example
const JSONBottleInterface = require('json-bottle').Interface;
const iface = new JSONBottleInterface();
Methods
-
clearPendingRequests( [error])
-
Clears all pending requests by failing them
Parameters:
Name Type Argument Description errorError <optional>
Error to reject pending requests with Returns:
- Self for chaining- Type
- JSONBottleInterface
-
createMessage(path, body)
-
Create new message bound to this interface
Parameters:
Name Type Description pathstring Message path bodyObject Message body Returns:
- New message bound to this interface- Type
- JSONBottleMessage
-
destroyAllSockets()
-
Destroy all sockets
Returns:
- Self for chaining- Type
- JSONBottleInterface
-
destroySocket(id)
-
Destroy socket
Parameters:
Name Type Description idstring Socket id Returns:
- Socket with the passed id or undefined- Type
- external:Socket | undefined
-
getConfig(key)
-
Get config key
Parameters:
Name Type Description keystring Key to get the config for Returns:
- Value for the key or undefined- Type
- mixed
-
getSocket(id)
-
Get socket by ID
Parameters:
Name Type Description idstring Socket id Returns:
- Socket with the passed id or undefined- Type
- external:Socket | undefined
-
getSockets()
-
Get all sockets as array
Returns:
- All sockets- Type
- Array.<external:Socket>
-
handleIncomingData(data [, socket])
-
Create message from input data. Mainly intended for internal use, but can be used from outside to inject message into the handling cycle.
Parameters:
Name Type Argument Description datastring | JSONBottleMessage~data Either raw JSON string or a message data object (if object, id can be passed as well) socketexternal:Socket <optional>
If present, represents the socket this message came from, otherwise the message returned will be treated as locally injected Returns:
- New message object- Type
- JSONBottleMessage
-
handleIncomingMessage(message)
-
Process message through the next registered handler
Parameters:
Name Type Description messageJSONBottleMessage Incoming message to process Returns:
- Self for chaining- Type
- JSONBottleInterface
-
request(path, body [, socketId] [, timeout])
-
Issue request
Parameters:
Name Type Argument Description pathJSONBottleMessage~data.path Send on specified path bodyJSONBottleMessage~data.body Body to send socketIdstring <optional>
Id of the socket to issue the request to (request will be sent to the first found socket if not provided) timeoutnumber <optional>
Response timeout in milliseconds, 10000 by default Returns:
- Promise representing the request- Type
- Promise
-
send(path, body [, socketId])
-
Send body on path to all sockets
Parameters:
Name Type Argument Description pathJSONBottleMessage~data.path Send on specified path bodyJSONBottleMessage~data.body Body to send socketIdstring | Array.<string> <optional>
If provided, sends to socket id(s) specified, otherwise sends to all socket Returns:
- Freshly sent message- Type
- JSONBottleMessage
-
sendMessage(message, sockets [, callback])
-
Send message
Parameters:
Name Type Argument Description messageJSONBottleMessage Message to send socketsArray.<external:Socket> Array of sockets to send the message to callbackJSONBottleInterface~outgoingMessageCallback <optional>
Outgoing message callback Returns:
- Self for chaining- Type
- JSONBottleInterface
-
sendMessageRequest(message, socket, timeout)
-
Send message as a request (await response)
Parameters:
Name Type Description messageJSONBottleMessage Message that is issuing a request socketexternal:Socket Socket to send the request to timeoutnumber Response timeout in milliseconds Returns:
- Promise representing the request- Type
- Promise
-
setupSocket(socket)
-
Setup socket
Parameters:
Name Type Description socketexternal:Socket Socket to setup Returns:
- Passed socket- Type
- external:Socket
-
use(callback)
-
Use handler for all incoming messages
Parameters:
Name Type Argument Description callbackJSONBottleInterface~incomingMessageHandlerCallback <repeatable>
Message handler function Returns:
- Self for chaining- Type
- JSONBottleInterface
-
useOnce(callback)
-
Use handler for any incoming message only once
Parameters:
Name Type Argument Description callbackJSONBottleInterface~incomingMessageHandlerCallback <repeatable>
Message handler function Returns:
- Self for chaining- Type
- JSONBottleInterface
Type Definitions
-
config
-
Interface configuration object
Type:
- object
Properties:
Name Type Argument Default Description timeoutnumber <optional>
10000 Default timeout to use for the requests -
incomingMessageHandler
-
Incoming message handler object
Type:
- object
Properties:
Name Type Description testRegExp | null Path tester callbackJSONBottleInterface~incomingMessageHandlerCallback Handler callback -
incomingMessageHandlerCallback(message)
-
Incoming message handler callback
Parameters:
Name Type Description messageJSONBottleMessage Incoming message being handled -
outgoingMessageCallback(error)
-
Outgoing message callback
Parameters:
Name Type Description errorError | null The last error detected while sending to all destination sockets. No error will not abort the sending process.