yanic/database/socket/internal.go

37 lines
701 B
Go
Raw Normal View History

2017-04-27 21:09:46 +02:00
package socket
import (
"encoding/json"
2017-04-27 22:28:31 +02:00
"log"
2017-04-27 21:09:46 +02:00
"net"
)
type EventMessage struct {
Event string `json:"event"`
Body interface{} `json:"body,omitempty"`
}
func (config *Connection) handleSocketConnection(ln net.Listener) {
for {
c, err := ln.Accept()
if err != nil {
2017-04-27 22:28:31 +02:00
log.Println("[socket-database] error during connection of a client", err)
2017-04-27 21:09:46 +02:00
continue
}
config.clients[c.RemoteAddr()] = c
}
}
func (conn *Connection) sendJSON(msg EventMessage) {
2017-04-27 22:28:31 +02:00
for addr, c := range conn.clients {
2017-04-27 21:09:46 +02:00
d := json.NewEncoder(c)
err := d.Encode(&msg)
if err != nil {
2017-04-27 22:28:31 +02:00
log.Println("[socket-database] client has not recieve event:", err)
2017-04-27 21:45:44 +02:00
c.Close()
2017-04-27 22:28:31 +02:00
delete(conn.clients, addr)
2017-04-27 21:09:46 +02:00
}
}
}