Commit 2c9d89b2 by 邓敏

WebSocket

1 parent 72047ffc
package com.subsidy.util;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpAddressUtil {
public static String getIpAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
if (ipAddress != null && ipAddress.length() > 15) {
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
}
package com.subsidy.util.websocket; package com.subsidy.util.websocket;
import com.subsidy.mapper.OprMemDictMapper;
import com.subsidy.model.OprMemDictDO;
import com.subsidy.util.DateFormatUtil; import com.subsidy.util.DateFormatUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
...@@ -23,6 +26,9 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -23,6 +26,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Component @Component
public class WebSocketUtil implements WebSocketHandler { public class WebSocketUtil implements WebSocketHandler {
@Autowired
private OprMemDictMapper oprMemDictMapper;
/** /**
* 存放建立连接webSocket对象 * 存放建立连接webSocket对象
*/ */
...@@ -74,10 +80,20 @@ public class WebSocketUtil implements WebSocketHandler { ...@@ -74,10 +80,20 @@ public class WebSocketUtil implements WebSocketHandler {
*/ */
@Override @Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
webSocketMap.remove(session); if(null != session) {
System.out.println("<---- webSocket is close"); webSocketMap.remove(session);
log.info("<---- webSocket is close");
log.info("session {} close, status: {}", session.getId(), closeStatus); OprMemDictDO oprMemDictDO = new OprMemDictDO();
String params = session.getUri().getQuery();
String[] id = params.split("=");
oprMemDictDO.setUserId(Long.valueOf(id[1]));
oprMemDictDO.setOprType("登出");
oprMemDictDO.setIpAddress(session.getRemoteAddress().getHostName());
oprMemDictMapper.insert(oprMemDictDO);
System.out.println("<---- webSocket is close");
log.info("<---- webSocket is close");
log.info("session {} close, status: {}", session.getId(), closeStatus);
}
} }
/** /**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!