mirror of
https://github.com/jech/galene.git
synced 2024-11-22 16:45:58 +01:00
Optimise broadcast messages.
Avoid formatting JSON multiple times.
This commit is contained in:
parent
69bb12014e
commit
7f4306ab3b
1 changed files with 32 additions and 13 deletions
|
@ -1209,12 +1209,9 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
||||||
Value: m.Value,
|
Value: m.Value,
|
||||||
}
|
}
|
||||||
if m.Dest == "" {
|
if m.Dest == "" {
|
||||||
clients := g.GetClients(nil)
|
err := broadcast(g.GetClients(nil), mm)
|
||||||
for _, cc := range clients {
|
if err != nil {
|
||||||
ccc, ok := cc.(*webClient)
|
log.Printf("broadcast(chat): %v", err)
|
||||||
if ok {
|
|
||||||
ccc.write(mm)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cc := g.GetClient(m.Dest)
|
cc := g.GetClient(m.Dest)
|
||||||
|
@ -1223,7 +1220,9 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
||||||
}
|
}
|
||||||
ccc, ok := cc.(*webClient)
|
ccc, ok := cc.(*webClient)
|
||||||
if !ok {
|
if !ok {
|
||||||
return c.error(group.UserError("this user doesn't chat"))
|
return c.error(group.UserError(
|
||||||
|
"this user doesn't chat",
|
||||||
|
))
|
||||||
}
|
}
|
||||||
ccc.write(mm)
|
ccc.write(mm)
|
||||||
}
|
}
|
||||||
|
@ -1240,12 +1239,9 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
||||||
Kind: "clearchat",
|
Kind: "clearchat",
|
||||||
Privileged: true,
|
Privileged: true,
|
||||||
}
|
}
|
||||||
clients := g.GetClients(nil)
|
err := broadcast(g.GetClients(nil), m)
|
||||||
for _, cc := range clients {
|
if err != nil {
|
||||||
cc, ok := cc.(*webClient)
|
log.Printf("broadcast(clearchat): %v", err)
|
||||||
if ok {
|
|
||||||
cc.write(m)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "lock", "unlock":
|
case "lock", "unlock":
|
||||||
if !c.permissions.Op {
|
if !c.permissions.Op {
|
||||||
|
@ -1394,6 +1390,11 @@ func clientWriter(conn *websocket.Conn, ch <-chan interface{}, done chan<- struc
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case []byte:
|
||||||
|
err := conn.WriteMessage(websocket.TextMessage, m)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
case closeMessage:
|
case closeMessage:
|
||||||
if m.data != nil {
|
if m.data != nil {
|
||||||
conn.WriteMessage(
|
conn.WriteMessage(
|
||||||
|
@ -1443,6 +1444,24 @@ func (c *webClient) write(m clientMessage) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func broadcast(cs []group.Client, m clientMessage) error {
|
||||||
|
b, err := json.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, c := range cs {
|
||||||
|
cc, ok := c.(*webClient)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case cc.writeCh <- b:
|
||||||
|
case <-cc.writerDone:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *webClient) close(data []byte) error {
|
func (c *webClient) close(data []byte) error {
|
||||||
select {
|
select {
|
||||||
case c.writeCh <- closeMessage{data}:
|
case c.writeCh <- closeMessage{data}:
|
||||||
|
|
Loading…
Reference in a new issue