Skip to content

Commit 7d2c35a

Browse files
committed
WIP on more involved chat example
1 parent aa4d85f commit 7d2c35a

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

examples/chat/chat.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
6-
"io/ioutil"
77
"log"
88
"net/http"
99
"sync"
@@ -14,6 +14,26 @@ import (
1414
"nhooyr.io/websocket"
1515
)
1616

17+
type clientJoin struct {
18+
ClientID string `json:"client_id"`
19+
ClientName string `json:"client_name"`
20+
}
21+
22+
type clientLeave struct {
23+
ClientID string `json:"client_id"`
24+
}
25+
26+
type clientRename struct {
27+
ClientID string `json:"client_id"`
28+
ClientName string `json:"client_name"`
29+
}
30+
31+
type messageBroadcast struct {
32+
ClientID string `json:"client_id"`
33+
Time time.Time `json:"time"`
34+
Message string `json:"msg"`
35+
}
36+
1737
// chatServer enables broadcasting to a set of subscribers.
1838
type chatServer struct {
1939
// subscriberMessageBuffer controls the max number
@@ -81,7 +101,7 @@ func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
81101
return
82102
}
83103
if websocket.CloseStatus(err) == websocket.StatusNormalClosure ||
84-
websocket.CloseStatus(err) == websocket.StatusGoingAway {
104+
websocket.CloseStatus(err) == websocket.StatusGoingAway {
85105
return
86106
}
87107
if err != nil {
@@ -98,13 +118,18 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
98118
return
99119
}
100120
body := http.MaxBytesReader(w, r.Body, 8192)
101-
msg, err := ioutil.ReadAll(body)
121+
122+
var msg message
123+
err := json.NewDecoder(body).Decode(&msg)
102124
if err != nil {
103-
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
125+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
104126
return
105127
}
106128

107-
cs.publish(msg)
129+
msg.Author =
130+
131+
// TODO improve
132+
cs.publish([]byte(msg.Message))
108133

109134
w.WriteHeader(http.StatusAccepted)
110135
}

examples/chat/index.css

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
body {
2-
width: 100vw;
3-
min-width: 320px;
4-
}
5-
61
#root {
72
padding: 40px 20px;
8-
max-width: 600px;
93
margin: auto;
4+
min-width: 320px;
5+
max-width: 600px;
106
height: 100vh;
117

128
display: flex;
@@ -79,3 +75,15 @@ body {
7975
#publish-form input[type="submit"]:active {
8076
background-color: red;
8177
}
78+
79+
#username-form-overlay {
80+
position: fixed;
81+
width: 100%;
82+
height: 100%;
83+
margin: 0;
84+
background-color: rgba(0, 0, 0, 0.5);
85+
86+
display: flex;
87+
align-items: center;
88+
justify-content: center;
89+
}

examples/chat/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
</head>
1313
<body>
1414
<div id="root">
15+
<div id="username-form-overlay">
16+
<form id="username-form">
17+
<input name="message" id="username-input" type="text" />
18+
<input value="Register Username" type="submit" />
19+
</form>
20+
</div>
21+
1522
<div id="message-log"></div>
1623
<div id="publish-form-container">
1724
<form id="publish-form">

examples/chat/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
try {
6565
const resp = await fetch("/publish", {
6666
method: "POST",
67-
body: msg,
67+
body: {
68+
time: new Date(),
69+
msg: msg,
70+
},
6871
})
6972
if (resp.status !== 202) {
7073
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)

0 commit comments

Comments
 (0)