SpringBoot+WebSocket实现多人在线聊天案例实例

网友投稿 299 2022-08-29


SpringBoot+WebSocket实现多人在线聊天案例实例

目录1.pom.xml2.消息实体类3.controller4.WebSocket的配置文件5.前端发送消息页面6.测试6.1.客户端A6.2.客户端B

1.pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.3

com.yl

chat01

0.0.1-SNAPSHOT

chat01

Demo project for Spring Boot

11

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-websocket

org.springframework.boot

spring-boot-starter-test

test

org.webjars

sockjs-client

1.1.2

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.3

com.yl

chat01

0.0.1-SNAPSHOT

chat01

Demo project for Spring Boot

11

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-websocket

org.springframework.boot

spring-boot-starter-test

test

org.webjars

sockjs-client

1.1.2

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.消息实体类

package com.yl.chat01.domain;

import java.io.Serializable;

public class Message implements Serializable {

private String name;

private String content;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

@Override

public String toString() {

return "Message{" +

"name='" + name + '\'' +

", content='" BZzZn+ content + '\'' +

'}';

}

}

3.controller

package com.yl.chat01.controller;

import com.yl.chat01.domain.Message;

import org.springframework.messaging.handler.annotation.MessageMapping;

import org.springframework.messaging.handler.annotation.SendTo;

import org.springframework.stereotype.Controller;

@Controller

public class GreetingController {

@MessageMapping("/hello") //发送消息请求

@SendTo("/topic/greetings") //消息广播

public Message greeting(Message message) {

return message;

}

}

4.WebSocket的配置文件

package com.yl.chat01.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.messaging.simp.config.MessageBrokerRegistry;

import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;

import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration

@EnableWebSocketMessageBroker

public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

// 注册端点,用于前端建立连接的

@Override

public void registerStompEndpoints(StompEndpointRegistry registry) {

registry.addEndpoint("/chat").withSockJS();

}

// 配置消息代理,通过广播的形式来传递消息

@Override

public void configureMessageBroker(MessageBrokerRegistry registry) {

registry.enableSimpleBroker("/topic");

}

}

5.前端发送消息页面

6.测试

6.1.客户端A

6.2.客户端B


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python中的abstractmethod(python中的//是什么意思)
下一篇:python的web框架知识点(基于Python的主流Web开发框架研究及实现)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~