website: send and receive message on websockets

This commit is contained in:
Jack Pearkes 2015-04-12 18:57:31 -07:00
parent 978d324c43
commit 89dd4041b1
6 changed files with 84 additions and 36 deletions

View file

@ -23,13 +23,10 @@
</div> </div>
{{/if}} {{/if}}
<div class="log"> <div class="log">{{logs}}
{{#each line in currentLog}}
{{logPrefix}}{{line}}
{{/each}}
</div> </div>
<form {{action "submitText" on="submit"}}> <form {{action "submitText" on="submit"}}>
{{logPrefix}} {{input value=currentText class="shell" spellcheck="false"}} $ {{input value=currentText class="shell" spellcheck="false"}}
</form> </form>
</script> </script>

View file

@ -1,43 +1,39 @@
Demo.DemoCrudController = Ember.ObjectController.extend({ Demo.DemoCrudController = Ember.ObjectController.extend({
needs: ['demo'], needs: ['demo', 'application'],
isLoading: Ember.computed.alias('controllers.demo.isLoading'), isLoading: Ember.computed.alias('controllers.demo.isLoading'),
currentText: Ember.computed.alias('controllers.demo.currentText'), currentText: Ember.computed.alias('controllers.demo.currentText'),
currentLog: Ember.computed.alias('controllers.demo.currentLog'), logs: Ember.computed.alias('controllers.demo.logs'),
logPrefix: Ember.computed.alias('controllers.demo.logPrefix'), logPrefix: Ember.computed.alias('controllers.demo.logPrefix'),
currentMarker: Ember.computed.alias('controllers.demo.currentMarker'), currentMarker: Ember.computed.alias('controllers.demo.currentMarker'),
notCleared: Ember.computed.alias('controllers.demo.notCleared'), notCleared: Ember.computed.alias('controllers.demo.notCleared'),
socket: Ember.computed.alias('controllers.application.socket'),
sendCommand: function() { sendCommand: function() {
// Request this.set('isLoading', true);
Ember.run.later(this, function() {
var demoController = this.get('controllers.demo');
var command = this.getWithDefault('currentText', ''); var command = this.getWithDefault('currentText', '');
var currentLogs = this.get('currentLog').toArray(); var log = this.get('log');
// Add the last log item
currentLogs.push(command);
// Clean the state
this.set('currentText', ''); this.set('currentText', '');
demoController.logCommand(command);
// Push the new logs demoController.appendLog(command, true);
this.set('currentLog', currentLogs);
switch(command) { switch(command) {
case "clear": case "clear":
this.set('currentLog', []); this.set('logs', "");
this.set('notCleared', false); this.set('notCleared', false);
break; break;
default: default:
console.log("Submitting: ", command); var data = JSON.stringify({type: "cli", data: {command: command}});
console.log("Sending: ", data);
this.get('socket').send(data);
} }
this.set('isLoading', false);
}, 1000);
}, },
actions: { actions: {
submitText: function() { submitText: function() {
this.set('isLoading', true); this.set('controllers.demo.isLoading', true);
// Send the actual request (fake for now) // Send the actual request (fake for now)
this.sendCommand(); this.sendCommand();

View file

@ -1,17 +1,34 @@
Demo.DemoController = Ember.ObjectController.extend({ Demo.DemoController = Ember.ObjectController.extend({
currentText: "vault help", currentText: "vault help",
currentLog: [], commandLog: [],
logPrefix: "$ ", logs: "",
cursor: 0, cursor: 0,
notCleared: true, notCleared: true,
isLoading: false, isLoading: false,
setFromHistory: function() { setFromHistory: function() {
var index = this.get('currentLog.length') + this.get('cursor'); var index = this.get('commandLog.length') + this.get('cursor');
var previousMessage = this.get('commandLog')[index];
this.set('currentText', this.get('currentLog')[index]); this.set('currentText', previousMessage);
}.observes('cursor'), }.observes('cursor'),
appendLog: function(data, prefix) {
if (prefix) {
data = '$ ' + data;
}
this.set('logs', this.get('logs')+'\n'+data);
},
logCommand: function(command) {
var commandLog = this.get('commandLog');
commandLog.push(command);
this.set('commandLog', commandLog);
},
actions: { actions: {
close: function() { close: function() {
this.transitionTo('index'); this.transitionTo('index');

View file

@ -0,0 +1,25 @@
Demo.DemoRoute = Ember.Route.extend({
activate: function() {
// connect to the websocket once we enter the application route
// var socket = window.io.connect('http://localhost:8080');
var socket = new WebSocket("ws://vault-demo-server.herokuapp.com/socket");
this.controllerFor('application').set('socket', socket);
socket.onmessage = function(message) {
var data = JSON.parse(message.data),
controller = this.controllerFor('demo');
// Add the item
if (data.stdout) {
controller.appendLog(data.stdout);
}
if (data.stderr) {
controller.appendLog(data.stderr);
}
controller.set('isLoading', false);
}.bind(this);
}
});

View file

@ -63,15 +63,20 @@ Demo.DemoView = Ember.View.extend({
$('body').unbind('DOMMouseScroll mousewheel'); $('body').unbind('DOMMouseScroll mousewheel');
}, },
click: function() { // click: function() {
var element = this.$(); // var element = this.$();
// Focus
element.find('input.shell')[0].focus(); // // Record scoll position
}, // var x = element.scrollX, y = element.scrollY;
// // Focus
// element.find('input.shell')[0].focus();
// // Scroll back to where you were
// element.scrollTo(x, y);
// },
keyDown: function(ev) { keyDown: function(ev) {
var cursor = this.get('controller.cursor'), var cursor = this.get('controller.cursor'),
currentLength = this.get('controller.currentLog.length'); currentLength = this.get('controller.commandLog.length');
switch(ev.keyCode) { switch(ev.keyCode) {
// Down arrow // Down arrow
@ -95,7 +100,7 @@ Demo.DemoView = Ember.View.extend({
// command + k // command + k
case 75: case 75:
if (ev.metaKey) { if (ev.metaKey) {
this.set('controller.currentLog', []); this.set('controller.logs', '');
this.set('controller.notCleared', false); this.set('controller.notCleared', false);
} }
break; break;
@ -117,14 +122,21 @@ Demo.DemoView = Ember.View.extend({
}.observes('controller.isLoading'), }.observes('controller.isLoading'),
focus: function() {
var element = this.$().find('input.shell');
element.focus()
}.observes('controller.cursor'),
submitted: function() { submitted: function() {
var element = this.$(); var element = this.$();
console.log("submitted");
// Focus the input // Focus the input
element.find('input.shell')[0].focus(); element.find('input.shell')[0].focus();
// Scroll to the bottom of the element // Scroll to the bottom of the element
element.scrollTop(element[0].scrollHeight); element.scrollTop(element[0].scrollHeight);
}.observes('controller.currentLog') }.observes('controller.logs.length')
}); });

View file

@ -5,11 +5,12 @@
width: 100%; width: 100%;
height: 80%; height: 80%;
max-height: 80%; max-height: 80%;
line-height: 1.3;
position: fixed; position: fixed;
background-color: black; background-color: black;
color: #DDDDDD; color: #DDDDDD;
overflow: scroll; overflow: scroll;
font-size: 18px; font-size: 15px;
font-family: 'Ubuntu Mono', 'Monaco', monospace; font-family: 'Ubuntu Mono', 'Monaco', monospace;
@include box-shadow(0px -2px 30px 0px rgba(0, 0, 0, 0.40)); @include box-shadow(0px -2px 30px 0px rgba(0, 0, 0, 0.40));
} }