@noveo/dual-rpc-ws
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -24,2 +24,3 @@ "use strict"; | ||
} | ||
// @ts-ignore | ||
class Client extends WebSocket { | ||
@@ -43,2 +44,6 @@ constructor(cid, address, protocols) { | ||
if (message.method) { | ||
if (message.method === 'connect') { | ||
this.dispatchEvent(new Event('handshake')); | ||
return; | ||
} | ||
try { | ||
@@ -45,0 +50,0 @@ // @ts-ignore |
@@ -40,2 +40,25 @@ "use strict"; | ||
ws.deviceId = message.params.id; | ||
if (this.listenerCount('handshake') === 1) { | ||
this.emit('handshake', message.params.id, (result) => { | ||
ws.send(JSON.stringify({ | ||
jsonrpc: '2.0', | ||
method: 'connect', | ||
params: { | ||
result, | ||
}, | ||
})); | ||
if (!result) { | ||
ws.close(); | ||
} | ||
}); | ||
} | ||
else { | ||
ws.send(JSON.stringify({ | ||
jsonrpc: '2.0', | ||
method: 'connect', | ||
params: { | ||
result: true, | ||
}, | ||
})); | ||
} | ||
} | ||
@@ -68,3 +91,3 @@ break; | ||
if (!this.requests.has(message.id)) { | ||
throw new Error(''); | ||
throw new Error(`Wrong request id: ${message.id}`); | ||
} | ||
@@ -82,3 +105,3 @@ // @ts-ignore | ||
if (!this.devices.has(clientId)) { | ||
throw new Error(''); | ||
throw new Error(`Device with id: ${clientId} doesn't connected`); | ||
} | ||
@@ -85,0 +108,0 @@ // @ts-ignore |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const index_1 = require("./index"); | ||
const server = new index_1.Server({ | ||
const server_1 = __importDefault(require("./server")); | ||
const server = new server_1.default({ | ||
port: 8081, | ||
}); | ||
const client = new index_1.Client('id1', 'ws://localhost:8081'); | ||
client.register('hi', (params) => { | ||
console.log('client hi', params); | ||
return Promise.resolve(`hello, ${params.b}`); | ||
}); | ||
server.register('hi', (params) => { | ||
@@ -24,9 +14,13 @@ console.log('server hi', params); | ||
}); | ||
setTimeout(() => __awaiter(this, void 0, void 0, function* () { | ||
const [a, b] = yield Promise.all([ | ||
client.call('hi', { a: 1 }), | ||
server.call('id1', 'hi', { b: 2 }), | ||
]); | ||
console.log(a, b); | ||
}), 2000); | ||
server.on('handshake', (id, callback) => { | ||
console.log('connected', id); | ||
callback(false); | ||
}); | ||
setInterval(async () => { | ||
try { | ||
const [a, b] = await Promise.all([server.call('id1', 'hi', { b: 2 })]); | ||
console.log(a, b); | ||
} | ||
catch (e) { } | ||
}, 20000); | ||
/* | ||
@@ -33,0 +27,0 @@ В терминале |
{ | ||
"name": "@noveo/dual-rpc-ws", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Remote procedure call", | ||
@@ -9,3 +9,3 @@ "browser": "build/client.js", | ||
"scripts": { | ||
"test": "tsc --build tsconfig.json && node build/test.js", | ||
"test": "tsc --build tsconfig.json && tsc --build tsconfig.client.json && node build/test.js", | ||
"build": "tsc --build tsconfig.json && tsc --build tsconfig.client.json", | ||
@@ -12,0 +12,0 @@ "prepublish": "npm run-script build" |
# Dual rpc via websockets | ||
## Client class | ||
### Constructor (token: string, address: string) | ||
Connect to the server using an address and unique id that concrete the client | ||
### Methods | ||
### call(method: string, params?: object|Array): Promise\<object\> | ||
Call the server method using params as one argument construction. | ||
Returns a Promise with the JSON response from the server | ||
### register(method: string, handler: Function\<Promise\>): void | ||
Register the method on the client side, handler must return an object or a Promise\<object\> | ||
which will be held on the server side | ||
## Example of usage | ||
@@ -4,0 +19,0 @@ |
@@ -22,4 +22,4 @@ { | ||
"include": [ | ||
"src/server.ts" | ||
"src/server.ts", "src/test.ts" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23306
408
47