Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lichenggang
/
web_log
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4da715ae
authored
Dec 22, 2020
by
lichenggang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
完成多文件多客户端的监听, 还差初始展示文件的全部内容
parent
7bd743c9
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
73 additions
and
28 deletions
main.go
src/code/monitor.go
src/code/see.go
src/code/server.go
src/code/ws.go
src/templates/page.html
main.go
View file @
4da715ae
...
@@ -4,7 +4,7 @@ import "code"
...
@@ -4,7 +4,7 @@ import "code"
const
(
const
(
Directory
=
"E:
\\
project
\\
see_log
\\
"
// directory of the log
Directory
=
"E:
\\
project
\\
see_log
\\
"
// directory of the log
TestPort
=
999
8
TestPort
=
999
7
)
)
// example
// example
...
...
src/code/monitor.go
View file @
4da715ae
...
@@ -3,6 +3,7 @@ package code
...
@@ -3,6 +3,7 @@ package code
import
(
import
(
"log"
"log"
"os"
"os"
"strings"
"time"
"time"
)
)
...
@@ -13,7 +14,8 @@ func monitor(filePath string) {
...
@@ -13,7 +14,8 @@ func monitor(filePath string) {
log
.
Printf
(
"[seelog] error:%+v"
,
err
)
log
.
Printf
(
"[seelog] error:%+v"
,
err
)
}
}
}()
}()
tmp
:=
strings
.
Split
(
filePath
,
"
\\
"
)
//todo linux下需要改动
filename
:=
tmp
[
len
(
tmp
)
-
1
]
var
fileInfo
os
.
FileInfo
var
fileInfo
os
.
FileInfo
var
err
error
var
err
error
for
i
:=
1
;
i
<=
10
;
i
++
{
for
i
:=
1
;
i
<=
10
;
i
++
{
...
@@ -26,6 +28,8 @@ func monitor(filePath string) {
...
@@ -26,6 +28,8 @@ func monitor(filePath string) {
}
}
offset
:=
fileInfo
.
Size
()
offset
:=
fileInfo
.
Size
()
//allready_msg := make([]byte, offset)
for
{
for
{
fileInfo
,
err
=
os
.
Stat
(
filePath
)
fileInfo
,
err
=
os
.
Stat
(
filePath
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -49,7 +53,10 @@ func monitor(filePath string) {
...
@@ -49,7 +53,10 @@ func monitor(filePath string) {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"[seelog] error:%v"
,
err
.
Error
())
log
.
Printf
(
"[seelog] error:%v"
,
err
.
Error
())
}
}
manager
.
broadcast
<-
msg
str_msg
:=
string
(
msg
)
s
:=
filename
+
"$$"
+
str_msg
manager
.
broadcast
<-
[]
byte
(
s
)
offset
=
newOffset
offset
=
newOffset
file
.
Close
()
file
.
Close
()
}
}
...
...
src/code/see.go
View file @
4da715ae
...
@@ -14,7 +14,7 @@ func See(filePath string, port int) {
...
@@ -14,7 +14,7 @@ func See(filePath string, port int) {
return
return
}
}
//开启socket管理器
//开启socket管理器
go
manager
.
start
()
//
go manager.start()
go
server
(
port
,
filePath
)
go
server
(
port
,
filePath
)
wg
.
Wait
()
wg
.
Wait
()
...
...
src/code/server.go
View file @
4da715ae
...
@@ -23,7 +23,6 @@ func server(port int, directory string) {
...
@@ -23,7 +23,6 @@ func server(port int, directory string) {
log
.
Printf
(
"[seelog] error:%+v"
,
err
)
log
.
Printf
(
"[seelog] error:%+v"
,
err
)
}
}
}()
}()
file_map
:=
make
(
map
[
string
]
struct
{})
// 返回页面
// 返回页面
//http.HandleFunc("/log",page)
//http.HandleFunc("/log",page)
...
@@ -53,29 +52,20 @@ func server(port int, directory string) {
...
@@ -53,29 +52,20 @@ func server(port int, directory string) {
w
.
Write
([]
byte
(
ct
.
String
()))
w
.
Write
([]
byte
(
ct
.
String
()))
return
return
}
}
fmt
.
Printf
(
"view log [%s]
\n
"
,
filename
)
// 监控文件
if
_
,
ok
:=
file_map
[
filename
];
ok
{
fmt
.
Printf
(
"%s 已经被监控
\n
"
,
filename
)
}
else
{
go
monitor
(
directory
+
filename
)
file_map
[
filename
]
=
struct
{}{}
}
fmt
.
Println
(
filename
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html"
)
w
.
WriteHeader
(
200
)
w
.
WriteHeader
(
200
)
tmpl
,
err
:=
template
.
ParseFiles
(
"src/templates/page.html"
)
t
,
err
:=
template
.
ParseFiles
(
"src/templates/page.html"
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
"err:"
,
err
)
log
.
Println
(
"err:"
,
err
)
return
return
}
}
t
.
Execute
(
w
,
nil
)
t
mpl
.
Execute
(
w
,
filename
)
})
})
// socket链接
// socket链接
http
.
Handle
(
"/ws"
,
websocket
.
Handler
(
gen
Conn
))
http
.
Handle
(
"/ws"
,
websocket
.
Handler
(
gen
(
directory
,
manager
.
filemap
)
))
// 测试
// 测试
http
.
HandleFunc
(
"/test"
,
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
http
.
HandleFunc
(
"/test"
,
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
t
,
err
:=
template
.
ParseFiles
(
"index.html"
)
t
,
err
:=
template
.
ParseFiles
(
"index.html"
)
...
@@ -85,6 +75,9 @@ func server(port int, directory string) {
...
@@ -85,6 +75,9 @@ func server(port int, directory string) {
t
.
Execute
(
writer
,
nil
)
t
.
Execute
(
writer
,
nil
)
})
})
var
bt
bytes
.
Buffer
var
bt
bytes
.
Buffer
go
manager
.
start
()
bt
.
WriteString
(
"127.0.0.1:"
)
bt
.
WriteString
(
"127.0.0.1:"
)
bt
.
WriteString
(
strconv
.
Itoa
(
port
))
bt
.
WriteString
(
strconv
.
Itoa
(
port
))
http
.
ListenAndServe
(
bt
.
String
(),
nil
)
http
.
ListenAndServe
(
bt
.
String
(),
nil
)
...
@@ -92,9 +85,31 @@ func server(port int, directory string) {
...
@@ -92,9 +85,31 @@ func server(port int, directory string) {
}
}
// 创建client对象
// 创建client对象
func
genConn
(
ws
*
websocket
.
Conn
)
{
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
client
:=
&
client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
)}
f
:=
func
(
ws
*
websocket
.
Conn
)
{
manager
.
register
<-
client
cli
:=
&
client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
)}
go
client
.
read
()
queryform
,
err
:=
url
.
ParseQuery
(
ws
.
Request
()
.
URL
.
RawQuery
)
client
.
write
()
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
}
}
src/code/ws.go
View file @
4da715ae
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"golang.org/x/net/websocket"
"golang.org/x/net/websocket"
"log"
"log"
"os"
"os"
"strings"
"time"
"time"
)
)
...
@@ -21,6 +22,7 @@ type clientManager struct {
...
@@ -21,6 +22,7 @@ type clientManager struct {
broadcast
chan
[]
byte
broadcast
chan
[]
byte
register
chan
*
client
register
chan
*
client
unregister
chan
*
client
unregister
chan
*
client
filemap
map
[
string
]
map
[
client
]
bool
}
}
var
manager
=
clientManager
{
var
manager
=
clientManager
{
...
@@ -28,8 +30,13 @@ var manager = clientManager{
...
@@ -28,8 +30,13 @@ var manager = clientManager{
register
:
make
(
chan
*
client
),
register
:
make
(
chan
*
client
),
unregister
:
make
(
chan
*
client
),
unregister
:
make
(
chan
*
client
),
clients
:
make
(
map
[
*
client
]
bool
),
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
()
{
func
(
manager
*
clientManager
)
start
()
{
defer
func
()
{
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
if
err
:=
recover
();
err
!=
nil
{
...
@@ -44,21 +51,37 @@ func (manager *clientManager) start() {
...
@@ -44,21 +51,37 @@ func (manager *clientManager) start() {
fmt
.
Printf
(
"A client connect, current clients count is %d
\n
"
,
len
(
manager
.
clients
))
fmt
.
Printf
(
"A client connect, current clients count is %d
\n
"
,
len
(
manager
.
clients
))
case
conn
:=
<-
manager
.
unregister
:
case
conn
:=
<-
manager
.
unregister
:
if
_
,
ok
:=
manager
.
clients
[
conn
];
ok
{
if
_
,
ok
:=
manager
.
clients
[
conn
];
ok
{
close
(
conn
.
send
)
close
(
conn
.
send
)
delete
(
manager
.
clients
,
conn
)
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
))
fmt
.
Printf
(
"A client disconnect, current clients count is %d
\n
"
,
len
(
manager
.
clients
))
}
}
case
message
:=
<-
manager
.
broadcast
:
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
{
select
{
case
conn
.
send
<-
message
:
case
conn
.
send
<-
[]
byte
(
msg
)
:
default
:
default
:
close
(
conn
.
send
)
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)
// }
//}
}
}
}
}
}
}
...
...
src/templates/page.html
View file @
4da715ae
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
var
filterText
=
""
var
filterText
=
""
function
connect
(){
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
)
{
ws
.
onmessage
=
function
(
e
)
{
console
.
log
(
filterText
)
console
.
log
(
filterText
)
if
(
out
&&
(
filterText
==
""
||
e
.
data
.
indexOf
(
filterText
)
!=
-
1
)){
if
(
out
&&
(
filterText
==
""
||
e
.
data
.
indexOf
(
filterText
)
!=
-
1
)){
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment