Commit 30cddf73 by lichenggang

解决多客户端初始访问的问题

parent e907d7c6
Showing with 10 additions and 9 deletions
......@@ -9,7 +9,7 @@ import (
"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)
file, err := os.Open(file_path)
if err != nil {
......
......@@ -122,7 +122,8 @@ func server(port int, directory string) {
// 创建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)}
cli := &client{time.Now().String(), ws, make(chan []byte, 1024), ""}
queryform, err := url.ParseQuery(ws.Request().URL.RawQuery)
if err != nil {
log.Fatal(err)
......@@ -138,13 +139,12 @@ func gen(directory string, file_map map[string]map[client]bool) func(ws *websock
if err != nil {
return
}
lastcontent_msg, err := return_byte(file_path, 500)
lastcontent_msg, err := return_init_str(file_path, 500)
if err != nil {
log.Fatal(err)
return
}
all_content := filename + "$$" + lastcontent_msg
manager.broadcast <- []byte(all_content)
cli.initstr = lastcontent_msg
// 监控文件
if _, ok := file_map[filename]; ok {
......
......@@ -9,9 +9,10 @@ import (
// websocket客户端
type client struct {
id string
socket *websocket.Conn
send chan []byte
id string
socket *websocket.Conn
send chan []byte
initstr string
}
// 客户端管理
......@@ -90,7 +91,7 @@ func (c *client) write() {
// manager.unregister <- c
// c.socket.Close()
//}()
c.socket.Write([]byte(c.initstr))
for {
select {
case message, ok := <-c.send:
......
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