Compare commits
2 Commits
main
...
c9c4800d68
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9c4800d68 | ||
|
|
d793e13361 |
@@ -1,4 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"tts-room/src/server"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
s := server.NewServer()
|
||||||
|
if err := http.ListenAndServe(":10000", s); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
go.mod
7
go.mod
@@ -1,3 +1,8 @@
|
|||||||
module tts-room
|
module tts-room
|
||||||
|
|
||||||
go 1.22.2
|
go 1.24.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
golang.org/x/time v0.14.0
|
||||||
|
)
|
||||||
|
|||||||
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
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/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||||
77
src/public/index.html
Normal file
77
src/public/index.html
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<script>
|
||||||
|
window.addEventListener("load", function(evt) {
|
||||||
|
|
||||||
|
var output = document.getElementById("output");
|
||||||
|
var input = document.getElementById("input");
|
||||||
|
var ws;
|
||||||
|
|
||||||
|
var print = function(message) {
|
||||||
|
var d = document.createElement("div");
|
||||||
|
d.textContent = message;
|
||||||
|
output.appendChild(d);
|
||||||
|
output.scroll(0, output.scrollHeight);
|
||||||
|
};
|
||||||
|
|
||||||
|
document.getElementById("open").onclick = function(evt) {
|
||||||
|
if (ws) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ws = new WebSocket("ws://"+window.location.host+"/ws");
|
||||||
|
ws.onopen = function(evt) {
|
||||||
|
print("OPEN");
|
||||||
|
}
|
||||||
|
ws.onclose = function(evt) {
|
||||||
|
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) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
print("SEND: " + input.value);
|
||||||
|
ws.send(input.value);
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
document.getElementById("close").onclick = function(evt) {
|
||||||
|
if (!ws) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ws.close();
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<table>
|
||||||
|
<tr><td valign="top" width="50%">
|
||||||
|
<p>Click "Open" to create a connection to the server,
|
||||||
|
"Send" to send a message to the server and "Close" to close the connection.
|
||||||
|
You can change the message and send multiple times.
|
||||||
|
<p>
|
||||||
|
<form>
|
||||||
|
<button id="open">Open</button>
|
||||||
|
<button id="close">Close</button>
|
||||||
|
<p><input id="input" type="text" value="Hello world!">
|
||||||
|
<button id="send">Send</button>
|
||||||
|
</form>
|
||||||
|
</td><td valign="top" width="50%">
|
||||||
|
<div id="output" style="max-height: 70vh;overflow-y: scroll;"></div>
|
||||||
|
</td></tr></table>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
33
src/server/server.go
Normal file
33
src/server/server.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct{}
|
||||||
|
|
||||||
|
func NewServer() Server {
|
||||||
|
return Server{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/ws":
|
||||||
|
if err := s.WS(w, r); err != nil {
|
||||||
|
log.Println("[ws]", err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
http.FileServer(http.Dir("./src/public")).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Server) WS(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
sess, err := newSession(w, r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer sess.Close()
|
||||||
|
|
||||||
|
return sess.Run()
|
||||||
|
}
|
||||||
81
src/server/session.go
Normal file
81
src/server/session.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
|
)
|
||||||
|
|
||||||
|
type session struct {
|
||||||
|
ctx context.Context
|
||||||
|
can context.CancelFunc
|
||||||
|
ws *websocket.Conn
|
||||||
|
wg sync.WaitGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{}
|
||||||
|
|
||||||
|
func newSession(w http.ResponseWriter, r *http.Request) (*session, error) {
|
||||||
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
ctx, can := context.WithCancel(r.Context())
|
||||||
|
return &session{
|
||||||
|
ctx: ctx,
|
||||||
|
can: can,
|
||||||
|
ws: c,
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *session) Close() {
|
||||||
|
if s.ws != nil {
|
||||||
|
s.ws.Close()
|
||||||
|
}
|
||||||
|
s.ws = nil
|
||||||
|
s.can()
|
||||||
|
s.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *session) Run() error {
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
go s.gather()
|
||||||
|
go s.scatter()
|
||||||
|
|
||||||
|
<-s.ctx.Done()
|
||||||
|
return s.ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *session) gather() {
|
||||||
|
s.while(func() error {
|
||||||
|
mt, message, err := s.ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Println(" read:", mt, message) // TODO
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *session) scatter() {
|
||||||
|
s.while(func() error {
|
||||||
|
return s.ws.WriteMessage(1, []byte("message")) // TODO
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *session) while(foo func() error) {
|
||||||
|
defer s.can()
|
||||||
|
|
||||||
|
s.wg.Add(1)
|
||||||
|
defer s.wg.Done()
|
||||||
|
|
||||||
|
l := rate.NewLimiter(20, 1)
|
||||||
|
for l.Wait(s.ctx) == nil {
|
||||||
|
if err := foo(); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user