()
| 92 | } |
| 93 | |
| 94 | func (d *Document) AddClient() chan string { |
| 95 | ch := make(chan string, 10) |
| 96 | |
| 97 | d.mu.Lock() |
| 98 | d.Clients[ch] = struct{}{} |
| 99 | // 注册时先推一次当前内容 |
| 100 | initial := d.Content |
| 101 | d.mu.Unlock() |
| 102 | |
| 103 | // 将当前内容先发出去 |
| 104 | go func() { |
| 105 | if initial != "" { |
| 106 | ch <- initial |
| 107 | } |
| 108 | }() |
| 109 | |
| 110 | return ch |
| 111 | } |
| 112 | |
| 113 | // RemoveClient 注销客户端,并关闭通道 |
| 114 | func (d *Document) RemoveClient(ch chan string) { |