mirror of https://github.com/nealey/microchat.git
Hopefully prevent race conditions
This commit is contained in:
parent
ba2d6a46ad
commit
0fdc23f923
42
microchat.go
42
microchat.go
|
@ -1,11 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"log"
|
||||
"time"
|
||||
"strconv"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
|
@ -17,14 +17,33 @@ type Message struct {
|
|||
type Forum struct {
|
||||
Name string
|
||||
Log []Message
|
||||
SendQ chan Message
|
||||
LogQ chan []Message
|
||||
}
|
||||
|
||||
var foraByName map[string]*Forum
|
||||
|
||||
func (f *Forum) HandleSends() {
|
||||
for m := range f.SendQ {
|
||||
f.Log = append(f.Log, m)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Forum) HandleReads() {
|
||||
for {
|
||||
f.LogQ <- f.Log
|
||||
}
|
||||
}
|
||||
|
||||
func newForum(name string) *Forum {
|
||||
f := &Forum{
|
||||
Name: name,
|
||||
SendQ: make(chan Message, 10),
|
||||
LogQ: make(chan []Message, 10),
|
||||
}
|
||||
go f.HandleSends()
|
||||
go f.HandleReads()
|
||||
|
||||
foraByName[name] = f
|
||||
return f
|
||||
}
|
||||
|
@ -35,13 +54,12 @@ func sayHandler(w http.ResponseWriter, r *http.Request) {
|
|||
Who: r.FormValue("who"),
|
||||
Text: r.FormValue("text"),
|
||||
}
|
||||
log.Println(message)
|
||||
forumName := r.FormValue("forum")
|
||||
forum, ok := foraByName[forumName]
|
||||
f, ok := foraByName[forumName]
|
||||
if !ok {
|
||||
forum = newForum(forumName)
|
||||
f = newForum(forumName)
|
||||
}
|
||||
forum.Log = append(forum.Log, message)
|
||||
f.SendQ <- message
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
@ -66,13 +84,15 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
|
|||
entries = 500
|
||||
}
|
||||
|
||||
forum, ok := foraByName[forumName]
|
||||
f, ok := foraByName[forumName]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
pos := len(forum.Log) - entries
|
||||
log := <-f.LogQ
|
||||
|
||||
pos := len(log) - entries
|
||||
if pos < 0 {
|
||||
pos = 0
|
||||
}
|
||||
|
@ -81,7 +101,7 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
e := json.NewEncoder(w)
|
||||
e.Encode(forum.Log[pos:])
|
||||
e.Encode(f.Log[pos:])
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -11,6 +11,10 @@ body {
|
|||
input[name="who"] {
|
||||
width: 6em;
|
||||
}
|
||||
input[name="text"] {
|
||||
min-width: 30em;
|
||||
width: calc(90% - 6em);
|
||||
}
|
||||
|
||||
.when {
|
||||
color: #888;
|
||||
|
|
|
@ -18,7 +18,6 @@ function μchatInit(initEvent) {
|
|||
let form = event.target
|
||||
let inp = form.elements.text
|
||||
let body = new FormData(form)
|
||||
console.log(form, body)
|
||||
fetch("say", {
|
||||
method: "POST",
|
||||
body: body,
|
||||
|
@ -86,6 +85,9 @@ function μchatInit(initEvent) {
|
|||
.then(updateLog)
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
toast("Server error: " + err)
|
||||
})
|
||||
}
|
||||
|
||||
for (let f of document.forms) {
|
||||
|
|
Loading…
Reference in New Issue