-type Subscribers = map[string]([](*websocket.Conn))
-
-type State struct {
- copyrights map[string]Copyright
- copyrightSubs map[string]([]*websocket.Conn)
- walletSubs map[string]([]*websocket.Conn)
-}
-
-func newState() *State {
- return &State {
- copyrights: make(map[string]Copyright),
- copyrightSubs: make(map[string]([]*websocket.Conn)),
- walletSubs: make(map[string]([]*websocket.Conn)),
- }
-}
-
-func subscribe(m Subscribers, k string, c *websocket.Conn) {
- var newSubs [](*websocket.Conn)
- if subs, ok := m[k]; ok {
- newSubs = append(subs, c)
- m[k] = newSubs
- } else {
- newSubs = make([](*websocket.Conn), 1)
- newSubs[0] = c
- m[k] = newSubs
- }
-}
-
-func unsubscribe(m Subscribers, k string, c *websocket.Conn) {
- var newSubs [](*websocket.Conn)
-
- if subs, ok := m[k]; ok {
- for _, sub := range subs {
- if (sub != c) {
- newSubs = append(newSubs, sub)
- }
- }
- m[k] = newSubs
- }
-}
-
-func listenConnection(state *State, c *websocket.Conn) {
- defer func() {
- c.Close()
- }()
-
- for {
- var m Message
- err := c.ReadJSON(&m)
-
- if err != nil {
- if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
- log.Printf("error: %v", err)
- }
- log.Printf("error: %v", err)
- break
- }
-
- switch m.Event {
- case "subscribe-wallets":
- for _, addr := range m.Addrs {
- subscribe((*state).walletSubs, addr, c)
- }
- default:
- log.Printf("error: Unsupported event: %v", m)
- }
- }
-}
-
-func serve(state *State, w http.ResponseWriter, r *http.Request) {
- conn, err := upgrader.Upgrade(w, r, nil)
- if err != nil {
- log.Println(err)
- return
- }
-
- go listenConnection(state, conn)
-}
-