Move JSON encoding to conn.send()

This commit is contained in:
Ken-Håvard Lieng 2015-06-07 06:32:19 +02:00
parent c6c740e24b
commit 3d20edd960
3 changed files with 9 additions and 14 deletions

View File

@ -9,14 +9,14 @@ import (
type conn struct {
conn *websocket.Conn
in chan WSRequest
out chan []byte
out chan WSResponse
}
func newConn(ws *websocket.Conn) *conn {
return &conn{
conn: ws,
in: make(chan WSRequest, 32),
out: make(chan []byte, 32),
out: make(chan WSResponse, 32),
}
}
@ -26,12 +26,12 @@ func (c *conn) send() {
for {
select {
case msg, ok := <-c.out:
case res, ok := <-c.out:
if !ok {
return
}
err = c.conn.WriteMessage(websocket.TextMessage, msg)
err = c.conn.WriteJSON(res)
case <-ping:
err = c.conn.WriteJSON(WSResponse{Type: "ping"})

View File

@ -13,7 +13,7 @@ type WSRequest struct {
type WSResponse struct {
Type string `json:"type"`
Response *json.RawMessage `json:"response"`
Response interface{} `json:"response"`
}
type Connect struct {

View File

@ -1,7 +1,6 @@
package server
import (
"encoding/json"
"sync"
"github.com/khlieng/name_pending/irc"
@ -14,7 +13,7 @@ type Session struct {
ws map[string]*conn
wsLock sync.Mutex
out chan []byte
out chan WSResponse
user *storage.User
}
@ -23,7 +22,7 @@ func NewSession() *Session {
return &Session{
irc: make(map[string]*irc.Client),
ws: make(map[string]*conn),
out: make(chan []byte, 32),
out: make(chan WSResponse, 32),
}
}
@ -68,11 +67,7 @@ func (s *Session) deleteWS(addr string) {
}
func (s *Session) sendJSON(t string, v interface{}) {
data, _ := json.Marshal(v)
raw := json.RawMessage(data)
res, _ := json.Marshal(WSResponse{Type: t, Response: &raw})
s.out <- res
s.out <- WSResponse{t, v}
}
func (s *Session) sendError(err error, server string) {