improve README
This commit is contained in:
parent
c7e3c4537b
commit
47ca497d48
31
README.md
31
README.md
@ -23,25 +23,38 @@ Under the hood we use socket.io and shortid for managed data exchange.
|
|||||||
### Serverside
|
### Serverside
|
||||||
```typescript
|
```typescript
|
||||||
import * as smartsocket from "smartsocket";
|
import * as smartsocket from "smartsocket";
|
||||||
|
import * as q from q // q is a promise library
|
||||||
|
|
||||||
|
// The smartsocket listens on a port and can receive new socketconnection requests.
|
||||||
let mySmartsocket = new smartsocket.Smartsocket({
|
let mySmartsocket = new smartsocket.Smartsocket({
|
||||||
port: 3000 // the port smartsocket will listen on
|
port: 3000 // the port smartsocket will listen on
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// A socket role can be referenced by SocketFunctions.
|
||||||
|
// All SocketRequests carry authentication data for a specific role.
|
||||||
|
// SocketFunctions now which roles are allowed to execute them
|
||||||
let mySocketRole = new smartsocket.SocketRole({
|
let mySocketRole = new smartsocket.SocketRole({
|
||||||
name: "someRoleName",
|
name: "someRoleName",
|
||||||
passwordHash: "someHashedString"
|
passwordHash: "someHashedString"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// A SocketFunction executes a referenced function and passes in any data of the corresponding request.
|
||||||
|
// The referenced function must return a promise and resolve with any data
|
||||||
|
// Any request will be carries a unique identifier. If the referenced function's promise resolved any passed on argument will be returned to the requesting party
|
||||||
let testSocketFunction1 = new smartsocket.SocketFunction({
|
let testSocketFunction1 = new smartsocket.SocketFunction({
|
||||||
funcName:"testSocketFunction1",
|
funcName:"testSocketFunction1",
|
||||||
funcDef:(data) => {
|
funcDef:(data) => {
|
||||||
|
console.log('testSocketFunction1 executed successfully!')
|
||||||
}, // the function to execute
|
},
|
||||||
allowedRoles:[mySocketRole] // all roles that have access to a specific function
|
allowedRoles:[mySocketRole] // all roles that have access to a specific function
|
||||||
});
|
});
|
||||||
|
|
||||||
mySmartsocket.clientCall("","restart",data,someTargetConnection)
|
// A smartsocket exposes a .clientCall() that gets
|
||||||
|
// 1. the name of the SocketFunctin on the client side
|
||||||
|
// 2. the data to pass in
|
||||||
|
// 3. And a target connection (there can be multiple connections at once)
|
||||||
|
// any unique id association is done internally
|
||||||
|
mySmartsocket.clientCall("restart",data,someTargetConnection)
|
||||||
.then((responseData) => {
|
.then((responseData) => {
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -51,6 +64,8 @@ mySmartsocket.clientCall("","restart",data,someTargetConnection)
|
|||||||
```typescript
|
```typescript
|
||||||
import * as smartsocket from "smartsocket";
|
import * as smartsocket from "smartsocket";
|
||||||
|
|
||||||
|
// A SmartsocketClient is different from a Smartsocket in that it doesn't expose any public address
|
||||||
|
// Thus any new connections must be innitiated from the client
|
||||||
let testSmartsocketClient = new smartsocket.SmartsocketClient({
|
let testSmartsocketClient = new smartsocket.SmartsocketClient({
|
||||||
port: testConfig.port,
|
port: testConfig.port,
|
||||||
url: "http://localhost",
|
url: "http://localhost",
|
||||||
@ -58,24 +73,22 @@ let testSmartsocketClient = new smartsocket.SmartsocketClient({
|
|||||||
alias: "testClient1",
|
alias: "testClient1",
|
||||||
role: "testRole1"
|
role: "testRole1"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// You can .connect() and .disconnect() from a Smartsocket
|
||||||
testSmartsocketClient.connect()
|
testSmartsocketClient.connect()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The client can also specify SocketFunctions. It can also specify Roles in case a client connects to multiple servers at once
|
||||||
let testSocketFunction2 = new smartsocket.SocketFunction({
|
let testSocketFunction2 = new smartsocket.SocketFunction({
|
||||||
funcName: "testSocketFunction2",
|
funcName: "testSocketFunction2",
|
||||||
funcDef: (data) => {}, // the function to execute, has to return promise
|
funcDef: (data) => {}, // the function to execute, has to return promise
|
||||||
allowedRoles:[]
|
allowedRoles:[]
|
||||||
});
|
});
|
||||||
|
|
||||||
let functionCalldata = {
|
|
||||||
funcName: "",
|
|
||||||
funcData: {
|
|
||||||
someKey:"someValue"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// A SmartsocketClient can call functions on the serverside using .serverCall() analog to the Smartsocket's .clientCall method.
|
||||||
mySmartsocketClient.serverCall("function",functionCallData)
|
mySmartsocketClient.serverCall("function",functionCallData)
|
||||||
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
|
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
|
||||||
|
|
||||||
|
10
test/test.ts
10
test/test.ts
@ -3,12 +3,12 @@ import should = require("should");
|
|||||||
import socketIoClient = require("socket.io-client");
|
import socketIoClient = require("socket.io-client");
|
||||||
import smartsocket = require("../dist/index");
|
import smartsocket = require("../dist/index");
|
||||||
import q = require("q");
|
import q = require("q");
|
||||||
import nodehash = require("nodehash");
|
import nodehash = require("nodehash")
|
||||||
|
|
||||||
let testSmartsocket: smartsocket.Smartsocket;
|
let testSmartsocket: smartsocket.Smartsocket
|
||||||
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
let testSmartsocketClient: smartsocket.SmartsocketClient
|
||||||
let testSocketRole1: smartsocket.SocketRole;
|
let testSocketRole1: smartsocket.SocketRole
|
||||||
let testSocketFunction1: smartsocket.SocketFunction;
|
let testSocketFunction1: smartsocket.SocketFunction
|
||||||
|
|
||||||
let testConfig = {
|
let testConfig = {
|
||||||
port: 3000
|
port: 3000
|
||||||
|
Loading…
Reference in New Issue
Block a user