Move JSON encoding to conn.send()
This commit is contained in:
parent
c6c740e24b
commit
3d20edd960
@ -9,14 +9,14 @@ import (
|
|||||||
type conn struct {
|
type conn struct {
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
in chan WSRequest
|
in chan WSRequest
|
||||||
out chan []byte
|
out chan WSResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConn(ws *websocket.Conn) *conn {
|
func newConn(ws *websocket.Conn) *conn {
|
||||||
return &conn{
|
return &conn{
|
||||||
conn: ws,
|
conn: ws,
|
||||||
in: make(chan WSRequest, 32),
|
in: make(chan WSRequest, 32),
|
||||||
out: make(chan []byte, 32),
|
out: make(chan WSResponse, 32),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,12 +26,12 @@ func (c *conn) send() {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg, ok := <-c.out:
|
case res, ok := <-c.out:
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.conn.WriteMessage(websocket.TextMessage, msg)
|
err = c.conn.WriteJSON(res)
|
||||||
|
|
||||||
case <-ping:
|
case <-ping:
|
||||||
err = c.conn.WriteJSON(WSResponse{Type: "ping"})
|
err = c.conn.WriteJSON(WSResponse{Type: "ping"})
|
||||||
|
@ -12,8 +12,8 @@ type WSRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WSResponse struct {
|
type WSResponse struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Response *json.RawMessage `json:"response"`
|
Response interface{} `json:"response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Connect struct {
|
type Connect struct {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/khlieng/name_pending/irc"
|
"github.com/khlieng/name_pending/irc"
|
||||||
@ -14,7 +13,7 @@ type Session struct {
|
|||||||
|
|
||||||
ws map[string]*conn
|
ws map[string]*conn
|
||||||
wsLock sync.Mutex
|
wsLock sync.Mutex
|
||||||
out chan []byte
|
out chan WSResponse
|
||||||
|
|
||||||
user *storage.User
|
user *storage.User
|
||||||
}
|
}
|
||||||
@ -23,7 +22,7 @@ func NewSession() *Session {
|
|||||||
return &Session{
|
return &Session{
|
||||||
irc: make(map[string]*irc.Client),
|
irc: make(map[string]*irc.Client),
|
||||||
ws: make(map[string]*conn),
|
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{}) {
|
func (s *Session) sendJSON(t string, v interface{}) {
|
||||||
data, _ := json.Marshal(v)
|
s.out <- WSResponse{t, v}
|
||||||
raw := json.RawMessage(data)
|
|
||||||
res, _ := json.Marshal(WSResponse{Type: t, Response: &raw})
|
|
||||||
|
|
||||||
s.out <- res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) sendError(err error, server string) {
|
func (s *Session) sendError(err error, server string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user