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
30cddf73
authored
Dec 23, 2020
by
lichenggang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
解决多客户端初始访问的问题
parent
e907d7c6
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
7 additions
and
6 deletions
code/initread.go
code/server.go
code/ws.go
code/initread.go
View file @
30cddf73
...
@@ -9,7 +9,7 @@ import (
...
@@ -9,7 +9,7 @@ import (
"strings"
"strings"
)
)
func
return_
byte
(
file_path
string
,
lines_num
int
)
(
string
,
error
)
{
func
return_
init_str
(
file_path
string
,
lines_num
int
)
(
string
,
error
)
{
lines
:=
int64
(
lines_num
)
lines
:=
int64
(
lines_num
)
file
,
err
:=
os
.
Open
(
file_path
)
file
,
err
:=
os
.
Open
(
file_path
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
code/server.go
View file @
30cddf73
...
@@ -122,7 +122,8 @@ func server(port int, directory string) {
...
@@ -122,7 +122,8 @@ func server(port int, directory string) {
// 创建client对象
// 创建client对象
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
func
gen
(
directory
string
,
file_map
map
[
string
]
map
[
client
]
bool
)
func
(
ws
*
websocket
.
Conn
)
{
f
:=
func
(
ws
*
websocket
.
Conn
)
{
f
:=
func
(
ws
*
websocket
.
Conn
)
{
cli
:=
&
client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
)}
cli
:=
&
client
{
time
.
Now
()
.
String
(),
ws
,
make
(
chan
[]
byte
,
1024
),
""
}
queryform
,
err
:=
url
.
ParseQuery
(
ws
.
Request
()
.
URL
.
RawQuery
)
queryform
,
err
:=
url
.
ParseQuery
(
ws
.
Request
()
.
URL
.
RawQuery
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
...
@@ -138,13 +139,12 @@ func gen(directory string, file_map map[string]map[client]bool) func(ws *websock
...
@@ -138,13 +139,12 @@ func gen(directory string, file_map map[string]map[client]bool) func(ws *websock
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
lastcontent_msg
,
err
:=
return_
byte
(
file_path
,
500
)
lastcontent_msg
,
err
:=
return_
init_str
(
file_path
,
500
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
return
return
}
}
all_content
:=
filename
+
"$$"
+
lastcontent_msg
cli
.
initstr
=
lastcontent_msg
manager
.
broadcast
<-
[]
byte
(
all_content
)
// 监控文件
// 监控文件
if
_
,
ok
:=
file_map
[
filename
];
ok
{
if
_
,
ok
:=
file_map
[
filename
];
ok
{
...
...
code/ws.go
View file @
30cddf73
...
@@ -12,6 +12,7 @@ type client struct {
...
@@ -12,6 +12,7 @@ type client struct {
id
string
id
string
socket
*
websocket
.
Conn
socket
*
websocket
.
Conn
send
chan
[]
byte
send
chan
[]
byte
initstr
string
}
}
// 客户端管理
// 客户端管理
...
@@ -90,7 +91,7 @@ func (c *client) write() {
...
@@ -90,7 +91,7 @@ func (c *client) write() {
// manager.unregister <- c
// manager.unregister <- c
// c.socket.Close()
// c.socket.Close()
//}()
//}()
c
.
socket
.
Write
([]
byte
(
c
.
initstr
))
for
{
for
{
select
{
select
{
case
message
,
ok
:=
<-
c
.
send
:
case
message
,
ok
:=
<-
c
.
send
:
...
...
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