smartsocket/README.md

76 lines
2.0 KiB
Markdown
Raw Normal View History

2016-08-06 21:27:53 +00:00
# smartsocket
2016-08-07 17:07:11 +00:00
easy and secure websocket communication, Typescript ready
2016-08-06 21:27:53 +00:00
## Status
[![build status](https://gitlab.com/pushrocks/smartsocket/badges/master/build.svg)](https://gitlab.com/pushrocks/smartsocket/commits/master)
## Usage
We recommend the use of typescript.
2016-08-07 17:07:11 +00:00
Under the hood we use socket.io and shortid for managed data exchange.
2016-08-06 21:27:53 +00:00
2016-08-07 13:12:21 +00:00
### Serverside
2016-08-07 12:58:20 +00:00
```typescript
2016-08-07 13:14:53 +00:00
import * as smartsocket from "smartsocket";
2016-08-07 12:58:20 +00:00
let mySmartsocket = new smartsocket.Smartsocket({
port: 3000 // the port smartsocket will listen on
});
let mySocketRole = new smartsocket.SocketRole({
name: "someRoleName",
2016-08-07 13:12:21 +00:00
passwordHash: "someHashedString"
2016-08-07 12:58:20 +00:00
});
2016-08-15 01:47:32 +00:00
let testSocketFunction1 = new smartsocket.SocketFunction({
funcName:"testSocketFunction1",
funcDef:(data) => {
2016-08-07 13:12:21 +00:00
2016-08-15 01:47:32 +00:00
}, // the function to execute
allowedRoles:[mySocketRole] // all roles that have access to a specific function
2016-08-07 12:58:20 +00:00
});
2016-08-15 01:47:32 +00:00
mySmartsocket.clientCall("","restart",data,someTargetConnection)
2016-08-07 13:12:21 +00:00
.then((responseData) => {
2016-08-07 12:58:20 +00:00
2016-08-07 13:12:21 +00:00
});
```
#### Client side
```typescript
2016-08-07 13:14:53 +00:00
import * as smartsocket from "smartsocket";
2016-08-15 01:47:32 +00:00
let testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: "http://localhost",
password: "testPassword",
alias: "testClient1",
role: "testRole1"
2016-08-07 12:58:20 +00:00
});
2016-08-15 01:47:32 +00:00
testSmartsocketClient.connect()
.then(() => {
done();
});
2016-08-07 12:58:20 +00:00
2016-08-15 01:47:32 +00:00
let testSocketFunction2 = new smartsocket.SocketFunction({
funcName: "testSocketFunction2",
funcDef: (data) => {}, // the function to execute, has to return promise
allowedRoles:[]
2016-08-07 12:58:20 +00:00
});
2016-08-15 01:47:32 +00:00
let functionCalldata = {
funcName: "",
funcData: {
someKey:"someValue"
}
}
2016-08-07 12:58:20 +00:00
2016-08-15 01:47:32 +00:00
mySmartsocketClient.serverCall("function",functionCallData)
.then((functionResponseData) => { // the functionResponseData comes from the server... awesome, right?
2016-08-07 13:12:21 +00:00
});;
2016-08-07 17:07:11 +00:00
```
2016-08-07 17:15:05 +00:00
> **NOTE:**
2016-08-15 01:47:32 +00:00
you can easily chain dependent requests on either the server or client side with promises.
2016-08-07 17:15:05 +00:00
`data` is always a js object that you can design for your specific needs.
It supports buffers for large binary data network exchange.