mirror of https://github.com/nealey/vail.git
Adds channels, groundwork for message verification
This commit is contained in:
parent
a28a0bb2df
commit
3ae0345104
53
main.go
53
main.go
|
@ -6,54 +6,43 @@ import (
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (ch Channel) Write(msg Message) {
|
var book Book
|
||||||
for i, o := range clients {
|
|
||||||
if o.active == false {
|
|
||||||
nclients := len(clients)
|
|
||||||
clients[i] = clients[nclients - 1]
|
|
||||||
clients[nclients - 1] = Client{}
|
|
||||||
clients = clients[:nclients - 1]
|
|
||||||
} else if o == c {
|
|
||||||
// Don't send it back to the sending client
|
|
||||||
} else {
|
|
||||||
o.ws.Write(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ws *websocket.Conn
|
repeaterName string
|
||||||
active bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var clients []Client
|
func (c Client) Handle(ws *websocket.Conn) {
|
||||||
|
ws.MaxPayloadBytes = 500
|
||||||
|
book.Join(c.repeaterName, ws)
|
||||||
|
defer book.Part(c.repeaterName, ws)
|
||||||
|
|
||||||
func (c Client) Chat() {
|
for {
|
||||||
websocket.Message.Receive
|
buf := make([]byte, ws.MaxPayloadBytes)
|
||||||
for c.active {
|
n, err := ws.Read(buf)
|
||||||
buf := make([]byte, 800)
|
|
||||||
n, err := c.ws.Read(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.active = false
|
break
|
||||||
}
|
}
|
||||||
buf = buf[:n]
|
buf = buf[:n]
|
||||||
|
book.Send(c.repeaterName, buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ChatServer(ws *websocket.Conn) {
|
func ChatHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
me := Client{
|
c := Client {
|
||||||
ws: ws,
|
repeaterName: r.FormValue("repeater"),
|
||||||
active: true,
|
|
||||||
}
|
}
|
||||||
clients = append(clients, me)
|
|
||||||
|
// This API is confusing as hell.
|
||||||
me.Chat()
|
// I suspect there's a better way to do this.
|
||||||
|
websocket.Handler(c.Handle).ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.Handle("/chat", websocket.Handler(ChatServer))
|
book = NewBook()
|
||||||
|
http.Handle("/chat", http.HandlerFunc(ChatHandler))
|
||||||
http.Handle("/", http.FileServer(http.Dir("static")))
|
http.Handle("/", http.FileServer(http.Dir("static")))
|
||||||
|
go book.Run()
|
||||||
err := http.ListenAndServe(":8080", nil)
|
err := http.ListenAndServe(":8080", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err.Error())
|
log.Fatal(err.Error())
|
||||||
|
|
Loading…
Reference in New Issue