-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (61 loc) · 2.34 KB
/
Copy pathscript.js
File metadata and controls
69 lines (61 loc) · 2.34 KB
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
package com.chat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.config.annotation.*;
import jakarta.persistence.*;
import java.time.LocalDateTime;
@SpringBootApplication
public class ChatApplication {
public static void main(String[] args) {
SpringApplication.run(ChatApplication.class, args);
}
}
// 1. DATABASE ENTITY
@Entity
class ChatMessage {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String sender;
private String content;
private LocalDateTime timestamp = LocalDateTime.now();
public ChatMessage() {}
public ChatMessage(String sender, String content) { this.sender = sender; this.content = content; }
// Getters/Setters
public String getSender() { return sender; }
public void setSender(String sender) { this.sender = sender; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
}
// 2. REPOSITORY
interface MessageRepository extends JpaRepository<ChatMessage, Long> {}
// 3. WEBSOCKET CONFIG
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").withSockJS();
}
}
// 4. CONTROLLER
@Controller
class ChatController {
private final MessageRepository repository;
public ChatController(MessageRepository repository) { this.repository = repository; }
@MessageMapping("/chat.sendMessage")
@SendTo("/topic/public")
public ChatMessage sendMessage(ChatMessage chatMessage) {
return repository.save(chatMessage);
}
}