From 28c989a603c891aac71762a05aaddc57d449d304 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 23 Jun 2013 15:50:11 -0400 Subject: [PATCH] errors now appear in red in the browser --- http.go | 43 +++++++++++++++++++++++++++++++++++++------ static/css/home.css | 4 ++++ static/js/skeam.js | 26 ++++++++++++++++++++------ templates/home.html | 8 +++++++- 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/http.go b/http.go index 0dd4314..b5034fb 100644 --- a/http.go +++ b/http.go @@ -2,6 +2,7 @@ package main import ( "code.google.com/p/go.net/websocket" + "encoding/json" "errors" "fmt" "github.com/jordanorelli/skeam/am" @@ -40,29 +41,59 @@ func getTemplate(relpath string) (*template.Template, error) { return t, err } -type templateHandler string +type templateHandler struct { + path string + context interface{} +} -func (t templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - tpl, err := getTemplate(string(t)) +func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + tpl, err := getTemplate(t.path) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - if err := tpl.Execute(w, nil); err != nil { + if err := tpl.Execute(w, t.context); err != nil { fmt.Println(err.Error()) } } +type wsMessage struct { + IsError bool `json:"is_error"` + Message string `json:"message"` +} + +type wsWriter struct { + conn *websocket.Conn +} + +func (w wsWriter) Write(b []byte) (int, error) { + out, err := json.Marshal(wsMessage{false, string(b)}) + if err != nil { + return 0, err + } + return w.conn.Write(out) +} + +type wsErrorWriter wsWriter + +func (w wsErrorWriter) Write(b []byte) (int, error) { + out, err := json.Marshal(wsMessage{true, string(b)}) + if err != nil { + return 0, err + } + return w.conn.Write(out) +} + func wsHandler(ws *websocket.Conn) { manager.Add(ws) defer manager.Remove(ws) - i := newInterpreter(ws, ws, ws) + i := newInterpreter(ws, wsWriter{ws}, wsErrorWriter{ws}) i.run(universe) } func runHTTPServer() { - http.Handle("/", templateHandler("home.html")) + http.Handle("/", &templateHandler{"home.html", map[string]interface{}{"ws_path": template.JS("/ws")}}) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(assets.AbsPath("static"))))) http.Handle("/ws", websocket.Handler(wsHandler)) http.ListenAndServe(*httpAddr, nil) diff --git a/static/css/home.css b/static/css/home.css index c88cec8..e8d3ecd 100644 --- a/static/css/home.css +++ b/static/css/home.css @@ -31,3 +31,7 @@ body { .message { } + +.error { + color: red; +} diff --git a/static/js/skeam.js b/static/js/skeam.js index 39ad42d..bde3f89 100644 --- a/static/js/skeam.js +++ b/static/js/skeam.js @@ -39,9 +39,15 @@ InputHandler.prototype.clear = function() { // connection handling // ----------------------------------------------------------------------------- +var buildWsPath = function(wsPath) { + if (window.location.port) { + return "ws://" + window.location.hostname + ":" + window.location.port + wsPath; + } + return "ws://" + window.location.hostname + wsPath; +} + var ConnectionHandler = function(wsPath) { - this.path = wsPath; - this.c = new WebSocket(wsPath); + this.c = new WebSocket(buildWsPath(wsPath)); this.c.onopen = _.bind(this.onopen, this); this.c.onclose = _.bind(this.onclose, this); this.c.onerror = _.bind(this.onerror, this); @@ -79,19 +85,27 @@ ConnectionHandler.prototype.sendMsg = function(message) { // response rendering // ----------------------------------------------------------------------------- -var MessageDisplay = function(selector, templateSelector) { +var MessageDisplay = function(selector, templateSelector, errorTemplateSelector) { this.elem = $(selector); this.renderMessage = _.template($(templateSelector).html()); + this.renderError = _.template($(errorTemplateSelector).html()); } -MessageDisplay.prototype.addMessage = function(message) { - var rendered = this.renderMessage({message: message}); +MessageDisplay.prototype.addMessage = function(rawmessage) { + var message = JSON.parse(rawmessage); + if (message.is_error) { + var rendered = this.renderError(message); + } else { + var rendered = this.renderMessage(message); + } this.elem.append(rendered); }; var Skeam = function(config) { this.inputHandler = new InputHandler(config.inputSelector); - this.messageDisplay = new MessageDisplay(config.outputSelector, config.messageTemplateSelector); + this.messageDisplay = new MessageDisplay(config.outputSelector, + config.messageTemplateSelector, + config.errorTemplateSelector); this.conn = new ConnectionHandler(config.wsPath); document.addEventListener("sendMsg", _.bind(this.sendMsg, this), false); document.addEventListener("receiveResponse", _.bind(this.receiveResponse, this), false); diff --git a/templates/home.html b/templates/home.html index 3a38cbb..09f153a 100644 --- a/templates/home.html +++ b/templates/home.html @@ -9,13 +9,19 @@
<%= message %>
+