-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
129 lines (113 loc) · 2.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"io"
"log"
"net/http"
"sync"
"time"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/webtransport-go"
)
type MessageServer struct {
listeners []chan []byte
}
func (m *MessageServer) Subscribe() chan []byte {
ch := make(chan []byte)
m.listeners = append(m.listeners, ch)
return ch
}
func (m *MessageServer) Unsubscribe(ch chan []byte) {
for i := range m.listeners {
if m.listeners[i] == ch {
m.listeners = m.listeners[:i+copy(m.listeners[i:], m.listeners[i+1:])]
close(ch)
break
}
}
}
func (m *MessageServer) Broadcast(message []byte) {
for _, ch := range m.listeners {
ch <- message
}
}
func main() {
messageServer := &MessageServer{
listeners: make([]chan []byte, 0),
}
wt := webtransport.Server{
H3: http3.Server{
Addr: ":4433",
},
CheckOrigin: func(r *http.Request) bool {
return true
},
}
sessionID := 0
http.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
session, err := wt.Upgrade(w, r)
if err != nil {
log.Printf("upgrading failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
sessionID += 1
log.Printf("Session #%d start.", sessionID)
messageCh := messageServer.Subscribe()
closeCh := make(chan int)
wg := &sync.WaitGroup{}
// メッセージを送信する
wg.Add(1)
go (func() {
defer wg.Done()
for {
select {
case message := <-messageCh:
log.Printf("Send message: %s\n", message)
stream, err := session.OpenUniStream()
if err != nil {
log.Println("Open stream failed:", err)
break
}
_, err = stream.Write(message)
if err != nil {
log.Println("Send stream failed:", err)
break
}
stream.Close()
case <-closeCh:
log.Println("Send stream closed.")
return
}
}
})()
// メッセージを受信する
wg.Add(1)
go (func() {
defer wg.Done()
for {
acceptCtx, acceptCtxCancel := context.WithTimeout(session.Context(), 10*time.Minute)
stream, err := session.AcceptUniStream(acceptCtx)
if err != nil {
acceptCtxCancel()
log.Println("Accept stream failed:", err)
break
}
acceptCtxCancel()
p, err := io.ReadAll(stream)
if err != nil {
log.Println("Session closed, ending stream listener:", err)
break
}
log.Printf("Received stream: %s", p)
messageServer.Broadcast(p)
}
closeCh <- 1
})()
wg.Wait()
messageServer.Unsubscribe(messageCh)
log.Printf("Session #%d closed.", sessionID)
})
wt.ListenAndServeTLS("localhost.pem", "localhost-key.pem")
}