Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created April 8, 2016 13:23
Show Gist options
  • Save MagnusThor/5ae998040b2a76752de864acc9dd077d to your computer and use it in GitHub Desktop.
Save MagnusThor/5ae998040b2a76752de864acc9dd077d to your computer and use it in GitHub Desktop.
// location controllers/controller.js
// this will store the messages and act as a fake db .-)
var fakeDb = {
messages : []
};
var ChatController = (function (db) {
// find out who this message targets?
var directMessage = function (message) {
var result = message.match(/@\w+/g);
return result;
};
// ctor function
var chatController = function (client) {
this.alias = "chat";
this.client = client;
this.nickname = Math.random().toString(36).substr(2, 5); // set a random nickname
};
// when a clients connect, send back the random created nick name
chatController.prototype.onopen = function () {
this.invoke({
t: "You are known as '" + this.nickname + "' to others...",
n: this.nickname
}, "nickname", this.alias)
};
// change the nick name for the current user/connection
chatController.prototype.setNickname = function (message) {
this.invokeToAll({
t: "'" + this.nickname +"' is now known as " +message.nickname
}, "chatmessage", this.alias)
this.nickname = message.nickname;
};
// get's the history
chatController.prototype.getHistory = function () {
this.invoke(db.messages,
"history", this.alias);
};
/// send a chat message
chatController.prototype.sendMessage = function (message) {
var self = this;
var messageTo = directMessage(message.t); // find if it tagets someone?
// add the "senders" nickname to message
message.n = this.nickname;
if (!messageTo) { // this message is for "all"
this.invokeToAll(message,
"chatmessage", this.alias);
db.messages.push(message); // store the message as it's pubic..
} else {
// find he user(s) and target them individualy
message.p = true; // flag the message as private
messageTo.forEach(function (nickname) {
var expression = function (pre) {
return pre["chat"].nickname === nickname.replace("@","");
};
self.invokeTo(expression, message, "chatmessage", self.alias);
});
// send the private message back to calle as well..
this.invoke(message,
"chatmessage", this.alias);
}
};
return chatController;
})(fakeDb /* pass a fare database :-) */);
exports.ChatController = ChatController;
<!-- // /test/index.html -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Chat built in thorio in 1h </title>
<style>
input {
display: inline;
}
#nick-name {
width: 150px;
}
#chat-message {
width: 60%;
}
.is-private {
color: red;
}
</style>
</head>
<body>
<h1>Chat</h1>
<div>
<input type="text" id="nick-name" placeholder="" />
<input type="text" id="chat-message" placeholder="enter a message" />
</div>
<hr />
<div id="messages"></div>
<script src="//rawgit.com/MagnusThor/thorio.client/master/src/thorio.client.latest.js">
</script>
<script>
var client;
var $ = function (q, el) {
if (!el) el = document;
return el.querySelector(q);
};
var addMessage = function (message) {
var p = document.createElement("p");
var dt = message.dt ? new Date(message.dt) : new Date();
var nickName = message.n ? message.n : "Unknown"
p.textContent = dt.getHours() + ":" + dt.getMinutes() + " (" + nickName + " ) - " + message.t;
if (message.hasOwnProperty("p")) p.classList.add("is-private");
$("#messages").insertBefore(p, $("#messages").firstChild);
};
document.addEventListener("DOMContentLoaded", function () {
var client = new ThorIOClient.Factory("ws://localhost:1337", ["chat"]);
client.onopen = function (chat) {
addMessage({
t: "Connected to the Engine (endpoint)",
})
chat.onopen = function (ci) {
addMessage({
t: "Connected to the chat, getting history.",
});
this.invoke("getHistory", {});
};
chat.on("nickname", function (message) {
addMessage(message);
$("#nick-name").value = message.n;
});
chat.on("chatmessage", function (message) {
addMessage(message);
});
chat.on("history", function (messages) {
messages.forEach(function (message) {
addMessage(message);
});
});
document.querySelector("#chat-message").addEventListener("keydown", function (evt) {
if (evt.keyCode === 13) {
chat.invoke("sendMessage",
{ t: evt.target.value, dt: new Date() });
evt.target.value = "";
};
});
document.querySelector("#nick-name").addEventListener("keydown", function (evt) {
if (evt.keyCode === 13) {
chat.invoke("setNickname",
{ nickname: evt.target.value });
};
});
chat.connect();
};
});
</script>
</body>
</html>
{
"name": "thoriodemo1",
"version": "1.0.0",
"description": "a simple chat ",
"main": "server.js",
"author": {
"name": "Magnus Thor",
"email": "[email protected]"
},
"dependencies": {
"thorio": "git://github.com/MagnusThor/thorio.git#master",
"express": "^4.13.4",
"express-ws": "^1.0.0"
}
}
var express = require("express");
app = express();
var ThorIO = require("thorio").ThorIO;
var controllers = require("./controllers/controllers.js");
var thorIO = new ThorIO.Engine([{
alias:"chat", instance: controllers.ChatController
}]);
var expressWs = require("express-ws")(app);
app.use('/test', express.static('test'));
app.ws("/", function (ws, req) {
thorIO.addConnection(ws);
});
app.listen(process.env.port || 1337);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment