Created
March 21, 2016 07:31
-
-
Save MagnusThor/940699bbef0b5e560bc6 to your computer and use it in GitHub Desktop.
Sort of p2p using - https://github.com/MagnusThor/thorio/
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
| <!DOCTYPE html> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Chat</title> | |
| <style> | |
| mark{ | |
| margin-right:10px; | |
| } | |
| .message { | |
| margin-right:10px; | |
| } | |
| .sender { | |
| font-style:italic; | |
| margin-right:10px; | |
| } | |
| label{ | |
| display:block; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>PeerController</h1> | |
| <label>PeerId:</label> | |
| <input type="text" id="peerId" /><button id="setPeerId">Change peerId</button> | |
| <label>Message</label> | |
| <input type="text" id="chatMessage" placeholder="Say something..." /> | |
| <ul id="messages"> | |
| </ul> | |
| <!--<script src="thorio.client.latest.js"></script>--> | |
| <script src="//rawgit.com/MagnusThor/thorio.client/master/src/thorio.client.latest.js"> </script> | |
| <script> | |
| var doc = document, client; | |
| var $ = function (selector, context) { | |
| if (!context) context = document; | |
| return context.querySelector(selector); | |
| }; | |
| var addMessage = function (message) { | |
| var li = doc.createElement("li"); | |
| var mark = doc.createElement("mark"); | |
| var date = new Date(message.created) | |
| mark.textContent = date.getHours() + ":" + date.getMinutes(); | |
| var text = doc.createElement("span"); | |
| text.classList.add("message"); | |
| text.textContent = message.text; | |
| var sender = doc.createElement("span"); | |
| sender.classList.add("sender"); | |
| sender.textContent = message.nickName; | |
| li.appendChild(mark); | |
| li.appendChild(sender); | |
| li.appendChild(text); | |
| doc.querySelector("#messages").appendChild(li); | |
| }; | |
| doc.addEventListener("DOMContentLoaded", function () { | |
| client = new ThorIOClient.Factory("ws://localhost:1337", [ | |
| "p2p" | |
| ]); | |
| client.onopen = function (chatController) { | |
| chatController.on("peerId", function (message) { | |
| $("#peerId").value = message.peerId; | |
| }); | |
| $("#chatMessage").addEventListener("keydown", function (event) { | |
| if (event.keyCode === 13) { | |
| event.preventDefault(); | |
| chatController.invoke("sendMessage", { text: this.value }); | |
| this.value = ""; | |
| }; | |
| }); | |
| $("#setPeerId").addEventListener("click", function (event) { | |
| event.preventDefault(); | |
| var peerId = $("#peerId").value; | |
| chatController.invoke("setPeerId", { peerId: peerId }); | |
| }); | |
| chatController.onopen = function (message) { | |
| console.log("connected .."); | |
| }; | |
| chatController.on("chatMessage", function (charMessage) { | |
| addMessage(charMessage); | |
| }); | |
| chatController.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
| MyControllers.PeerController =(function () { | |
| var randomString = function () { | |
| return Math.random().toString(36).substring(7); | |
| }; | |
| var chatMessage = function (text,nickname) { | |
| this.text = text; | |
| this.created = new Date(); | |
| }; | |
| var peerController = function (client) { | |
| this.client = client; | |
| this.alias = "p2p"; | |
| this.peerId = randomString(); // Just create a random PeerId | |
| }; | |
| peerController.prototype.setPeerId = function (message) { | |
| this.peerId = message.peerId; | |
| this.invoke(new chatMessage("You are now known connected to Peer - " + this.peerId), | |
| "chatMessage", this.alias); | |
| }; | |
| peerController.prototype.onopen = function () { | |
| this.invoke(new chatMessage("Welcome to the PeerController... "), | |
| "chatMessage", this.alias); | |
| // send the created / random peerId to callee | |
| this.invoke({ peerId: this.peerId }, | |
| "peerId", this.alias); | |
| }; | |
| peerController.prototype.sendMessage = function (message) { | |
| var peerId = this.peerId; | |
| var expression = function (pre) { | |
| return pre["p2p"].peerId === peerId; | |
| }; | |
| this.invokeTo(expression, | |
| new chatMessage(message.text), | |
| "chatMessage", this.alias); | |
| }; | |
| return peerController; | |
| })(); |
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"); app = express(); | |
| //var ThorIO = require("./index.js").ThorIO; | |
| var ThorIO = require("thorio").ThorIO | |
| var myControllers = require("./examples/controllers/controllers.js").MyControllers | |
| var thorIO = new ThorIO.Engine( | |
| [ | |
| { alias: "p2p", instance: myControllers.PeerController } | |
| ] | |
| ); | |
| var expressWs = require("express-ws")(app); | |
| app.use('/examples', express.static('examples')); | |
| 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