From 73cd1c6538547bd487c6a3f3fde815459d91b31e Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 22 Aug 2014 15:37:26 +0800 Subject: [PATCH] . --- data/src/hello/main.go | 2 +- output/outputs.go | 16 +++++++++++----- static/js/wide.js | 20 ++++++++++++++++---- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/data/src/hello/main.go b/data/src/hello/main.go index 4b8553a..ae3e566 100644 --- a/data/src/hello/main.go +++ b/data/src/hello/main.go @@ -5,5 +5,5 @@ import ( ) func main() { - fmt.Println("Hello, 世界") + fmt.Println("Hello, 世界")1 } diff --git a/output/outputs.go b/output/outputs.go index b3a2b61..6774409 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -69,7 +69,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { cmd.Start() - rec := map[string]interface{}{} + channelRet := map[string]interface{}{} go func(runningId int) { glog.Infof("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath) @@ -83,10 +83,11 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { break } else { - rec["output"] = string(buf[:count]) + channelRet["output"] = string(buf[:count]) + channelRet["cmd"] = "run" if nil != outputWS[sid] { - err := outputWS[sid].WriteJSON(&rec) + err := outputWS[sid].WriteJSON(&channelRet) if nil != err { glog.Error(err) break @@ -150,7 +151,12 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { cmd := exec.Command("go", argv...) cmd.Dir = curDir - glog.Info("go build ", filePath) + glog.Infof("go build -o %s %s", executable, filePath) + + executable = curDir + string(os.PathSeparator) + executable + + // 先把可执行文件删了 + os.RemoveAll(executable) stdout, err := cmd.StdoutPipe() if nil != err { @@ -184,7 +190,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { channelRet["output"] = string(buf[:count]) channelRet["cmd"] = "build" channelRet["nextCmd"] = "run" - channelRet["executable"] = curDir + string(os.PathSeparator) + executable + channelRet["executable"] = executable if nil != outputWS[sid] { glog.Infof("Session [%s] 's build [id=%d, file=%s] has done", sid, runningId, filePath) diff --git a/static/js/wide.js b/static/js/wide.js index 47efa52..a530986 100644 --- a/static/js/wide.js +++ b/static/js/wide.js @@ -2,13 +2,24 @@ var outputWS = new WebSocket(config.channel.output + '/output/ws'); outputWS.onopen = function() { console.log('[output onopen] connected'); }; + outputWS.onmessage = function(e) { console.log('[output onmessage]' + e.data); var data = JSON.parse(e.data); - if ('init-output' !== data.cmd) { + if ('run' === data.cmd) { + console.log('run: ' + data.cmd); + $('#output').val($('#output').val() + data.output); - } + } else if ('build' === data.cmd) { + console.log('build: ' + data.cmd); + + $('#output').val(data.output); + + if (0 != data.output.length) { // 说明编译有错误输出 + return; + } + } if ('build' == data.cmd) { if ('run' === data.nextCmd) { @@ -22,9 +33,10 @@ outputWS.onmessage = function(e) { data: JSON.stringify(request), dataType: "json", beforeSend: function(data) { - $('#output').val(''); + $('#output').val(''); }, - success: function(data) { + success: function(data) { + } }); }