Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created July 30, 2020 12:50
Show Gist options
  • Save MagnusThor/714d5e6e59d2e042111e3aefc94fe435 to your computer and use it in GitHub Desktop.
Save MagnusThor/714d5e6e59d2e042111e3aefc94fe435 to your computer and use it in GitHub Desktop.
thor-io.vnext ExampleController
import { CanSet } from '../../src/Decorators/CanSet';
import { CanInvoke } from '../../src/Decorators/CanInvoke';
import { ControllerProperties } from '../../src/Decorators/ControllerProperties';
import { ControllerBase } from "../../src/Controller/ControllerBase";
import { BinaryMessage } from 'thor-io.client-vnext';
import { Connection } from '../../src/Connection/Connection';
interface IMyModel{
age:number;
fullName: string;
skills:Array<string>
}
/**
* Each client that connects to the ThorIO instance will have its own instance of ExampleController
* See each example method below for different way to make RPC calls to the clients.
* @export
* @class MyController
* @extends {ControllerBase}
*/
@ControllerProperties("example") // controller will be exposed as "exaple"
export class ExampleController extends ControllerBase {
@CanSet(true)
age: number; // can be set by client using .setProperty(name,value);
constructor(connection: Connection) {
super(connection);
this.age = 0;
}
onclose(){
console.log(`Closed an instance of ${this.alias} for ${this.connection.id}`);
}
onopen(){
this.queryParameters.forEach ( (v,k) => {
console.log(k,v);
});
this.headers.forEach ( (p,v) => {
console.log(p,v);
});
console.log(`Created an instance of ${this.alias} for ${this.connection.id}`);
}
/**
* When invokeAndReturn is called a RPC call to the callle (the client that called the method)
*
* @param {*} data
* @memberof MyController
*/
@CanInvoke(true)
invokeAndReturn(data: any) {
this.invoke(data, "invokeAndReturn");
}
/**
* When invokeAndSendToAll is called a RPC call to all connnected clients is done
* invokeToAll(...);
* @param {*} data
* @memberof MyController
*/
@CanInvoke(true)
invokeAndSendToAll(data: any) {
this.invokeToAll(data, "invokeAndSendToAll");
}
/**
* Publish send a message to all client's that has established a subscription using subscribe(topic)
* in this case tempChange.
* @param {*} temperatue
* @memberof MyController
*/
@CanInvoke(true)
publishTemperature(temperatue:any){
this.publishToAll(temperatue, "tempChange");
}
/**
* When invokeAndSendToAll is called a RPC call to all except called clients is done.
*
* @param {*} data
* @memberof MyController
*/
@CanInvoke(true)
invokeAndSendOthers(data: any) {
this.invokeToOthers(data, "invokeAndSendOthers");
}
/**
* When invoke to invokeToExpr is called, myExpresion is evaluated
* on each client and RPC call to invokeToExpr for each match, based on the
* state of age:number in this example.
* @param {IMyModel} myModel
* @memberof MyController
*/
@CanInvoke(true)
invokeToExpr(myModel: IMyModel) {
// this is a expression, send just to clients
// that has an age (property) > myModel.age;
const myExpresion = (pre: ExampleController) => {
return pre.age >= myModel.age;
}
this.invokeTo(myExpresion, myModel, "invokeToExpr");
}
/**
* Create and send a BinaryMessage
*
* @param {IMyModel} myModel
* @memberof MyController
*/
@CanInvoke(true)
passAnArrayByffer(myModel:IMyModel){
let buffer = new ArrayBuffer(1024);
this.invoke(myModel,"anArrayBuffer",this.alias,buffer); // this.alias
}
/**
* Just another method that illustrates that any methor name is valid, note the CanInvoke(boolean) decorator
* that is used to make the method invokeable, default is false
* @param {IMyModel} myModel
* @memberof MyController
*/
@CanInvoke(true)
anyMethodNameThatYouWhantToExpose(myModel:IMyModel){
// do ops..
this.age= myModel.age;
this.invoke(myModel,"anyListnerOnTheClient");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment