mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-12-23 08:40:18 +01:00
Use more modern function channel approach in websocket forwarder.
This commit is contained in:
parent
da2b327985
commit
25e62b3a8b
@ -15,10 +15,8 @@ import (
|
||||
|
||||
type websocketForwarder struct {
|
||||
upgrader *websocket.Upgrader
|
||||
register chan *connection
|
||||
unregister chan *connection
|
||||
broadcast chan msg
|
||||
connections map[*connection]bool
|
||||
connections map[*connection]struct{}
|
||||
funcs chan func(*websocketForwarder)
|
||||
}
|
||||
|
||||
type connection struct {
|
||||
@ -32,40 +30,58 @@ type msg struct {
|
||||
}
|
||||
|
||||
func newWebsocketForwarder() *websocketForwarder {
|
||||
upgrader := &websocket.Upgrader{ReadBufferSize: 512, WriteBufferSize: 2048}
|
||||
upgrader := &websocket.Upgrader{
|
||||
ReadBufferSize: 512,
|
||||
WriteBufferSize: 2048,
|
||||
//CheckOrigin: func(*http.Request) bool { return true },
|
||||
}
|
||||
return &websocketForwarder{
|
||||
upgrader: upgrader,
|
||||
register: make(chan *connection),
|
||||
unregister: make(chan *connection),
|
||||
broadcast: make(chan msg),
|
||||
connections: make(map[*connection]bool)}
|
||||
connections: make(map[*connection]struct{}),
|
||||
funcs: make(chan func(*websocketForwarder)),
|
||||
}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) run() {
|
||||
for {
|
||||
select {
|
||||
case c := <-wsf.register:
|
||||
wsf.connections[c] = true
|
||||
case c := <-wsf.unregister:
|
||||
for fn := range wsf.funcs {
|
||||
fn(wsf)
|
||||
}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) register(c *connection) {
|
||||
wsf.funcs <- func(wsf *websocketForwarder) {
|
||||
wsf.connections[c] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) unregister(c *connection) {
|
||||
wsf.funcs <- func(wsf *websocketForwarder) {
|
||||
if _, ok := wsf.connections[c]; ok {
|
||||
delete(wsf.connections, c)
|
||||
close(c.send)
|
||||
}
|
||||
case message := <-wsf.broadcast:
|
||||
}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) send(m *msg) {
|
||||
wsf.funcs <- func(wsf *websocketForwarder) {
|
||||
if len(wsf.connections) == 0 {
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
encoder := json.NewEncoder(&buf)
|
||||
if err := encoder.Encode(message); err != nil {
|
||||
if err := encoder.Encode(m); err != nil {
|
||||
log.Printf("encoding changes failed: %s\n", err)
|
||||
continue
|
||||
return
|
||||
}
|
||||
m := buf.Bytes()
|
||||
|
||||
data := buf.Bytes()
|
||||
|
||||
for c := range wsf.connections {
|
||||
select {
|
||||
case c.send <- m:
|
||||
case c.send <- data:
|
||||
default:
|
||||
delete(wsf.connections, c)
|
||||
close(c.send)
|
||||
@ -73,6 +89,13 @@ func (wsf *websocketForwarder) run() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) BaseTilesUpdated(changes []xz) {
|
||||
wsf.send(&msg{Tiles: changes})
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) {
|
||||
wsf.send(&msg{Pls: pls})
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
@ -82,20 +105,12 @@ func (wsf *websocketForwarder) ServeHTTP(rw http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
c := &connection{ws: ws, send: make(chan []byte, 8)}
|
||||
wsf.register <- c
|
||||
defer func() { wsf.unregister <- c }()
|
||||
wsf.register(c)
|
||||
defer wsf.unregister(c)
|
||||
go c.writer()
|
||||
c.reader()
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) BaseTilesUpdated(changes []xz) {
|
||||
wsf.broadcast <- msg{Tiles: changes}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) {
|
||||
wsf.broadcast <- msg{Pls: pls}
|
||||
}
|
||||
|
||||
func (c *connection) writer() {
|
||||
defer c.ws.Close()
|
||||
for msg := range c.send {
|
||||
|
Loading…
Reference in New Issue
Block a user