yanic/database/socket/internal.go

34 lines
540 B
Go
Raw Normal View History

2017-04-27 19:09:46 +00:00
package socket
import (
"encoding/json"
"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 {
continue
}
config.clients[c.RemoteAddr()] = c
}
}
func (conn *Connection) sendJSON(msg EventMessage) {
for i, c := range conn.clients {
d := json.NewEncoder(c)
err := d.Encode(&msg)
if err != nil {
2017-04-27 19:45:44 +00:00
c.Close()
2017-04-27 19:09:46 +00:00
delete(conn.clients, i)
}
}
}