java中的接口是类吗
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">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
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.前端发送消息页面
var stompClient;
$(function () {
//连接
$("#connect").click(function () {
connect()
})
//发送消息到后台
$("#send").click(function () {
stompClient.send("/hello",{},JSON.stringify({'name':$('#username').val(),'content':$("#content").val()}))
})
//断开连接
$("#disconnect").cilck(function () {
stompClient.disconnect();
disableButton(false);
})
})
function connect() {
if (!$("#username").val()) {
alert("请输入用户名")
return;
}
//BZzZn获取连接地址
var socketjs = new SockJS("/chat");
stompClient = Stomp.over(socketjs);
//建立连接
stompClient.connect({},function (frame) {
alert("connect success")
disableButton(true);
//订阅消息
stompClient.subscribe("/topic/greetings",function (obj) {
var msg = JSON.parse(obj.body);
$("#chat").append("
})
})
}
function disableButton(connected) {
$("#connect").prop("disabled",connected);
$("#disconnect").prop("disabled",!connected);
$("#send").prop("disabled",!connected);
}
6.测试
6.1.客户端A
6.2.客户端B
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~