Skip to content

Commit 893348a

Browse files
author
Christopher Strauss
committed
Gamechat im panel
1 parent bfa8d9b commit 893348a

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
useState,
3+
useEffect,
4+
useCallback,
5+
useContext,
6+
} from "react"
7+
import {
8+
useParams,
9+
} from "react-router-dom"
10+
import {
11+
useAuthStore,
12+
} from "./../store.js"
13+
import {
14+
StompContext,
15+
tfetch,
16+
doTry,
17+
} from "./../util.js"
18+
19+
export const GameChat = () => {
20+
let [messages, setMessages] = useState([]);
21+
22+
let stompClient = useContext(StompContext)
23+
let { gameId } = useParams()
24+
let auth = useAuthStore(state => state.auth)
25+
26+
useEffect(() => {
27+
stompClient.subscribe("/topic/chat/" + gameId, (message) => {
28+
let newMessage = JSON.parse(message.body)
29+
setMessages(previous => [...previous, newMessage])
30+
})
31+
32+
doTry(async () => {
33+
let chat = await tfetch("/api/chat/" + gameId, {
34+
method: "GET",
35+
headers: {
36+
"Authorization": "Bearer " + auth.token,
37+
"Content-Type": "application/json",
38+
},
39+
})
40+
setMessages(chat.messages || [])
41+
})
42+
43+
}, [stompClient, auth, gameId])
44+
45+
let onSendMessage = useCallback((event) => doTry(async () => {
46+
event.preventDefault()
47+
let data = new FormData(event.target)
48+
49+
stompClient.publish({
50+
destination: "/app/chat/send/",
51+
body: JSON.stringify({
52+
message: data.get("message"),
53+
id: gameId,
54+
}),
55+
56+
})
57+
}), [ stompClient, gameId ])
58+
59+
return (
60+
61+
<div>
62+
<div className="overflow-auto">
63+
<div>
64+
{messages.map(message => (
65+
<p>{message.user + ": " + message.message}</p>
66+
))}
67+
</div>
68+
</div>
69+
<form onSubmit={onSendMessage}>
70+
<input
71+
type="text"
72+
name="message"
73+
/>
74+
<button type="submit">Send</button>
75+
</form>
76+
</div>
77+
);
78+
}

src/main/client/src/feature/GamePanel.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import {
3131
useAuthStore,
3232
useGameStore,
3333
} from "../store.js"
34+
import {
35+
GameChat,
36+
} from "./GameChat.jsx"
3437

3538
export const GamePanel = ({zoom, setZoom}) => {
3639
return (
@@ -171,6 +174,9 @@ function Panel({zoom, setZoom}) {
171174
</div>
172175
</div>
173176
)}
177+
<div>
178+
<GameChat />
179+
</div>
174180
</>
175181
)
176182
}

src/main/java/com/bernd/ChatController.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
import com.bernd.model.Chat;
44
import com.bernd.model.ChatMessage;
55
import com.bernd.model.ChatRequest;
6+
import com.bernd.util.Auth;
7+
import java.security.Principal;
68
import java.util.ArrayList;
79
import java.util.List;
8-
import java.util.Objects;
910
import org.springframework.http.ResponseEntity;
1011
import org.springframework.messaging.core.MessageSendingOperations;
11-
import org.springframework.security.core.context.SecurityContextHolder;
12+
import org.springframework.messaging.handler.annotation.MessageMapping;
1213
import org.springframework.stereotype.Controller;
1314
import org.springframework.web.bind.annotation.GetMapping;
1415
import org.springframework.web.bind.annotation.PathVariable;
15-
import org.springframework.web.bind.annotation.PostMapping;
16-
import org.springframework.web.bind.annotation.RequestBody;
1716
import org.springframework.web.bind.annotation.ResponseBody;
1817

1918
@Controller
@@ -32,14 +31,14 @@ public class ChatController {
3231
@ResponseBody
3332
@GetMapping("/api/chat/{id}")
3433
public Chat getChat(@PathVariable String id) {
34+
System.out.println("GETCHAT " + id);
3535
return chats.get(id);
3636
}
3737

38-
@PostMapping("/api/send_chat")
39-
public ResponseEntity<?> sendChat(@RequestBody ChatRequest chatRequest) {
40-
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
41-
String user = Objects.toString(principal);
42-
ChatMessage message = new ChatMessage(chatRequest.message(), user);
38+
@MessageMapping("/chat/send/")
39+
public ResponseEntity<?> sendChat(ChatRequest chatRequest, Principal principal) {
40+
String user = Auth.getPrincipal(principal);
41+
ChatMessage message = new ChatMessage(chats.chats().size(), chatRequest.message(), user);
4342
Chat chat = chats.get(chatRequest.id());
4443
if (chat != null) {
4544
chat.messages().add(message);
@@ -49,7 +48,7 @@ public ResponseEntity<?> sendChat(@RequestBody ChatRequest chatRequest) {
4948
chat = new Chat(chatRequest.id(), messages);
5049
}
5150
chats.put(chat);
52-
operations.convertAndSend("/topic/chat/" + chat.id(), chat);
51+
operations.convertAndSend("/topic/chat/" + chat.id(), message);
5352
return ResponseEntity.ok().build();
5453
}
5554

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bernd.model;
22

33
public record ChatMessage(
4-
String message,
5-
String user) {
4+
int n,
5+
String message,
6+
String user) {
67
}

0 commit comments

Comments
 (0)