new Connection(config, version_, autoreconnect_)
An abstraction for a websocket or a long polling connection.
- Source:
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Object
|
configuration parameters.
|
|||||||||||||||
version_ |
string
|
Major value of the protocol version, e.g. '0' in '0.17.1'. | |||||||||||||||
autoreconnect_ |
boolean
|
If connection is lost, try to reconnect automatically. |
Members
onAutoreconnectIteration :Tinode.Connection.AutoreconnectIterationType
A callback to inform when the next attampt to reconnect will happen and to receive connection promise.
- Source:
Type:
onDisconnect :function
A callback for reporting a dropped connection.
- Source:
Type:
-
function
onOpen :function
A callback called when the connection is ready to be used for sending. For websockets it's socket open,
for long polling it's
readyState=1
(OPENED)
- Source:
Type:
-
function
Methods
connect(host_, force) → {Promise}
Initiate a new connection
- Source:
Parameters:
Name | Type | Description |
---|---|---|
host_ |
string
|
Host name to connect to; if null the old host name will be used.
|
force |
boolean
|
Force new connection even if one already exists. |
Returns:
- Type:
-
Promise
Promise resolved/rejected when the connection call completes, resolution is called without
parameters, rejection passes the {Error} as parameter.
isConnected() → {boolean}
Check if connection is alive.
- Source:
Returns:
- Type:
-
boolean
true
if connection is live, false
otherwise.
reconnect(force)
Try to restore a network connection, also reset backoff.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
force |
boolean
|
reconnect even if there is a live connection already. |
sendText(msg)
Send a string to the server.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
msg |
string
|
String to send. |
Throws:
Throws an exception if the underlying connection is not live.
transport() → {string}
Get the name of the current network transport.
- Source:
Returns:
- Type:
-
string
name of the transport such as
"ws"
or "lp"
.
Type Definitions
AutoreconnectIterationType(timeout, promise)
A callback to notify of reconnection attempts. See Tinode.Connection#onAutoreconnectIteration.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
timeout |
string
|
time till the next reconnect attempt in milliseconds. -1 means reconnect was skipped.
|
promise |
Promise
|
resolved or rejected when the reconnect attemp completes. |
OnMessage(message)
A callback to pass incoming messages to. See Tinode.Connection#onMessage.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
message |
string
|
Message to process. |