This page gives an overview of using the Connector API in the client. This page is intended for developers only. # Use and examples First, import Connector: ```javascript import connector from 'path/to/connector' ``` Then, you have to wait until the connection is open: ```javascript function doStuff () { ... } connector.addEventListener('open', doStuff) // or connector.addEventListener('open', () => {...}) ``` In or after this event you can listen to or send messages. * If you want to constantly listen to a specific message type: ```javascript connector.listenToMsg('message-type', (content) => { ... }) ``` * If you want to constantly listen to a all messages: ```javascript connector.addEventListener('message', (response) => { ... }) ``` * If you want to stop listening with that function: ```javascript connector.addEventListener('message', doStuff) // or connector.listenToMsg('type', doStuff) connector.removeEventListener('message', doStuff) ``` * If you want to do a single request (for messages with request-response structure): ```javascript connector.request( 'message-type-request', 'message-type-response', {payload} ).then((response) => { ... }) ``` * If you want to send a message: ```javascript connector.send( 'file-content-request', {payload} ) ``` # JSdoc ## Classes
Connector
## Typedefs
Message : Object
connectorCallback : function
## Connector **Kind**: global class * [Connector](#Connector) * [new Connector(path)](#new_Connector_new) * [.ws](#Connector+ws) : WebSocket * [.close(code, reason)](#Connector+close) * [.addEventListener(type, callback)](#Connector+addEventListener) * [.removeEventListener(type, callback)](#Connector+removeEventListener) * [.send(type, content, [sender], [prefDest])](#Connector+send) * [.request(requestType, responseType, content)](#Connector+request) ⇒ Promise.<Object> * [.listenToMsg(type, listener)](#Connector+listenToMsg) ### new Connector(path) Creates a connection and setups listeners. | Param | Type | Description | | --- | --- | --- | | path | string \| URL | The path to the websocket server. | ### connector.ws : WebSocket The websocket interface. **Kind**: instance property of [Connector](#Connector) ### connector.close(code, reason) Closes connection to websocket server **Kind**: instance method of [Connector](#Connector) | Param | Type | Default | Description | | --- | --- | --- | --- | | code | number | 0 | Status code of why the connection is closing. | | reason | string | "I am done." | A human readable reason of why the connection is closing. | ### connector.addEventListener(type, callback) Add event listener to the websocket interface. **Kind**: instance method of [Connector](#Connector) **See**: https://github.com/websockets/ws/blob/HEAD/doc/ws.md#event-close-1 | Param | Type | Description | | --- | --- | --- | | type | string | A websocket event type. | | callback | [connectorCallback](#connectorCallback) | The listener that is called when the specified event happens. | ### connector.removeEventListener(type, callback) Removes a listener. **Kind**: instance method of [Connector](#Connector) | Param | Type | Description | | --- | --- | --- | | type | string | The type of the event. | | callback | [connectorCallback](#connectorCallback) | The function to stop listening to the specific event. | ### connector.send(type, content, [sender], [prefDest]) Send some content to the websocket server. **Kind**: instance method of [Connector](#Connector) | Param | Type | Default | Description | | --- | --- | --- | --- | | type | string | | The type of the message. | | content | Object | | The payload of the message. | | [sender] | string | "CLIENT" | The identifier of the sender. | | [prefDest] | string | null | The destination where the message should preferable go. | ### connector.request(requestType, responseType, content) ⇒ Promise.<Object> Sends a message and listens for the response. A helper method for messages with a request-response structure. **Kind**: instance method of [Connector](#Connector) **Returns**: Promise.<Object> - - A promise with the response content/ | Param | Type | Description | | --- | --- | --- | | requestType | string | The type of the message send. | | responseType | string | The type of the message to receive. | | content | Object | The payload to send. | ### connector.listenToMsg(type, listener) A method to listen to a specific message type. **Kind**: instance method of [Connector](#Connector) | Param | Type | Description | | --- | --- | --- | | type | string | The message type to listen to. | | listener | [connectorCallback](#connectorCallback) | The function to call. | ## Message : Object **Kind**: global typedef **See**: https://github.com/psedit/cte/wiki/Server-service-messages **Properties** | Name | Type | Description | | --- | --- | --- | | type | string | The type of message | | uuid | string | The unique id of the message | | sender | string | sender service or sender client address | | pref_dest | string | sender service or sender client address | | content | Object | the payload of the message | ## connectorCallback : function **Kind**: global typedef | Param | Type | Description | | --- | --- | --- | | The | [Message](#Message) | message response. |