2014-09-26 21:04:07 +08:00
sockets-for-cordova
===================
2014-11-26 16:22:46 +08:00
This Cordova plugin provides JavaScript API, that allows you to communicate with server through TCP protocol.
2014-09-26 21:04:07 +08:00
2014-11-25 04:51:42 +08:00
Currently we support these platforms: iOS, Android, WP8.
2014-12-05 02:26:06 +08:00
## Installation
Install this plugin simply by:
`cordova plugin add cz.blocshop.socketsforcordova`
or you can use GIT repository for most recent version:
2014-12-05 03:26:14 +08:00
`cordova plugin add https://github.com/blocshop/sockets-for-cordova`
2014-12-05 02:26:06 +08:00
2014-11-25 04:51:42 +08:00
## Sample usage
2014-12-08 06:00:27 +08:00
Here is simple example of how to connect to remote server, consume data from it and close the connection.
Create instance of Socket type:
```
var socket = new Socket();
```
Set data consumer, error and close handlers:
```
socket.onData = function(data) {
// invoked after new batch of data is received (typed array of bytes Uint8Array)
};
socket.onError = function(errorMessage) {
// invoked after error occurs during connection
};
socket.onClose = function(hasError) {
// invoked after connection close
};
```
Connect to server someremoteserver.com, with port 1234:
```
socket.open(
"someremoteserver.com",
1234,
function() {
// invoked after successful opening of socket
},
function(errorMessage) {
// invoked after unsuccessful opening of socket
});
```
Send "Hello world" to server:
```
var dataString = "Hello world";
var data = new Uint8Array(dataString.length);
for (var i = 0; i < data.length ; i + + ) {
data[i] = dataString.charCodeAt(i);
}
2014-12-18 04:52:50 +08:00
socket.write(data);
2014-12-08 06:00:27 +08:00
```
Close the connection gracefully by sending FIN to server:
```
socket.shutdownWrite();
```
or close the connection immediately:
```
socket.close();
```
2014-10-19 04:59:20 +08:00
## API
### Event handlers
2014-11-10 17:58:53 +08:00
#### `onData: (data: Uint8Array) => void`
2014-12-08 06:00:27 +08:00
Invoked after new batch of data is received by the client. Data are represented as typed array of bytes (`Uint8Array`).
2014-10-19 04:59:20 +08:00
#### `onClose: (hasError: boolean) => void`
2014-12-08 06:00:27 +08:00
Invoked after connection close. Native resources are released after this handler is invoked. Parameter `hasError` indicates whether connection was closed as a result of some error.
2014-10-19 04:59:20 +08:00
#### `onError: (message: string) => void`
2014-12-08 06:00:27 +08:00
Invoked when some error occurs during connection.
2014-10-19 04:59:20 +08:00
2014-12-20 06:28:52 +08:00
### Properties
#### `state: Socket.State`
2015-01-03 16:44:55 +08:00
Provides state of the socket. It can have 4 values represented by `Socket.State` enum: `Socket.State.CLOSED` , `Socket.State.OPENING` , `Socket.State.OPENED` or `Socket.State.CLOSING` .
2014-12-20 06:28:52 +08:00
2015-01-03 16:44:55 +08:00
Initial state of socket is CLOSED. Invoking `open` method changes state to OPENING. If it's successfuly opened, it goes to OPENED state. If opening fails, it goes back to CLOSED. Socket goes to CLOSING state immediately after `close` method is called. When socket is closed (by the server or by calling close method), it goes to CLOSED state.
##### Example
Check if socket is connected:
```
if (socket.state == Socket.State.OPENED) {
console.log("Socket is opened");
}
```
2014-12-20 06:28:52 +08:00
2014-10-19 04:59:20 +08:00
### Methods
#### `open(host, port, onSuccess?, onError?): void`
Establishes connection with the remote host.
| parameter | type | description |
| ----------- |-----------------------------|--------------|
| `host` | `string` | Remote host/ip address |
| `port` | `number` | Tcp port number |
2014-11-12 03:28:37 +08:00
| `onSuccess` | `() => void` | Success callback - called after successfull connection to the remote host. (optional)|
| `onError` | `(message: string) => void` | Error callback - called when some error occurs during connecting to the remote host. (optional)|
2014-10-19 04:59:20 +08:00
#### `write(data, onSuccess?, onError?): void`
2014-11-12 03:28:37 +08:00
Sends data to remote host.
2014-10-19 04:59:20 +08:00
| parameter | type | description |
| ----------- |-----------------------------|--------------|
2014-11-12 03:28:37 +08:00
| `data` | `Uint8Array` | Typed array of bytes, that will be written to output stream. |
| `onSuccess` | `() => void` | Success callback - called after data are successfully written to the output stream. (optional)|
| `onError` | `(message: string) => void` | Error callback - called when some error occurs during writing of data to the output stream. (optional)|
2014-10-19 04:59:20 +08:00
#### `shutdownWrite(onSuccess?, onError?): void`
2014-11-12 03:28:37 +08:00
Sends `FIN` to remote host and finishes data sending. You cannot call `write` method after you call `shutdownWrite` , otherwise `onError` callback (of `write` method) will be called.
2014-10-19 04:59:20 +08:00
| parameter | type | description |
| ----------- |-----------------------------|--------------|
2014-11-12 03:28:37 +08:00
| `onSuccess` | `() => void` | Success callback - called after sending of data is finished. (optional)|
| `onError` | `(message: string) => void` | Error callback - called when some error occurs during this procedure. (optional)|
2014-10-19 04:59:20 +08:00
#### `close(onSuccess?, onError?): void`
2014-11-12 03:28:37 +08:00
Closes the connection. `onClose` event handler is called when connection is successfuly closed.
2014-10-19 04:59:20 +08:00
| parameter | type | description |
| ----------- |-----------------------------|--------------|
2014-11-12 03:28:37 +08:00
| `onSuccess` | `() => void` | Success callback, called after connection is successfully closed. `onClose` event handler is called before that callback. (optional)|
2014-10-19 04:59:20 +08:00
| `onError` | `(message: string) => void` | Error callback, called when some error occurs during this procedure. (optional)|