---
title: OpenClient
project: Total.js V5
version: 0.0.20
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v5/0-0-20/reference/openclient/
updated: 2026-11-18T16:56:49.572Z
---

# OpenClient

> OPENCLIENT() e i metodi di un client.

## API

### OPENCLIENT(url, [id])

Type: `method`

Apre un client WebSocket per messaggi richiesta-risposta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | L'URL WebSocket (`ws://`, `wss://`, `http://` o `https://`). |
| `id` | `String` | no |  | Un identificatore del client; se omesso ne viene generato uno casuale. |

Returns: `Object` Il client.

Si connette a un endpoint WebSocket (`http` nell'URL viene cambiato in `ws`). Tutti i client con lo stesso URL condividono un'unica connessione, che si riconnette quando il server scompare. Un client creato con un `id` già esistente sostituisce il vecchio client.

Quando il server invia un messaggio `{ type: 'init', id: '...' }`, ogni client riceve `client.meta` (il messaggio), `client.type` (il suo `id`) e viene eseguito il suo handler `online()`. `client.connected` è `true` finché la connessione è aperta. I messaggi che non sono risposte a `rpc()` vanno all'handler `message()`.

Example: Connettersi
```js
var client = OPENCLIENT('wss://service.example.com/$client/', 'myservice');

client.online(function(meta) {
	console.log(meta ? 'Connected to ' + meta.id : 'Offline');
});

client.message(function(msg) {
	console.log(msg);
});
```

### client.send(msg, [callback], [timeout])

Type: `method`

Invia un messaggio al server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `msg` | `Object` | yes |  | Il messaggio. |
| `callback` | `Function` | no |  | `function(err, response)`. |
| `timeout` | `Number` | no |  | Millisecondi di attesa della risposta. |

Returns: `Object` Il client, per il concatenamento.

Invia `msg` (un oggetto, come JSON). Con `callback` il messaggio riceve un `callbackid` e la callback viene eseguita con `(err, response)` quando il server risponde `{ callbackid, error, response }`, con `'timeout'` dopo `timeout` millisecondi, oppure con `'offline'` quando non c'è connessione. Mentre la connessione è chiusa il messaggio viene ritentato ogni 500 ms, fino a dieci volte.

`client.cmd(msg)` invia un messaggio senza attendere una risposta.

Example: Inviare e attendere
```js
client.send({ type: 'users' }, function(err, response) {
	console.log(err, response);
}, 5000);
```

Example: Comando
```js
client.cmd({ type: 'refresh' });
```

### client.rpc(msg, [callback], [timeout])

Type: `method`

Invia un messaggio e attende la risposta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `msg` | `Object` | yes |  | Il messaggio. |
| `callback` | `Function` | no |  | `function(err, response)`. |
| `timeout` | `Number` | no |  | Millisecondi di attesa. |

Returns: `Promise|Object` Una promise senza callback.

Equivale a `send()` con una callback. Senza callback restituisce una promise che si risolve con la risposta e viene rifiutata con l'errore (`'timeout'`, `'offline'` o l'`error` della risposta).

Example: await
```js
var users = await client.rpc({ type: 'users', page: 1 }, 5000);
```

### client.message(fn)

Type: `method`

Imposta l'handler dei messaggi in arrivo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(msg)`. |

Returns: `Object` Il client, per il concatenamento.

Viene eseguito per ogni messaggio del server che non è una risposta a `send()` o `rpc()` e non è il messaggio `init`.

Example: Messaggi
```js
client.message(function(msg) {
	if (msg.type === 'notify')
		console.log(msg.body);
});
```

### client.online(fn)

Type: `method`

Imposta l'handler dei cambi di stato online e offline.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(meta)`. |

Returns: `Object` Il client, per il concatenamento.

Viene eseguito con il messaggio `init` del server quando il servizio è pronto, e con `null` quando la connessione si chiude.

Example: Stato online
```js
client.online(function(meta) {
	console.log(meta ? 'online' : 'offline');
});
```

### client.error(fn)

Type: `method`

Imposta l'handler degli errori di connessione.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(err)`. |

Returns: `Object` Il client, per il concatenamento.

Viene eseguito con il messaggio di errore quando la connessione fallisce, e con `(code, message)` quando il server la chiude con un codice 4000 o superiore. Le chiamate `rpc()` in sospeso vengono rifiutate con l'errore.

Example: Errori
```js
client.error(function(err) {
	console.log('OPENCLIENT error', err);
});
```

### client.close()

Type: `method`

Rimuove il client.

`client.remove()` è un alias. Le callback in sospeso di questo client ricevono `'offline'`. La connessione condivisa viene chiusa quando non ha altri client.

Example: Chiudere
```js
client.close();
```
