作者:email_osoo_240 | 来源:互联网 | 2023-05-19 03:41
1、相关配置文件:
1)web.xml的配置内容如下:
contextConfigLocation
classpath:conf/spring.xml;
webAppRootKey
springmvc.root
springMVC-web
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:conf/spring-mvc.xml
1
springMVC-web
/
2)spring-mvc.xml的配置内容如下:
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cOntext="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket.xsd">
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/page/" p:suffix=".jsp" />
3)spring.xml的配置如下:
xmlns:cOntext="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
class="org.springframework.validation.DefaultMessageCodesResolver">
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
2、前台代码如下:
<%@ page language="java" cOntentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Welcome
3、后台代码结构如如下:
4、后台代码如下
1)WebSocketConfig.java
package websocket.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import websocket.handler.SystemWebSocketHandler;
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/webSocketServer");
}
@Bean
public WebSocketHandler myHandler() {
return new SystemWebSocketHandler();
}
}
2)SystemWebSocketHandler.java
package websocket.handler;
import java.io.IOException;
import java.util.ArrayList;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
public class SystemWebSocketHandler implements WebSocketHandler {
private static final ArrayList users = new ArrayList();;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
users.add(session);
System.out.println("ConnectionEstablished"+"=>当前在线用户的数量是:"+users.size());
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage> message) throws Exception {
System.out.println(message.getPayload());
TextMessage returnMessage = new TextMessage("received at server");
sendMessageToUsers(returnMessage);
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
if(session.isOpen()){
session.close();
}
users.remove(session);
}
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
users.remove(session);
System.out.println("ConnectionClosed"+"=>当前在线用户的数量是:"+users.size());
}
@Override
public boolean supportsPartialMessages() {
return false;
}
/**
* 给所有在线用户发送消息
* @param message
*/
public void sendMessageToUsers(TextMessage message) {
for (WebSocketSession user : users) {
if (user.isOpen()) {
try {
for(int i = 0;i <5;i++ ){
user.sendMessage(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
5、测试方法与上一篇博客一样,此处不再赘述。
注意:本博客中用红颜色标注的地方还是挺重要的,控制跳转路径