fix some nil basic on connect

This commit is contained in:
Martin Geno 2017-07-06 12:17:10 +02:00
parent fbd01cf7ab
commit b921e6cdee
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
1 changed files with 12 additions and 5 deletions

View File

@ -54,7 +54,9 @@ func (c *Client) Close() {
// Listen Write and Read request via chanel // Listen Write and Read request via chanel
func (c *Client) Listen() { func (c *Client) Listen() {
go c.listenWrite() go c.listenWrite()
if stats != nil {
c.Write(&Message{Type: MessageTypeStats, Body: stats}) c.Write(&Message{Type: MessageTypeStats, Body: stats})
}
c.publishAllData() c.publishAllData()
c.listenRead() c.listenRead()
} }
@ -66,9 +68,11 @@ func (c *Client) publishAllData() {
for _, node := range nodes.Current { for _, node := range nodes.Current {
c.Write(&Message{Type: MessageTypeCurrentNode, Node: node}) c.Write(&Message{Type: MessageTypeCurrentNode, Node: node})
} }
if commands != nil {
for _, cmd := range commands.List { for _, cmd := range commands.List {
c.Write(&Message{Type: MessageTypeCommand, Command: cmd}) c.Write(&Message{Type: MessageTypeCommand, Command: cmd})
} }
}
} }
func (c *Client) handleMessage(msg *Message) { func (c *Client) handleMessage(msg *Message) {
@ -77,6 +81,9 @@ func (c *Client) handleMessage(msg *Message) {
nodes.UpdateNode(msg.Node) nodes.UpdateNode(msg.Node)
break break
case MessageTypeCommand: case MessageTypeCommand:
if commands == nil {
break
}
cmd := commands.AddCommand(msg.Command) cmd := commands.AddCommand(msg.Command)
w := worker.NewWorker(time.Millisecond*300, func() { w := worker.NewWorker(time.Millisecond*300, func() {
SendAll(Message{Type: MessageTypeCommand, Command: cmd}) SendAll(Message{Type: MessageTypeCommand, Command: cmd})
@ -98,9 +105,9 @@ func (c *Client) listenWrite() {
websocket.JSON.Send(c.ws, msg) websocket.JSON.Send(c.ws, msg)
case <-c.writeQuit: case <-c.writeQuit:
clientsMutex.Lock()
close(c.ch) close(c.ch)
close(c.writeQuit) close(c.writeQuit)
clientsMutex.Lock()
delete(clients, c.ip) delete(clients, c.ip)
clientsMutex.Unlock() clientsMutex.Unlock()
return return
@ -114,8 +121,8 @@ func (c *Client) listenRead() {
select { select {
case <-c.readQuit: case <-c.readQuit:
close(c.readQuit)
clientsMutex.Lock() clientsMutex.Lock()
close(c.readQuit)
delete(clients, c.ip) delete(clients, c.ip)
clientsMutex.Unlock() clientsMutex.Unlock()
return return