Commit 830d16ca by lichenggang

websocket从x/net/websocket切换到gorilla/websocket

parent 1ae14b4b
......@@ -3,7 +3,7 @@ package code
import (
"bytes"
"fmt"
"golang.org/x/net/websocket"
"github.com/gorilla/websocket"
"html/template"
"io/ioutil"
"log"
......@@ -60,7 +60,8 @@ func server(port int, directory string) {
// 返回页面
http.HandleFunc("/index", webhandler.indexhandle)
// socket链接
http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap)))
//http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap)))
http.HandleFunc("/ws", genHand(directory, manager.filemap))
go manager.start()
......@@ -71,13 +72,23 @@ func server(port int, directory string) {
//log.Fatal()
}
// 创建client对象
func gen(directory string, file_map map[string]map[Client]bool) func(ws *websocket.Conn) {
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
f := func(ws *websocket.Conn) {
cli := &Client{time.Now().String(), ws, make(chan []byte, 1024), ""}
func genHand(directory string, file_map map[string]map[Client]bool) func(http.ResponseWriter, *http.Request) {
f := func(w http.ResponseWriter, r *http.Request) {
queryform, err := url.ParseQuery(ws.Request().URL.RawQuery)
// 完成ws协议的握手操作
// Upgrade:websocket
ws, err := upGrader.Upgrade(w, r, nil)
if err != nil {
return
}
cli := &Client{time.Now().String(), ws, make(chan []byte, 1024), ""}
queryform, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
log.Fatal(err)
}
......@@ -119,3 +130,52 @@ func gen(directory string, file_map map[string]map[Client]bool) func(ws *websock
}
return f
}
// 创建client对象
//func gen(directory string, file_map map[string]map[Client]bool) func(ws *websocket.Conn) {
//
// f := func(ws *websocket.Conn) {
// cli := &Client{time.Now().String(), ws, make(chan []byte, 1024), ""}
//
// queryform, err := url.ParseQuery(ws.Request().URL.RawQuery)
// if err != nil {
// log.Fatal(err)
// }
// filename := queryform["file"][0]
//
// //初始监听时加载最后500行全部内容
// file_path := directory
// for _, s := range strings.Split(filename, "~~") {
// file_path += "/" + s
// }
// _, err = os.Stat(file_path)
// if err != nil {
// return
// }
// lastcontent_msg, err := return_init_str(file_path, 500)
// if err != nil {
// log.Fatal(err)
// return
// }
// cli.Initstr = lastcontent_msg
//
// // 监控文件
// if _, ok := file_map[filename]; ok {
// file_map[filename][*cli] = true
// fmt.Printf("%s has been monitored\n", filename)
// } else {
// go monitor(file_path, filename)
// l := make(map[Client]bool)
// l[*cli] = true
// file_map[filename] = l
// fmt.Printf("Add a new monitored file [%s]\n", filename)
//
// }
//
// //注册client并开始传输信息
// manager.register <- cli
// go cli.read()
// cli.write()
// }
// return f
//}
......@@ -2,6 +2,7 @@ package code
import (
"fmt"
"github.com/kataras/iris/v12"
"log"
"net/http"
"net/url"
......@@ -63,49 +64,49 @@ func (wbh *WebHandler) indexhandle(w http.ResponseWriter, r *http.Request) {
}
//func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
// innerip := ctx.URLParam("innerip")
// server_direc := ctx.URLParam("server_direc")
// file := ctx.URLParam("file")
//
// if innerip != "" {
// ip_files, err := get_files(wbh.Directory + "/" + innerip)
//
// if err != nil {
// return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip)
// return
// }
// if server_direc != "" {
// ip_server_files, err := get_files(wbh.Directory + "/" + innerip + "/" + server_direc)
// if err != nil {
// return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip+"/"+server_direc)
// return
// }
// if file != "" {
// str_filepath := innerip + "~~" + server_direc + "~~" + file
// filename := innerip + "/" + server_direc + "/" + file
//
// _, err := os.Stat(wbh.Directory + "/" + filename)
// if err != nil {
// return_error_res(ctx.ResponseWriter(), filename)
// return
// }
// fmt.Printf("view log [%s]\n", file)
// return_res(ctx.ResponseWriter(), "web/views/page.html", str_filepath)
// } else {
// return_res(ctx.ResponseWriter(), "web/views/server_direc.html", strings.Join(ip_server_files, "~~"))
// }
// } else {
//
// return_res(ctx.ResponseWriter(), "web/views/innnerip.html", strings.Join(ip_files, "~~"))
// }
// } else {
// ips, err := get_files(wbh.Directory)
// if err != nil {
// return_error_res(ctx.ResponseWriter(), wbh.Directory)
// return
// }
// return_res(ctx.ResponseWriter(), "web/views/index.html", strings.Join(ips, "~~"))
// }
//
//}
func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
innerip := ctx.URLParam("innerip")
server_direc := ctx.URLParam("server_direc")
file := ctx.URLParam("file")
if innerip != "" {
ip_files, err := get_files(wbh.Directory + "/" + innerip)
if err != nil {
return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip)
return
}
if server_direc != "" {
ip_server_files, err := get_files(wbh.Directory + "/" + innerip + "/" + server_direc)
if err != nil {
return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip+"/"+server_direc)
return
}
if file != "" {
str_filepath := innerip + "~~" + server_direc + "~~" + file
filename := innerip + "/" + server_direc + "/" + file
_, err := os.Stat(wbh.Directory + "/" + filename)
if err != nil {
return_error_res(ctx.ResponseWriter(), filename)
return
}
fmt.Printf("view log [%s]\n", file)
return_res(ctx.ResponseWriter(), "web/views/page.html", str_filepath)
} else {
return_res(ctx.ResponseWriter(), "web/views/server_direc.html", strings.Join(ip_server_files, "~~"))
}
} else {
return_res(ctx.ResponseWriter(), "web/views/innnerip.html", strings.Join(ip_files, "~~"))
}
} else {
ips, err := get_files(wbh.Directory)
if err != nil {
return_error_res(ctx.ResponseWriter(), wbh.Directory)
return
}
return_res(ctx.ResponseWriter(), "web/views/index.html", strings.Join(ips, "~~"))
}
}
......@@ -2,7 +2,7 @@ package code
import (
"fmt"
"golang.org/x/net/websocket"
"github.com/gorilla/websocket"
"log"
"strings"
)
......@@ -91,15 +91,17 @@ func (c *Client) write() {
// manager.unregister <- c
// c.Socket.Close()
//}()
c.Socket.Write([]byte(c.Initstr))
if err := c.Socket.WriteMessage(websocket.TextMessage, []byte(c.Initstr)); err != nil {
return
}
for {
select {
case message, ok := <-c.Send:
if !ok {
c.Socket.WriteClose(1)
c.Socket.WriteMessage(websocket.TextMessage, message)
return
}
c.Socket.Write(message)
c.Socket.WriteMessage(websocket.TextMessage, message)
}
}
}
......@@ -110,19 +112,19 @@ func (c *Client) read() {
}()
//
for {
var reply string
if err := websocket.Message.Receive(c.Socket, &reply); err != nil {
_, reply, err := c.Socket.ReadMessage()
if err != nil {
fmt.Println("Error! Can't receive message...")
break
}
if reply != "heart" {
if string(reply) != "heart" {
manager.unregister <- c
c.Socket.Close()
} else {
fmt.Println("heart package...")
}
reply = ""
//reply = []byte
//time.Sleep(2000 * time.Millisecond)
//jsonMessage, _ := json.Marshal(&Message{Sender: c.Id, Content: string(message)})
......
......@@ -2,4 +2,31 @@ module web_log
go 1.14
require golang.org/x/net v0.0.0-20201216054612-986b41b23924
require (
github.com/CloudyKit/jet/v3 v3.0.1 // indirect
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/gorilla/websocket v1.4.2
github.com/iris-contrib/jade v1.1.4 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/kataras/golog v0.1.6 // indirect
github.com/kataras/iris/v12 v12.1.8
github.com/klauspost/compress v1.11.4 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/microcosm-cc/bluemonday v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/ryanuber/columnize v2.1.2+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/ugorji/go v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 // indirect
golang.org/x/text v0.3.4 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment