1
1
package main
2
2
3
3
import (
4
- // "code.google.com/p/go.net/websocket"
5
4
"flag"
6
5
"fmt"
6
+ "github.com/jijinggang/go-websocket"
7
7
"github.com/jijinggang/goutil"
8
8
"html/template"
9
9
"io"
@@ -23,18 +23,17 @@ func showCmdListPage(w http.ResponseWriter, req *http.Request) {
23
23
tmpl .Execute (w , _config .Cmds )
24
24
}
25
25
26
- /*
27
26
func showCmdResultInitPage (w http.ResponseWriter , req * http.Request ) {
28
27
id := req .FormValue ("id" )
29
28
html := strings .Replace (_html , "{id}" , id , - 1 )
30
29
io .WriteString (w , html )
31
- }*/
30
+ }
32
31
33
32
func writeString (w io.Writer , str string ) {
34
33
w .Write ([]byte (str ))
35
34
}
36
35
37
- func exec_cmd (id int , w io. Writer ) {
36
+ func exec_cmd (id int , w * websocket. Conn ) {
38
37
cmdCfg := & _config .Cmds [id ]
39
38
if cmdCfg .Running {
40
39
writeString (w , "The script is running, please waitting ......." )
@@ -74,13 +73,16 @@ func exec_cmd(id int, w io.Writer) {
74
73
75
74
}
76
75
77
- func execAndRefreshCmdResult (w http.ResponseWriter , req * http.Request ) {
76
+ func execAndRefreshCmdResult (ws * websocket.Conn ) {
77
+ req := ws .Request ()
78
78
id , _ := strconv .Atoi (req .FormValue ("id" ))
79
79
if id >= len (_config .Cmds ) {
80
- writeString (w , "Invalid Command." )
80
+ writeString (ws , "Invalid Command." )
81
81
return
82
82
}
83
- exec_cmd (id , w )
83
+
84
+ //ws.SetWriteDeadline(time.Now().Add(20 * time.Second))
85
+ exec_cmd (id , ws )
84
86
}
85
87
86
88
type Cmd struct {
@@ -107,9 +109,8 @@ func main() {
107
109
port = _config .Port
108
110
_html = strings .Replace (HTML_EXEC , "{port}" , strconv .Itoa (port ), - 1 )
109
111
http .HandleFunc ("/run" , showCmdListPage )
110
- //http.HandleFunc("/run/cmd", showCmdResultInitPage)
111
- //http.Handle("/run/cmd", websocket.Handler(execAndRefreshCmdResult))
112
- http .HandleFunc ("/run/cmd" , execAndRefreshCmdResult )
112
+ http .HandleFunc ("/run/cmd" , showCmdResultInitPage )
113
+ http .Handle ("/run/exec" , websocket .Handler (execAndRefreshCmdResult ))
113
114
http .Handle ("/" , http .FileServer (http .Dir (_config .WWWRoot ))) //use fileserver directly
114
115
fmt .Printf ("http://localhost:%d/run\n " , port )
115
116
err := http .ListenAndServe (fmt .Sprintf (":%d" , port ), nil )
@@ -140,7 +141,7 @@ function init() {
140
141
//ws.send("ok");
141
142
};
142
143
ws.onmessage = function (e) {
143
- div.innerText = div.innerText + e.data + "\n" ;
144
+ div.innerText = div.innerText + e.data;
144
145
};
145
146
ws.onclose = function (e) {
146
147
// div.innerText = div.innerText + "closed";
0 commit comments