Skip to content

Commit b95bd3a

Browse files
committed
Fix connecting clients
1 parent 7b8c4af commit b95bd3a

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

app/src/App.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class App extends Component {
1313
init: true,
1414
participants: [],
1515
messages: [],
16+
name: "",
1617
};
1718

1819
this.participantSubmit = this.participantSubmit.bind(this);
@@ -25,21 +26,20 @@ class App extends Component {
2526
if (input === "") {
2627
return;
2728
}
29+
30+
// init client websocket
2831
socket = new WebSocket(`ws://127.0.0.1:8081/ws?name=${input}`);
2932
socket.onopen = (event) => {
3033
console.log('Opened socket');
31-
console.log(event);
3234
};
3335
socket.onerror = (event) => {
3436
console.log(event);
3537
}
36-
socket.onmessage = (event) => {
37-
const names = JSON.parse(event.data);
38-
this.setState({
39-
init: false,
40-
participants: names,
41-
});
42-
}
38+
this.setState({
39+
init: false,
40+
name: input,
41+
participants: [],
42+
});
4343
}
4444

4545
textSubmit(e) {
@@ -49,9 +49,10 @@ class App extends Component {
4949
return;
5050
}
5151

52+
// send message request to server
5253
socket.send(JSON.stringify({
5354
text: input,
54-
client: "testClient",
55+
name: this.state.name,
5556
timestamp: new Date().toLocaleTimeString(),
5657
}));
5758

@@ -62,24 +63,30 @@ class App extends Component {
6263
// keep message box scrolled appropriately with new messages
6364
if (!this.state.init) {
6465
document.getElementById("conversation-main").scrollTop = document.getElementById("conversation-main").scrollHeight;
65-
}
66-
}
6766

68-
componentDidMount() {
69-
if (!this.state.init) {
67+
console.log("componentDidUpdate")
7068
socket.onmessage = (event) => {
71-
const { text, client, timestamp } = JSON.parse(event.data);
69+
console.log(event);
70+
const data = JSON.parse(event.data);
71+
console.log(data);
72+
if (!data.text) { // client added
73+
this.setState({
74+
participants: data
75+
});
76+
} else { // message added
77+
const { text, name, timestamp } = data;
7278

73-
this.setState({
74-
messages: [
75-
...this.state.messages,
76-
{
77-
text,
78-
client,
79-
timestamp,
80-
}
81-
],
82-
});
79+
this.setState({
80+
messages: [
81+
...this.state.messages,
82+
{
83+
text,
84+
name,
85+
timestamp,
86+
}
87+
],
88+
});
89+
}
8390
}
8491
}
8592
}

app/src/components/Message.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import React from 'react';
33
import '../styles/Message.css';
44

55
const Message = (props) => {
6-
console.log(props);
7-
const { text, client, timestamp } = props.message;
6+
const { text, name, timestamp } = props.message;
87

98
return (
109
<div className="message">
1110
<div className="message-top">
12-
<span className="client-name">{client}</span>
11+
<span className="client-name">{name}</span>
1312
<span className="timestamp">{timestamp}</span>
1413
</div>
1514
<div className="message-text">

server/websocket.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ var upgrader = websocket.Upgrader{
3030
type JsonData struct {
3131
Name string `json:"name"`
3232
Text string `json:"text"`
33-
Client string `json:"client"`
3433
Timestamp string `json:"timestamp"`
3534
}
3635

@@ -58,6 +57,7 @@ func (c *Client) readPump() {
5857

5958
// handle connection read
6059
for {
60+
fmt.Println("reading from client")
6161
// read JSON data from connection
6262
message := JsonData{}
6363
if err := c.conn.ReadJSON(&message); err != nil {
@@ -84,7 +84,7 @@ func (c *Client) writePump() {
8484
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
8585
if !ok {
8686
// channel has been closed by the hub
87-
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
87+
// c.conn.WriteMessage(websocket.CloseMessage, []byte{})
8888
return
8989
}
9090

@@ -138,7 +138,8 @@ func wsHandler(hub *ConnHub, w http.ResponseWriter, r *http.Request) {
138138
i++
139139
}
140140
names[i] = name
141-
client.conn.WriteJSON(names)
141+
namesJson, _ := json.Marshal(names)
142+
client.hub.broadcast <- namesJson
142143

143144
// separate reads and writes to conform to WebSocket standard of one concurrent reader and writer
144145
go client.writePump()

0 commit comments

Comments
 (0)