UNPKG

mudb

Version:

Real-time database for multiplayer games

62 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class MuRPCHttpClientTransport { constructor(spec) { this._url = spec.url; this._timeout = spec.timeout; } send(schemas, arg) { const xhr = new XMLHttpRequest(); xhr.open('POST', this._url + '/' + schemas.protocol.name, true); xhr.responseType = ''; if (this._timeout < Infinity && this._timeout) { xhr.timeout = this._timeout; } xhr.withCredentials = true; const body = JSON.stringify(schemas.argSchema.toJSON(arg)); return new Promise((resolve, reject) => { let completed = false; xhr.onreadystatechange = () => { if (completed) { return; } const readyState = xhr.readyState; if (readyState === 4) { completed = true; const responseText = xhr.responseText; try { let json; if (0 < responseText.length) { json = JSON.parse(responseText); } else { json = { type: 'error', data: 'empty response', }; } return resolve(schemas.responseSchema.fromJSON(json)); } catch (e) { return reject(e); } } }; xhr.onabort = () => { if (completed) { return; } reject(`request aborted [mudb/rpc]`); }; xhr.onerror = () => { if (completed) { return; } reject(`error during request [mudb/rpc]`); }; xhr.send(body); }); } } exports.MuRPCHttpClientTransport = MuRPCHttpClientTransport; //# sourceMappingURL=client-browser.js.map