Last active
August 10, 2016 12:24
-
-
Save MagnusThor/382c49c8473320b809306115d7e8a853 to your computer and use it in GitHub Desktop.
An example of how to build a "chat" using on a state-full thor-io controller (typescript,nodejs,express)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ThorIO,CanInvoke,ControllerProperties,CanSet | |
} from "thor-io.vnext" | |
// simple model | |
class ChatMessage | |
{ | |
text:string; | |
} | |
@ControllerProperties("chatController",false) | |
export class ChatController extends ThorIO.Controller | |
{ | |
@CanSet(true) // this property 'age' can be modified by the client | |
age: number ; | |
constructor(client:ThorIO.Connection){ | |
super(client); | |
this.age = 1; | |
} | |
@CanInvoke(true) // this method can be called / invoked by the client | |
sendMessage(message:ChatMessage,topic:string,controller:string) | |
{ | |
// just send (invoke) the chatMessage method on the clients where the age is => to "my" age | |
var expression = (pre: ChatController) => { | |
return pre.age >= this.age; | |
}; | |
this.invokeTo(expression,message,"chatMessage",this.alias); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<h1>Example 001 - Statefull-chat</h1> | |
<div> | |
Message:<input type="text" id="txt-message" />Age:<input type="number" id="num-age" min="0" max="99" value="1" /> | |
</div> | |
<div id="messages"></div> | |
<script src="src/thor-io.client.js"></script> | |
<script> | |
var factory, $ = function(s, el) { | |
if (!el) el = document; | |
return el.querySelector(s); | |
} | |
document.addEventListener("DOMContentLoaded", function() { | |
factory = new ThorIOClient.Factory(location.origin.replace(/^http/, 'ws'), ["chatController"]); | |
factory.OnOpen = function(chatProxy) { | |
chatProxy.On("chatMessage", function(message) { | |
var p = document.createElement("p"); | |
p.textContent = message.text; | |
$("#messages").appendChild(p); | |
}); | |
$("#num-age").addEventListener("change", function() { | |
age = parseInt(this.value); | |
chatProxy.SetProperty("age", age); // set the .age property of the ChatController | |
}); | |
$("#txt-message").addEventListener("keyup", function(evt) { | |
if (evt.keyCode === 13) { | |
// invoke the sendMessage method on ChatController.ts | |
chatProxy.Invoke("sendMessage", { | |
text: this.value | |
}); | |
this.value = ""; | |
} | |
}); | |
chatProxy.Connect(); | |
}; | |
}); | |
</script> | |
</body> | |
</html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var thorIO = require("thor-io.vnext").ThorIO; | |
var app = express(); | |
require("express-ws")(app); | |
// set up an thor-io engine, and load the controller(s) | |
var engine = new thorIO.Engine([ | |
require("./controllers/ChatController.js").ChatController | |
]) | |
app.use("/app", express.static("app")); | |
app.ws("/", function (ws, req) { | |
engine.addConnection(ws); // add the webSocket (connection) to the thor-io engine | |
}); | |
app.listen(1337); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment