1、pom.xml导入websocket依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
2、websocket握手拦截器
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.Map;
@Component
public class HandShakeInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
if (serverHttpRequest instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
String userName = "websocket_test";
map.put("WEBSOCKET_USERNAME", userName);
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
}
}
3、websocket实现
import com.util.ResultUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class WebSocketHander implements WebSocketHandler {
private static final List<WebSocketSession> USERS = new ArrayList<>();
@Override
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
System.out.println("---------------------------websocket连接成功-------------------------");
USERS.add(webSocketSession);
String username = (String) webSocketSession.getAttributes().get("WEBSOCKET_USERNAME");
if (username != null) {
for (WebSocketSession wss : USERS) {
wss.sendMessage(new TextMessage(ResultUtils.successWithMessage("websocket连接成功!")));
}
}
}
@Override
public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {
}
@Override
public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
USERS.remove(webSocketSession);
}
@Override
public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
USERS.remove(webSocketSession);
}
@Override
public boolean supportsPartialMessages() {
return false;
}
public void sendMessageToUsers(Object obj, String code) {
TextMessage message;
if (code.equals("success")) {
message = new TextMessage(ResultUtils.successWithDataAndMessage(obj, "websocket发送数据!"));
} else {
message = new TextMessage(ResultUtils.errorWithMessage((String) obj));
}
for (WebSocketSession user : USERS) {
try {
if (user.isOpen()) {
user.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void sendMessageToUser(String userName, Object obj) {
TextMessage message = new TextMessage(ResultUtils.successWithDataAndMessage(obj, "websocket发送数据!"));
for (WebSocketSession user : USERS) {
if (user.getAttributes().get("WEBSOCKET_USERNAME").equals(userName)) {
try {
if (user.isOpen()) {
user.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
}
4、XML文件配置请求路径
<bean id="customHandler" class="com.controller.websocket.WebSocketHander"/>
<websocket:handlers>
<websocket:mapping path="/websocket.do" handler="customHandler"/>
<websocket:handshake-interceptors>
<bean class="com.controller.websocket.HandShakeInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>
5、websocket请求路径
ws://localhost/websocket.do
|