new Peer(calls, messages, auth, connection)
Represent one of the sides in the protocol. This class exposes most of the interface the final user will interact with NOTE: This class should not instantiated directly. See Context#_createPeer
Parameters:
Name | Type | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
calls |
Object | ||||||||||||||||||||||||||
messages |
Object | ||||||||||||||||||||||||||
auth |
Object |
Properties
|
|||||||||||||||||||||||||
connection |
Connection |
Extends
- EventEmitter
Members
(static) ERROR :number
Protocol error codes Negative codes are always generated locally. Codes 1xxx are reserved for protocol use
Type:
- number
Properties:
Name | Type | Description |
---|---|---|
TIMEOUT |
number | The call has timed out. If the answer is received after the timeout, it will be ignored |
CLOSED |
number | The connection was closed before this call could be answer. |
NOT_IMPLEMENTED |
number | The remote has not attached a handler to this call type |
INVALID_DATA |
number | The call input data was badly formatted. You should never see this error, since the local code validates the data before sending it |
INTERNAL |
number | The remote misbehaved and could not answer the call correctly It's caused by invalid answer format |
auth :Peer~Auth
Type:
(readonly) closed :boolean
Whether the connection has been closed and nothing more can be done. Note that 'error' events may still be raised after 'close'
Type:
- boolean
(readonly) handshakeDone :boolean
Whether the handshake has been completed
Type:
- boolean
Methods
call(name, dataopt, timeoutopt, callback)
Make a call to the other side (remote)
The callback will be executed asynchronously and only once
Making an unsupported call is considered error
If the remote answers the call after the timeout has fired, the response will be ignored
If no response is expected, you should send messages, not make calls!
All errors related to this call will be routed to the callback (including invalid input format)
One can check if the error was raised locally or by the remote (as a response) by checking the isLocal
flag in the Error object
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name |
string | call name |
||
data |
* |
<optional> |
null | must follow the call input format |
timeout |
number |
<optional> |
10e3 | call timeout in ms (0 means no timeout) |
callback |
function | required |
canCall(name)
Check if the remote can answer to a call
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
canSend(name)
Check if the remote accepts a message
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
close()
Close the connection and drop all pending calls
send(name, dataopt, strictopt)
Send a message to the other side (remote) This is a fire-and-forget operation, any error will not be informed If it is important to know whether the remote has received the message, consider using calls By default (strict=false), no feedback is given
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name |
string | message name |
||
data |
* |
<optional> |
must follow the message format |
|
strict |
boolean |
<optional> |
false | whether to throw error if the operation is invalid |
Throws:
if strict and some error occurs locally
Type Definitions
Auth
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
user |
string | |
required |
boolean | |
handler |
Peer~AuthHandler | |
remoteUser |
string |
AuthHandler(user, password, done)
This:
- {Peer}
Parameters:
Name | Type | Description |
---|---|---|
user |
string | |
password |
string | |
done |
function |
PendingCall
Type:
- Object
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
function | ||
call |
Call | ||
timer |
Timer |
<nullable> |
timeout timer |
Events
close
Emited when the connection is closed and no more activity will happen
connect
Fired when the connection is ready
error
Type:
- Error