cuốn sách gpt4 ai đã làm

SpringBoot + WebSocket triển khai trường hợp trò chuyện trực tuyến điểm-điểm

In lại Tác giả: Người biết Thời gian cập nhật: 2024-03-13 08:13:12 27 4
mua khóa gpt4 Nike

1.pom.xml

  4.0.0  org.springframework.boot spring -boot-starter-parent 2.4.2    com.yl chat01 0.0.1-SNAPSHOT chat01 Dự án demo cho Spring Boot  11    org.springframework.boot spring-boot-starter-web   org.springframework.boot spring- boot-starter-security   org.springframework.boot spring-boot-starter-websocket   org.springframework.boot spring- boot-starter-test test    org.webjars sockjs-client 1.1.2  < dependency> org.webjars stomp-websocket 2.3.3   org.webjars jquery 3.5.1   org.webjars webjars-locator-core       org.springframework.boot spring-boot-maven-plugin    

2. Lớp thực thể

gói com.yl.chat01.domain; nhập java.io.Serializable; nhập java.util.Date; lớp công khai Trò chuyện triển khai Chuỗi riêng tư từ; Nội dung chuỗi riêng tư; { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to } public void setTo(String to) { this.to = to; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getDate() { return date } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "Chat{" + "from='" + from + '\'' + ", to='" + to + '\'' + ", content='" + nội dung + '\'' + ", date=" + ngày + '}';

3.Cấu hình WebSocket

gói com.yl.chat01.config; nhập org.springframework.context.annotation.Configuration; nhập org.springframework.messaging.simp.config.MessageBrokerRegistry; nhập org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; org.springframework.web.socket.config.annotation.StompEndpointRegistry; nhập org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker lớp công khai WebSocketConfig triển khai WebSocketMessageBrokerConfigurer { //Điểm cuối đăng ký, được sử dụng cho kết nối giao diện người dùng thành lập @Override public void registerStompEndpoints(StompEndpointRegistry register) { register.addEndpoint("/chat").setAllowedOrigins("http://localhost:8080").withSockJS(); } // Định cấu hình proxy tin nhắn để gửi tin nhắn thông qua phát sóng @Override public void configureMessageBroker (Đăng ký MessageBrokerRegistry) { register.enableSimpleBroker("/topic","/queue");

4. Cấu hình bảo mật, cấu hình tên người dùng và mật khẩu để đăng nhập

gói com.yl.chat01.config; nhập org.springframework.context.annotation.Configuration; nhập org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; nhập org.springframework.security.config.annotation.web. builders.HttpSecurity nhập khẩu; org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig mở rộng WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) ném Ngoại lệ { auth.inMemoryAuthentication() .withUser("admin") .password ("{noop}123") .roles("admin") .and() .withUser("root") .password("{noop}123") .roles("admin"); } @Override protected void configure(HttpSecurity http) ném Ngoại lệ { http .authorizeRequests() .anyRequest().authenticated() .and().formLogin().permitAll();

5. bộ điều khiển

gói com.yl.chat01.controller; nhập com.yl.chat01.domain.Chat; nhập org.springframework.beans.factory.annotation.Autowired; nhập org.springframework.messaging.handler.annotation.MessageMapping; nhập .messaging.simp.SimpMessagingTemplate; org.springframework.stereotype.Controller; nhập java.security.Principal; nhập java.util.Date; @Controller lớp công khai GreetingController { // /** // * Trò chuyện nhóm // * tin nhắn @param // * @return / / */ // @MessageMapping("/hello") //Gửi yêu cầu tin nhắn// @SendTo("/topic/greetings") //Tin nhắn phát// Tin nhắn công khai lời chào (Tin nhắn tin nhắn) { // trả lại tin nhắn; // } //Mẫu tin nhắn gửi trò chuyện trực tuyến @Autowired SimpMessagingTemplate simpMessagingTemplate /** * Trò chuyện đơn* @param hiệu trưởng * @param trò chuyện */ @MessageMapping("/onlineChat") public void chat(Hiệu trưởng, Trò chuyện trò chuyện) { chat.setFrom(principal.getName()); simpMessagingTemplate.convertAndSendToUser(chat.getTo(),"/queue/chat",chat);

5. Trang đầu

    Tiêu đề      
Xem sitemap của VNExpress

6. Kiểm tra

6.1 Khách hàng A, đăng nhập bằng quản trị viên

6.2 Client B, đăng nhập với tư cách người dùng root

6.3 Gửi tin nhắn cho nhau
Khách hàng A:

Khách hàng B:

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress