Compare commits
11 Commits
c9c4800d68
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
001fef8bbc | ||
|
|
a9004c38af | ||
|
|
b34c980ea5 | ||
|
|
78fd4fd28b | ||
|
|
b89d8e3d3f | ||
|
|
c4a21861cd | ||
|
|
d5c3a52215 | ||
|
|
bc1f7779d7 | ||
|
|
49880c837d | ||
|
|
4dd5a40dfe | ||
|
|
13b583a77e |
2
go.mod
2
go.mod
@@ -6,3 +6,5 @@ require (
|
|||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
golang.org/x/time v0.14.0
|
golang.org/x/time v0.14.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/google/uuid v1.6.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
||||||
|
|||||||
@@ -1,77 +1,158 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<script>
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
window.addEventListener("load", function(evt) {
|
<meta name="viewport" content="width=device-width" />
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
|
||||||
|
<script>
|
||||||
|
window.addEventListener("load", function(evt) {
|
||||||
|
var output = document.getElementById("output");
|
||||||
|
var input = document.getElementById("input");
|
||||||
|
var ws;
|
||||||
|
|
||||||
var output = document.getElementById("output");
|
var print = function(message) {
|
||||||
var input = document.getElementById("input");
|
var before = output.innerHTML.split("<br>");
|
||||||
var ws;
|
before.unshift(`<span>${new Date().toLocaleTimeString()} | ${message}</span>`);
|
||||||
|
if (before.length > 20) {
|
||||||
|
before = before.slice(0, 20);
|
||||||
|
}
|
||||||
|
var after = before.join("<br>");
|
||||||
|
output.innerHTML = after;
|
||||||
|
output.scroll(0, 0);
|
||||||
|
};
|
||||||
|
|
||||||
var print = function(message) {
|
const synth = window.speechSynthesis;
|
||||||
var d = document.createElement("div");
|
const voices = synth.getVoices().sort(function (a, b) {
|
||||||
d.textContent = message;
|
const aname = a.name.toUpperCase();
|
||||||
output.appendChild(d);
|
const bname = b.name.toUpperCase();
|
||||||
output.scroll(0, output.scrollHeight);
|
|
||||||
};
|
|
||||||
|
|
||||||
document.getElementById("open").onclick = function(evt) {
|
if (aname < bname) {
|
||||||
if (ws) {
|
return -1;
|
||||||
return false;
|
} else if (aname == bname) {
|
||||||
}
|
return 0;
|
||||||
ws = new WebSocket("ws://"+window.location.host+"/ws");
|
} else {
|
||||||
ws.onopen = function(evt) {
|
return +1;
|
||||||
print("OPEN");
|
}
|
||||||
}
|
});
|
||||||
ws.onclose = function(evt) {
|
var voices_idx = 0;
|
||||||
print("CLOSE");
|
|
||||||
ws = null;
|
|
||||||
}
|
|
||||||
ws.onmessage = function(evt) {
|
|
||||||
print("RESPONSE: " + evt.data);
|
|
||||||
}
|
|
||||||
ws.onerror = function(evt) {
|
|
||||||
print("ERROR: " + evt.data);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
document.getElementById("send").onclick = function(evt) {
|
{
|
||||||
if (!ws) {
|
var ele = document.getElementById("voices");
|
||||||
return false;
|
for(var i in voices) {
|
||||||
}
|
var voice = voices[i];
|
||||||
print("SEND: " + input.value);
|
const opt = document.createElement("input");
|
||||||
ws.send(input.value);
|
opt.type = "radio";
|
||||||
return false;
|
opt.name = "voice";
|
||||||
};
|
opt.id = `voices-${i}-${voice.name}`
|
||||||
|
opt.value = i;
|
||||||
|
|
||||||
document.getElementById("close").onclick = function(evt) {
|
const label = document.createElement("label");
|
||||||
if (!ws) {
|
label.for = opt.id;
|
||||||
return false;
|
label.textContent = voice.name;
|
||||||
}
|
|
||||||
ws.close();
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
});
|
ele.appendChild(opt);
|
||||||
</script>
|
ele.appendChild(label);
|
||||||
</head>
|
ele.appendChild(document.createElement("br"));
|
||||||
<body>
|
}
|
||||||
<table>
|
}
|
||||||
<tr><td valign="top" width="50%">
|
|
||||||
<p>Click "Open" to create a connection to the server,
|
ws = new WebSocket("ws://"+window.location.host+"/ws"+window.location.search);
|
||||||
"Send" to send a message to the server and "Close" to close the connection.
|
ws.onopen = function(evt) {
|
||||||
You can change the message and send multiple times.
|
print("READY");
|
||||||
<p>
|
}
|
||||||
<form>
|
ws.onclose = function(evt) {
|
||||||
<button id="open">Open</button>
|
print("CLOSE");
|
||||||
<button id="close">Close</button>
|
ws = null;
|
||||||
<p><input id="input" type="text" value="Hello world!">
|
}
|
||||||
<button id="send">Send</button>
|
ws.onmessage = function(evt) {
|
||||||
</form>
|
console.log("evt.data:", evt.data)
|
||||||
</td><td valign="top" width="50%">
|
const data = JSON.parse(evt.data);
|
||||||
<div id="output" style="max-height: 70vh;overflow-y: scroll;"></div>
|
|
||||||
</td></tr></table>
|
const utterThis = new SpeechSynthesisUtterance(data.Text);
|
||||||
</body>
|
|
||||||
|
const idx = data.VoiceIdx || 0;
|
||||||
|
utterThis.voice = voices[idx];
|
||||||
|
|
||||||
|
if (data.Pitch) {
|
||||||
|
utterThis.pitch = data.Pitch
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.Rate) {
|
||||||
|
utterThis.rate = data.Rate
|
||||||
|
}
|
||||||
|
|
||||||
|
window.speechSynthesis.speak(utterThis);
|
||||||
|
print(data.Text);
|
||||||
|
}
|
||||||
|
ws.onerror = function(evt) {
|
||||||
|
print("ERROR: " + evt.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById("send").onclick = function(evt) {
|
||||||
|
if (!ws || !input.value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var voiceIdx = "0";
|
||||||
|
var voiceEles = document.getElementsByName("voice");
|
||||||
|
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),
|
||||||
|
"Rate": Number.parseFloat(document.getElementById("rate").value, 10),
|
||||||
|
"VoiceIdx": Number.parseInt(voiceIdx, 10),
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.send(data);
|
||||||
|
print("SENT: " + input.value);
|
||||||
|
input.value = "";
|
||||||
|
if (document.getElementById("listen").checked) {
|
||||||
|
ws.onmessage({data: data});
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</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;">
|
||||||
|
<div style="display: flex; flex-direction: column;">
|
||||||
|
<form style="flex-grow: 1;">
|
||||||
|
<p><input id="input" type="textarea" value="" autofocus style="width: 80%">
|
||||||
|
<p>
|
||||||
|
<button id="send">Send</button>
|
||||||
|
</form>
|
||||||
|
<div>
|
||||||
|
<form id="voices"></form>
|
||||||
|
|
||||||
|
<input id="pitch" type="number" value="0"/>
|
||||||
|
<label for="pitch">pitch</label></br>
|
||||||
|
|
||||||
|
<input id="rate" type="number" value="0"/>
|
||||||
|
<label for="rate">rate</label></br>
|
||||||
|
|
||||||
|
<input id="listen" type="checkbox" />
|
||||||
|
<label for="listen">listen</label></br>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="output" style="flex-grow: 1; overflow-y: scroll;"></div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
9
src/server/message.go
Normal file
9
src/server/message.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
type message struct {
|
||||||
|
Text string
|
||||||
|
Pitch int
|
||||||
|
Rate float64
|
||||||
|
VoiceIdx int
|
||||||
|
room string
|
||||||
|
}
|
||||||
@@ -5,13 +5,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct{}
|
type Server struct {
|
||||||
|
sessions []*session
|
||||||
func NewServer() Server {
|
|
||||||
return Server{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func NewServer() *Server {
|
||||||
|
return &Server{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/ws":
|
case "/ws":
|
||||||
if err := s.WS(w, r); err != nil {
|
if err := s.WS(w, r); err != nil {
|
||||||
@@ -22,12 +24,37 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) WS(w http.ResponseWriter, r *http.Request) error {
|
func (s *Server) WS(w http.ResponseWriter, r *http.Request) error {
|
||||||
sess, err := newSession(w, r)
|
sess, err := newSession(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer sess.Close()
|
defer sess.Close()
|
||||||
|
|
||||||
|
log.Println("someone has joined", sess.room)
|
||||||
|
defer log.Println("someone has left", sess.room)
|
||||||
|
|
||||||
|
sess.cb = func(m message) error {
|
||||||
|
for i := range s.sessions {
|
||||||
|
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():
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sessions = append(s.sessions, sess)
|
||||||
|
defer func() {
|
||||||
|
for i := range s.sessions {
|
||||||
|
if s.sessions[i].id == sess.id {
|
||||||
|
s.sessions = append(s.sessions[:i], s.sessions[i+1:]...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return sess.Run()
|
return sess.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,30 +2,40 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type session struct {
|
type session struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
can context.CancelFunc
|
can context.CancelFunc
|
||||||
ws *websocket.Conn
|
ws *websocket.Conn
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
cb func(message) error
|
||||||
|
id string
|
||||||
|
scatterc chan (message)
|
||||||
|
room string
|
||||||
}
|
}
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{}
|
var upgrader = websocket.Upgrader{}
|
||||||
|
|
||||||
func newSession(w http.ResponseWriter, r *http.Request) (*session, error) {
|
func newSession(w http.ResponseWriter, r *http.Request, cb func(message) error) (*session, error) {
|
||||||
c, err := upgrader.Upgrade(w, r, nil)
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
ctx, can := context.WithCancel(r.Context())
|
ctx, can := context.WithCancel(r.Context())
|
||||||
return &session{
|
return &session{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
can: can,
|
can: can,
|
||||||
ws: c,
|
ws: c,
|
||||||
|
cb: cb,
|
||||||
|
id: uuid.New().String(),
|
||||||
|
scatterc: make(chan message, 20),
|
||||||
|
room: r.URL.Query().Get("room"),
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,18 +60,33 @@ func (s *session) Run() error {
|
|||||||
|
|
||||||
func (s *session) gather() {
|
func (s *session) gather() {
|
||||||
s.while(func() error {
|
s.while(func() error {
|
||||||
mt, message, err := s.ws.ReadMessage()
|
mt, msg, err := s.ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Println(" read:", mt, message) // TODO
|
if mt != 1 {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var m message
|
||||||
|
if err := json.Unmarshal(msg, &m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.room = s.room
|
||||||
|
|
||||||
|
return s.cb(m)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) scatter() {
|
func (s *session) scatter() {
|
||||||
s.while(func() error {
|
s.while(func() error {
|
||||||
return s.ws.WriteMessage(1, []byte("message")) // TODO
|
select {
|
||||||
|
case m := <-s.scatterc:
|
||||||
|
b, _ := json.Marshal(m)
|
||||||
|
return s.ws.WriteMessage(1, b)
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
return s.ctx.Err()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user