未经过项目测试,只是自己做一下感受一下websocket的功能,非常好用,比轮询强多了。另外因为前端差,页面没怎么优化,简单看一下效果,方便自己以后用到,也希望对看到的朋友有帮助
使用springboot集成websocket要特意去注入serverEndpointExporter类关于此类我也不是很理解。我把简单理解为一种注解支持,因为springboot在打包时不会包含此类,如果是普通项目使用tomcat就不必单独注入了,在tomcat中已经包含此类。该类主要支持@ServerEndpoint注解,告知被注解的类是websoket的接入类
package com.example.sbwd.service;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @Author 小王
* @ClassName WebSocketConfig
* @Date 2020/6/22 17:23
* @Description TODO
* @Version 1.0
*/
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
也可以这样
package com.example.sbwd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@SpringBootApplication
public class SbwdApplication /*extends WebMvcConfigurationSupport*/ {
public static void main(String[] args) {
SpringApplication.run(SbwdApplication.class, args);
}
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
2,编写websoket接入类,带上@ServerEndpoint("入口名称")
package com.example.sbwd.controller;
import org.springframework.web.bind.annotation.RestController;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author 小王
* @ClassName WebSocket
* @Date 2020/6/22 17:26
* @Description TODO
* @Version 1.0
*/
@RestController
@ServerEndpoint("/WebSocket/{id}")
public class WebSocket {
// 存储会话
private static ConcurrentHashMap webSocket = new ConcurrentHashMap();
private String id;
private Session session;
/**
* 接入连接回调
*
* @param session 会话对象
* @param id 会话ID
* @throws Exception 异常
*/
@OnOpen
public void onOpen(Session session, @PathParam("id") String id) throws Exception {
this.id = id;
this.session = session;
webSocket.put(id, this);
// 检验后端能否正常给前端发送信息
// sendMessageToId(this.id, "前端你好,我是后端,我正在通过WebSocket给你发送消息");
System.out.println(id + "接入连接");
}
/**
* 关闭连接回调
*/
@OnClose
public void onClose() {
webSocket.remove(this.id);
System.out.println(this.id + "关闭连接");
}
/**
* 收到客户端发来消息回调
*
* @param message
* @param session
*/
@OnMessage
public void onMessage(String message, Session session ,@PathParam("id") String id) throws IOException {
//做简单的群聊在接受到页面消息的时候发出去,这里是发给全部,也可以发给个人,可以自己调
sendMessageToAll(message+":"+id);
System.err.println(this.id + "发来消息:" + message);
}
/**
* 会话出现错误回调
*
* @param session 会话对象
* @param error 错误信息
*/
@OnError
public void onError(Session session, Throwable error) {
}
/**
* 发送消息给客户端
*
* @param message 消息
* @throws IOException 异常
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 给指定的会话发送消息
*
* @param id 会话ID
* @param message 消息
* @throws IOException 异常
*/
public void sendMessageToId(String id, String message) throws IOException {
webSocket.get(id).sendMessage(message);
}
/**
* 群发消息
*
* @param message 消息
* @throws IOException 异常
*/
public void sendMessageToAll(String message) throws IOException {
for (String key : webSocket.keySet()) {
try {
webSocket.get(key).sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3,controller层
package com.example.sbwd.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author 小王
* @ClassName WebControllerDemo
* @Date 2020/6/22 11:06
* @Description TODO
* @Version 1.0
*/
@Controller
public class WebControllerDemo {
@RequestMapping(value = "test")
public String test(Model model){
return "index";
}
//模拟第一个人接入
@RequestMapping(value = "chat")
public String chat(){
return "chat/chat";
}
//模拟第二个人接入
@RequestMapping(value = "chat1")
public String chat1(){
return "chat/chat1";
}
//模拟第三个人接入
@RequestMapping(value = "chat2")
public String chat2(){
return "chat/chat2";
}
//模拟第四个人接入
@RequestMapping(value = "chat3")
public String chat3(){
return "chat/chat3";
}
//注:实际项目中写一个就够了,这里只是模拟有四个人登录
}
4,jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
form表单
底部div
1,启动项目之后根据你自己设置的路径访问,我这里是http://localhost:8888/sbwd/chat,http://localhost:8888/sbwd/chat1,http://localhost:8888/sbwd/chat2,http://localhost:8888/sbwd/chat3
2,后台打印内容
3,前台页面,刚进来四个都一样,然后现在chat里面发送一条消息
先看后台
再看chat1,chat2,chat3。如果你成功的话应该都是一样的,这里就不一一粘出来了
可以再试一下用chat2,chat3发送一条消息看chat1,会把之前的消息都接收到
chat3:
chat1:
这个不能直接使用在项目中,根据实际项目还要做出自己的调整,修正这里面的瑕疵。欢迎大家指出错误,请勿骂人!!!