Compare commits
5 Commits
c4a21861cd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
001fef8bbc | ||
|
|
a9004c38af | ||
|
|
b34c980ea5 | ||
|
|
78fd4fd28b | ||
|
|
b89d8e3d3f |
@@ -5,15 +5,15 @@
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
|
||||
<script>
|
||||
<script>
|
||||
window.addEventListener("load", function(evt) {
|
||||
var output = document.getElementById("output");
|
||||
var input = document.getElementById("input");
|
||||
var ws;
|
||||
|
||||
|
||||
var print = function(message) {
|
||||
var before = output.innerHTML.split("<br>");
|
||||
before.unshift(`${new Date().toLocaleTimeString()} | ${message}`);
|
||||
before.unshift(`<span>${new Date().toLocaleTimeString()} | ${message}</span>`);
|
||||
if (before.length > 20) {
|
||||
before = before.slice(0, 20);
|
||||
}
|
||||
@@ -56,8 +56,8 @@
|
||||
ele.appendChild(document.createElement("br"));
|
||||
}
|
||||
}
|
||||
|
||||
ws = new WebSocket("ws://"+window.location.host+"/ws");
|
||||
|
||||
ws = new WebSocket("ws://"+window.location.host+"/ws"+window.location.search);
|
||||
ws.onopen = function(evt) {
|
||||
print("READY");
|
||||
}
|
||||
@@ -83,12 +83,12 @@
|
||||
}
|
||||
|
||||
window.speechSynthesis.speak(utterThis);
|
||||
print("RESPONSE: " + evt.data);
|
||||
print(data.Text);
|
||||
}
|
||||
ws.onerror = function(evt) {
|
||||
print("ERROR: " + evt.data);
|
||||
}
|
||||
|
||||
|
||||
document.getElementById("send").onclick = function(evt) {
|
||||
if (!ws || !input.value) {
|
||||
return false;
|
||||
@@ -99,7 +99,7 @@
|
||||
for (var i = 0; i < voiceEles.length; i++)
|
||||
if (voiceEles[i].checked)
|
||||
voiceIdx = voiceEles[i].value;
|
||||
|
||||
|
||||
const data = JSON.stringify({
|
||||
"Text": input.value,
|
||||
"Pitch": Number.parseInt(document.getElementById("pitch").value, 10),
|
||||
@@ -117,6 +117,19 @@
|
||||
};
|
||||
});
|
||||
</script>
|
||||
<style>
|
||||
#output > span {
|
||||
opacity: 0.33;
|
||||
}
|
||||
#output > span:nth-child(1) {
|
||||
opacity: 1;
|
||||
}
|
||||
#output > span:nth-child(2)
|
||||
, #output > span:nth-child(3)
|
||||
{
|
||||
opacity: 0.66;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div style="width: 80%; height: 80%; margin: auto; display: flex; flex-direction: row;">
|
||||
|
||||
@@ -5,4 +5,5 @@ type message struct {
|
||||
Pitch int
|
||||
Rate float64
|
||||
VoiceIdx int
|
||||
room string
|
||||
}
|
||||
|
||||
@@ -31,10 +31,12 @@ func (s *Server) WS(w http.ResponseWriter, r *http.Request) error {
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
log.Println("someone has joined", sess.room)
|
||||
defer log.Println("someone has left", sess.room)
|
||||
|
||||
sess.cb = func(m message) error {
|
||||
log.Printf("cbing to all other sessions %+v", m)
|
||||
for i := range s.sessions {
|
||||
if s.sessions[i].id != sess.id {
|
||||
if s.sessions[i].id != sess.id && s.sessions[i].room == sess.room {
|
||||
select {
|
||||
case s.sessions[i].scatterc <- m:
|
||||
case <-s.sessions[i].ctx.Done():
|
||||
|
||||
@@ -20,6 +20,7 @@ type session struct {
|
||||
cb func(message) error
|
||||
id string
|
||||
scatterc chan (message)
|
||||
room string
|
||||
}
|
||||
|
||||
var upgrader = websocket.Upgrader{}
|
||||
@@ -34,6 +35,7 @@ func newSession(w http.ResponseWriter, r *http.Request, cb func(message) error)
|
||||
cb: cb,
|
||||
id: uuid.New().String(),
|
||||
scatterc: make(chan message, 20),
|
||||
room: r.URL.Query().Get("room"),
|
||||
}, err
|
||||
}
|
||||
|
||||
@@ -70,8 +72,8 @@ func (s *session) gather() {
|
||||
if err := json.Unmarshal(msg, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
m.room = s.room
|
||||
|
||||
log.Printf("gathered %+v (%s)", m, msg)
|
||||
return s.cb(m)
|
||||
})
|
||||
}
|
||||
@@ -80,7 +82,6 @@ func (s *session) scatter() {
|
||||
s.while(func() error {
|
||||
select {
|
||||
case m := <-s.scatterc:
|
||||
log.Printf("scattering %+v", m)
|
||||
b, _ := json.Marshal(m)
|
||||
return s.ws.WriteMessage(1, b)
|
||||
case <-s.ctx.Done():
|
||||
|
||||
Reference in New Issue
Block a user