# OPENCLIENT RPC, eventi e connessioni condivise

> Esercitare OPENCLIENT con una fissura di protocollo locale, tra cui inizializzazione, correlazione callback, timout e rimozione logico-cliente.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: it
Content-SHA256: 7c9557a663c89e8cdcdcea7ff16889034b1faf1414064902dcb22c6cfe964a53
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`OPENCLIENT(url, [id])` è il client nativo del protocollo Open Platform. Converte gli URL `http`/`https` in `ws`/`wss`. I client logici con lo stesso URL condividono un WebSocket. `.online(fn)` riceve i metadati `{type:'init',id:...}` del server; `.message(fn)` riceve i messaggi non richiesti. `.rpc(message)` restituisce una promise; `.send(message, callback, [filter], [timeoutMs])` usa un callback; `.cmd(message)` invia senza attendere. `.remove()` chiude il socket fisico solo dopo la rimozione del suo ultimo client logico.

L'esempio include un server di protocollo locale. `callbackid`, `error` e `response` sono campi di protocolli. Un timout di chiamata è opt-in; impostare uno per le richieste che richiedono una scadenza. Mantenere le credenziali della piattaforma reale e gli URL nella configurazione del server.

```js controllers/open.js
exports.install = function() {
	ROUTE('SOCKET /fixture/', fixture);
	ROUTE('GET /openclient/', exercise);
};
function fixture() {
	var server = this;
	server.on('open', function(client) {
		client.send({ type: 'init', id: 'local-fixture', version: 1 });
	});
	server.on('message', function(client, message) {
		if (message.type === 'ignore')
			return;
		if (message.type === 'event') {
			client.send({ type: 'notice', value: message.value });
			return;
		}
		client.send({ callbackid: message.callbackid, error: message.type === 'fail' ? 'denied' : null, response: { value: message.value } });
	});
}
async function exercise($) {
	var a;
	var b;
	try {
		var url = $.hostname('/fixture/');
		a = OPENCLIENT(url, 'first');
		await new Promise(function(resolve, reject) {
			var timer = setTimeout(() => reject(new Error('init deadline')), 2000);
			a.online(function(meta) { if (meta) { clearTimeout(timer); resolve(); } });
		});
		b = OPENCLIENT(url, 'second');
		var shared = a.ws === b.ws;
		var sum = await a.rpc({ type: 'echo', value: 7 });
		var event = new Promise(resolve => a.message(resolve));
		a.cmd({ type: 'event', value: 9 });
		var notice = await event;
		var denied = await new Promise(resolve => a.send({ type: 'fail' }, err => resolve(String(err)), 300));
		var timeout = await new Promise(resolve => a.send({ type: 'ignore' }, err => resolve(String(err)), 50));
		a.remove();
		a = null;
		var alive = await b.rpc({ type: 'echo', value: 11 });
		var meta = b.meta;
		b.remove();
		b = null;
		$.json({ shared: shared, sum: sum, notice: notice, denied: denied, timeout: timeout, alive: alive, meta: meta });
	} catch (e) { $.invalid(e); }
	finally { if (a) a.remove(); if (b) b.remove(); }
}
```
