-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redo weather/traffic websocket updates (#74).
- Loading branch information
Showing
5 changed files
with
83 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"golang.org/x/net/websocket" | ||
) | ||
|
||
type uibroadcaster struct { | ||
sockets []*websocket.Conn | ||
messages chan []byte | ||
} | ||
|
||
func NewUIBroadcaster() *uibroadcaster { | ||
ret := &uibroadcaster{ | ||
sockets: make([]*websocket.Conn, 0), | ||
messages: make(chan []byte, 1024), | ||
} | ||
go ret.writer() | ||
return ret | ||
} | ||
|
||
func (u *uibroadcaster) Send(msg []byte) { | ||
u.messages <- msg | ||
} | ||
|
||
func (u *uibroadcaster) AddSocket(sock *websocket.Conn) { | ||
u.sockets = append(u.sockets, sock) | ||
} | ||
|
||
func (u *uibroadcaster) writer() { | ||
for { | ||
msg := <-u.messages | ||
// Send to all. | ||
p := make([]*websocket.Conn, 0) // Keep a list of the writeable sockets. | ||
for _, sock := range u.sockets { | ||
_, err := sock.Write(msg) | ||
if err == nil { | ||
p = append(p, sock) | ||
} | ||
} | ||
u.sockets = p // Save the list of writeable sockets. | ||
} | ||
} |