Commit 4da715ae by lichenggang

完成多文件多客户端的监听, 还差初始展示文件的全部内容

parent 7bd743c9
......@@ -4,7 +4,7 @@ import "code"
const (
Directory = "E:\\project\\see_log\\" // directory of the log
TestPort = 9998
TestPort = 9997
)
// example
......
......@@ -3,6 +3,7 @@ package code
import (
"log"
"os"
"strings"
"time"
)
......@@ -13,7 +14,8 @@ func monitor(filePath string) {
log.Printf("[seelog] error:%+v", err)
}
}()
tmp := strings.Split(filePath, "\\") //todo linux下需要改动
filename := tmp[len(tmp)-1]
var fileInfo os.FileInfo
var err error
for i := 1; i <= 10; i++ {
......@@ -26,6 +28,8 @@ func monitor(filePath string) {
}
offset := fileInfo.Size()
//allready_msg := make([]byte, offset)
for {
fileInfo, err = os.Stat(filePath)
if err != nil {
......@@ -49,7 +53,10 @@ func monitor(filePath string) {
if err != nil {
log.Printf("[seelog] error:%v", err.Error())
}
manager.broadcast <- msg
str_msg := string(msg)
s := filename + "$$" + str_msg
manager.broadcast <- []byte(s)
offset = newOffset
file.Close()
}
......
......@@ -14,7 +14,7 @@ func See(filePath string, port int) {
return
}
//开启socket管理器
go manager.start()
//go manager.start()
go server(port, filePath)
wg.Wait()
......
......@@ -23,7 +23,6 @@ func server(port int, directory string) {
log.Printf("[seelog] error:%+v", err)
}
}()
file_map := make(map[string]struct{})
// 返回页面
//http.HandleFunc("/log",page)
......@@ -53,29 +52,20 @@ func server(port int, directory string) {
w.Write([]byte(ct.String()))
return
}
// 监控文件
if _, ok := file_map[filename]; ok {
fmt.Printf("%s 已经被监控\n", filename)
} else {
go monitor(directory + filename)
file_map[filename] = struct{}{}
}
fmt.Println(filename)
fmt.Printf("view log [%s]\n", filename)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
t, err := template.ParseFiles("src/templates/page.html")
tmpl, err := template.ParseFiles("src/templates/page.html")
if err != nil {
log.Println("err:", err)
return
}
t.Execute(w, nil)
tmpl.Execute(w, filename)
})
// socket链接
http.Handle("/ws", websocket.Handler(genConn))
http.Handle("/ws", websocket.Handler(gen(directory, manager.filemap)))
// 测试
http.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) {
t, err := template.ParseFiles("index.html")
......@@ -85,6 +75,9 @@ func server(port int, directory string) {
t.Execute(writer, nil)
})
var bt bytes.Buffer
go manager.start()
bt.WriteString("127.0.0.1:")
bt.WriteString(strconv.Itoa(port))
http.ListenAndServe(bt.String(), nil)
......@@ -92,9 +85,31 @@ func server(port int, directory string) {
}
// 创建client对象
func genConn(ws *websocket.Conn) {
client := &client{time.Now().String(), ws, make(chan []byte, 1024)}
manager.register <- client
go client.read()
client.write()
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]
// 监控文件
if _, ok := file_map[filename]; ok {
file_map[filename][*cli] = true
fmt.Printf("%s has been monitored\n", filename)
} else {
go monitor(directory + filename)
l := make(map[client]bool)
l[*cli] = true
file_map[filename] = l
fmt.Printf("Add a new monitored file [%s]\n", filename)
}
manager.register <- cli
go cli.read()
cli.write()
}
return f
}
......@@ -5,6 +5,7 @@ import (
"golang.org/x/net/websocket"
"log"
"os"
"strings"
"time"
)
......@@ -21,6 +22,7 @@ type clientManager struct {
broadcast chan []byte
register chan *client
unregister chan *client
filemap map[string]map[client]bool
}
var manager = clientManager{
......@@ -28,8 +30,13 @@ var manager = clientManager{
register: make(chan *client),
unregister: make(chan *client),
clients: make(map[*client]bool),
filemap: getfilemap(),
}
func getfilemap() map[string]map[client]bool {
file_map := make(map[string]map[client]bool)
return file_map
}
func (manager *clientManager) start() {
defer func() {
if err := recover(); err != nil {
......@@ -44,21 +51,37 @@ func (manager *clientManager) start() {
fmt.Printf("A client connect, current clients count is %d \n", len(manager.clients))
case conn := <-manager.unregister:
if _, ok := manager.clients[conn]; ok {
close(conn.send)
delete(manager.clients, conn)
for _, map_tmp := range manager.filemap {
delete(map_tmp, *conn)
}
fmt.Printf("A client disconnect, current clients count is %d \n", len(manager.clients))
}
case message := <-manager.broadcast:
for conn := range manager.clients {
str_msg := string(message)
tmp := strings.Split(str_msg, "$$")
filename := tmp[0]
msg := tmp[1]
map_tmp := manager.filemap[filename]
for conn, _ := range map_tmp {
select {
case conn.send <- message:
case conn.send <- []byte(msg):
default:
close(conn.send)
delete(manager.clients, conn)
delete(manager.clients, &conn)
}
}
//for conn := range manager.clients {
// select {
// case conn.send <- message:
// default:
// close(conn.send)
// delete(manager.clients, conn)
// }
//}
}
}
}
......
......@@ -14,7 +14,7 @@
var filterText = ""
function connect (){
var ws = new WebSocket("ws://"+ window.location.host +"/ws");
var ws = new WebSocket("ws://"+ window.location.host +"/ws?file={{ . }}");
ws.onmessage = function(e) {
console.log(filterText)
if (out && (filterText == "" || e.data.indexOf(filterText) != -1)){
......
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