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
830d16ca
authored
Dec 24, 2020
by
lichenggang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
websocket从x/net/websocket切换到gorilla/websocket
parent
1ae14b4b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
62 deletions
code/server.go
code/webloghandler.go
code/ws.go
go.mod
code/server.go
View file @
830d16ca
...
@@ -3,7 +3,7 @@ package code
...
@@ -3,7 +3,7 @@ package code
import
(
import
(
"bytes"
"bytes"
"fmt"
"fmt"
"g
olang.org/x/net
/websocket"
"g
ithub.com/gorilla
/websocket"
"html/template"
"html/template"
"io/ioutil"
"io/ioutil"
"log"
"log"
...
@@ -60,7 +60,8 @@ func server(port int, directory string) {
...
@@ -60,7 +60,8 @@ func server(port int, directory string) {
// 返回页面
// 返回页面
http
.
HandleFunc
(
"/index"
,
webhandler
.
indexhandle
)
http
.
HandleFunc
(
"/index"
,
webhandler
.
indexhandle
)
// socket链接
// 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
()
go
manager
.
start
()
...
@@ -71,13 +72,23 @@ func server(port int, directory string) {
...
@@ -71,13 +72,23 @@ func server(port int, directory string) {
//log.Fatal()
//log.Fatal()
}
}
// 创建client对象
var
upGrader
=
websocket
.
Upgrader
{
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
Client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
CheckOrigin
:
func
(
r
*
http
.
Request
)
bool
{
return
true
},
}
f
:=
func
(
ws
*
websocket
.
Conn
)
{
func
genHand
(
directory
string
,
file_map
map
[
string
]
map
[
Client
]
bool
)
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
{
cli
:=
&
Client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
),
""
}
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
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
...
@@ -119,3 +130,52 @@ func gen(directory string, file_map map[string]map[Client]bool) func(ws *websock
...
@@ -119,3 +130,52 @@ func gen(directory string, file_map map[string]map[Client]bool) func(ws *websock
}
}
return
f
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
//}
code/webloghandler.go
View file @
830d16ca
...
@@ -2,6 +2,7 @@ package code
...
@@ -2,6 +2,7 @@ package code
import
(
import
(
"fmt"
"fmt"
"github.com/kataras/iris/v12"
"log"
"log"
"net/http"
"net/http"
"net/url"
"net/url"
...
@@ -63,49 +64,49 @@ func (wbh *WebHandler) indexhandle(w http.ResponseWriter, r *http.Request) {
...
@@ -63,49 +64,49 @@ func (wbh *WebHandler) indexhandle(w http.ResponseWriter, r *http.Request) {
}
}
//
func (wbh *WebHandler) Indexhandle2(ctx iris.Context) {
func
(
wbh
*
WebHandler
)
Indexhandle2
(
ctx
iris
.
Context
)
{
//
innerip := ctx.URLParam("innerip")
innerip
:=
ctx
.
URLParam
(
"innerip"
)
//
server_direc := ctx.URLParam("server_direc")
server_direc
:=
ctx
.
URLParam
(
"server_direc"
)
//
file := ctx.URLParam("file")
file
:=
ctx
.
URLParam
(
"file"
)
//
//
if innerip != "" {
if
innerip
!=
""
{
//
ip_files, err := get_files(wbh.Directory + "/" + innerip)
ip_files
,
err
:=
get_files
(
wbh
.
Directory
+
"/"
+
innerip
)
//
//
if err != nil {
if
err
!=
nil
{
//
return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip)
return_error_res
(
ctx
.
ResponseWriter
(),
wbh
.
Directory
+
"/"
+
innerip
)
//
return
return
//
}
}
//
if server_direc != "" {
if
server_direc
!=
""
{
//
ip_server_files, err := get_files(wbh.Directory + "/" + innerip + "/" + server_direc)
ip_server_files
,
err
:=
get_files
(
wbh
.
Directory
+
"/"
+
innerip
+
"/"
+
server_direc
)
//
if err != nil {
if
err
!=
nil
{
//
return_error_res(ctx.ResponseWriter(), wbh.Directory+"/"+innerip+"/"+server_direc)
return_error_res
(
ctx
.
ResponseWriter
(),
wbh
.
Directory
+
"/"
+
innerip
+
"/"
+
server_direc
)
//
return
return
//
}
}
//
if file != "" {
if
file
!=
""
{
//
str_filepath := innerip + "~~" + server_direc + "~~" + file
str_filepath
:=
innerip
+
"~~"
+
server_direc
+
"~~"
+
file
//
filename := innerip + "/" + server_direc + "/" + file
filename
:=
innerip
+
"/"
+
server_direc
+
"/"
+
file
//
//
_, err := os.Stat(wbh.Directory + "/" + filename)
_
,
err
:=
os
.
Stat
(
wbh
.
Directory
+
"/"
+
filename
)
//
if err != nil {
if
err
!=
nil
{
//
return_error_res(ctx.ResponseWriter(), filename)
return_error_res
(
ctx
.
ResponseWriter
(),
filename
)
//
return
return
//
}
}
//
fmt.Printf("view log [%s]\n", file)
fmt
.
Printf
(
"view log [%s]
\n
"
,
file
)
//
return_res(ctx.ResponseWriter(), "web/views/page.html", str_filepath)
return_res
(
ctx
.
ResponseWriter
(),
"web/views/page.html"
,
str_filepath
)
//
} else {
}
else
{
//
return_res(ctx.ResponseWriter(), "web/views/server_direc.html", strings.Join(ip_server_files, "~~"))
return_res
(
ctx
.
ResponseWriter
(),
"web/views/server_direc.html"
,
strings
.
Join
(
ip_server_files
,
"~~"
))
//
}
}
//
} else {
}
else
{
//
//
return_res(ctx.ResponseWriter(), "web/views/innnerip.html", strings.Join(ip_files, "~~"))
return_res
(
ctx
.
ResponseWriter
(),
"web/views/innnerip.html"
,
strings
.
Join
(
ip_files
,
"~~"
))
//
}
}
//
} else {
}
else
{
//
ips, err := get_files(wbh.Directory)
ips
,
err
:=
get_files
(
wbh
.
Directory
)
//
if err != nil {
if
err
!=
nil
{
//
return_error_res(ctx.ResponseWriter(), wbh.Directory)
return_error_res
(
ctx
.
ResponseWriter
(),
wbh
.
Directory
)
//
return
return
//
}
}
//
return_res(ctx.ResponseWriter(), "web/views/index.html", strings.Join(ips, "~~"))
return_res
(
ctx
.
ResponseWriter
(),
"web/views/index.html"
,
strings
.
Join
(
ips
,
"~~"
))
//
}
}
//
//
}
}
code/ws.go
View file @
830d16ca
...
@@ -2,7 +2,7 @@ package code
...
@@ -2,7 +2,7 @@ package code
import
(
import
(
"fmt"
"fmt"
"g
olang.org/x/net
/websocket"
"g
ithub.com/gorilla
/websocket"
"log"
"log"
"strings"
"strings"
)
)
...
@@ -91,15 +91,17 @@ func (c *Client) write() {
...
@@ -91,15 +91,17 @@ func (c *Client) write() {
// manager.unregister <- c
// manager.unregister <- c
// c.Socket.Close()
// c.Socket.Close()
//}()
//}()
c
.
Socket
.
Write
([]
byte
(
c
.
Initstr
))
if
err
:=
c
.
Socket
.
WriteMessage
(
websocket
.
TextMessage
,
[]
byte
(
c
.
Initstr
));
err
!=
nil
{
return
}
for
{
for
{
select
{
select
{
case
message
,
ok
:=
<-
c
.
Send
:
case
message
,
ok
:=
<-
c
.
Send
:
if
!
ok
{
if
!
ok
{
c
.
Socket
.
Write
Close
(
1
)
c
.
Socket
.
Write
Message
(
websocket
.
TextMessage
,
message
)
return
return
}
}
c
.
Socket
.
Write
(
message
)
c
.
Socket
.
Write
Message
(
websocket
.
TextMessage
,
message
)
}
}
}
}
}
}
...
@@ -110,19 +112,19 @@ func (c *Client) read() {
...
@@ -110,19 +112,19 @@ func (c *Client) read() {
}()
}()
//
//
for
{
for
{
var
reply
string
_
,
reply
,
err
:=
c
.
Socket
.
ReadMessage
()
if
err
:=
websocket
.
Message
.
Receive
(
c
.
Socket
,
&
reply
);
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Error! Can't receive message..."
)
fmt
.
Println
(
"Error! Can't receive message..."
)
break
break
}
}
if
reply
!=
"heart"
{
if
string
(
reply
)
!=
"heart"
{
manager
.
unregister
<-
c
manager
.
unregister
<-
c
c
.
Socket
.
Close
()
c
.
Socket
.
Close
()
}
else
{
}
else
{
fmt
.
Println
(
"heart package..."
)
fmt
.
Println
(
"heart package..."
)
}
}
reply
=
""
//reply = []byte
//time.Sleep(2000 * time.Millisecond)
//time.Sleep(2000 * time.Millisecond)
//jsonMessage, _ := json.Marshal(&Message{Sender: c.Id, Content: string(message)})
//jsonMessage, _ := json.Marshal(&Message{Sender: c.Id, Content: string(message)})
...
...
go.mod
View file @
830d16ca
...
@@ -2,4 +2,31 @@ module web_log
...
@@ -2,4 +2,31 @@ module web_log
go 1.14
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
)
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