Skip to content

Commit 6ce84ad

Browse files
Christopher Straussh908714124
Christopher Strauss
authored andcommitted
Gamechat im panel
1 parent bfa8d9b commit 6ce84ad

File tree

4 files changed

+100
-12
lines changed

4 files changed

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

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 className="absolute bottom-10 pr-2">
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)