yanic/database/socket/internal.go

36 lines
683 B
Go
Raw Permalink Normal View History

2017-04-27 19:09:46 +00:00
package socket
import (
"encoding/json"
2017-04-27 20:28:31 +00:00
"log"
2017-04-27 19:09:46 +00:00
"net"
)
2017-04-27 20:44:06 +00:00
func (conn *Connection) handleSocketConnection(ln net.Listener) {
2017-04-27 19:09:46 +00:00
for {
c, err := ln.Accept()
if err != nil {
2017-04-27 20:28:31 +00:00
log.Println("[socket-database] error during connection of a client", err)
2017-04-27 19:09:46 +00:00
continue
}
2017-04-27 20:44:06 +00:00
conn.clientMux.Lock()
conn.clients[c.RemoteAddr()] = c
conn.clientMux.Unlock()
2017-04-27 19:09:46 +00:00
}
}
2017-05-29 19:33:51 +00:00
func (conn *Connection) sendJSON(msg Message) {
2017-04-27 20:44:06 +00:00
conn.clientMux.Lock()
2017-04-27 20:28:31 +00:00
for addr, c := range conn.clients {
2017-04-27 19:09:46 +00:00
d := json.NewEncoder(c)
err := d.Encode(&msg)
if err != nil {
2017-04-27 20:28:31 +00:00
log.Println("[socket-database] client has not recieve event:", err)
2017-04-27 19:45:44 +00:00
c.Close()
2017-04-27 20:28:31 +00:00
delete(conn.clients, addr)
2017-04-27 19:09:46 +00:00
}
}
2017-04-27 20:44:06 +00:00
conn.clientMux.Unlock()
2017-04-27 19:09:46 +00:00
}