Commit 30cddf73 by lichenggang

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

parent e907d7c6
Showing with 10 additions and 9 deletions
...@@ -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 {
......
...@@ -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 {
......
...@@ -9,9 +9,10 @@ import ( ...@@ -9,9 +9,10 @@ import (
// websocket客户端 // websocket客户端
type client struct { 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:
......
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