修改命令行窗口的bug

This commit is contained in:
weidong 2023-11-16 18:16:52 +08:00
parent f5be484563
commit e7df85fdfa
5 changed files with 61 additions and 11 deletions

View File

@ -4,10 +4,28 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
@Configuration
public class WebSocketConfig {
public class WebSocketConfig extends ServerEndpointConfig.Configurator{
/* 修改握手,就是在握手协议建立之前修改其中携带的内容 */
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
HttpSession httpSession = (HttpSession)request.getHttpSession();
if (httpSession != null) {
// 读取session域中存储的数据
sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
super.modifyHandshake(sec, request, response);
}
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
}

View File

@ -38,7 +38,8 @@ public class CmdLineController extends BasicController{
/****** 发送指令 *******/
@PostMapping(value = "/gnss/config_cmd")
@ResponseBody
public HttpResult configCmd(@RequestParam("tx_win") String cmd,
public HttpResult configCmd(HttpSession session,
@RequestParam("tx_win") String cmd,
@RequestParam("device_id") String deviceId,
@RequestParam("cmd_type") int cmdType) {
String sendCmd = cmd.replaceAll(" +","");
@ -74,6 +75,7 @@ public class CmdLineController extends BasicController{
// 保存
GnssMsg gnssMsg = new GnssMsg();
gnssMsg.setTenantid(getTenantId(session));
gnssMsg.setCreatetime(new Date());
gnssMsg.setDeviceid(deviceId);
gnssMsg.setMsgtype(msgType);

View File

@ -1,31 +1,38 @@
package com.imdroid.beidou.controller;
import com.imdroid.beidou.config.WebSocketConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
//单例
@ServerEndpoint("/websocket")
@ServerEndpoint(value = "/websocket",configurator = WebSocketConfig.class)
@Component
@Slf4j
public class WebSocketServer {
// thread safety counter
// thread safety set to hold websocket objects
private static CopyOnWriteArraySet<Session> webSocketSet = new CopyOnWriteArraySet<>();
private static Map<String,Session> webSocketSet = new ConcurrentHashMap<>();
@OnOpen
public void onOpen(Session session){
webSocketSet.add(session);
HttpSession httpSession= (HttpSession) session.getUserProperties().get(HttpSession.class.getName());
webSocketSet.put(httpSession.getId(), session);
log.info("new websocket opened, total socket num: "+ webSocketSet.size());
}
@OnClose
public void onClose(Session session){
webSocketSet.remove(session);
HttpSession httpSession= (HttpSession) session.getUserProperties().get(HttpSession.class.getName());
if(httpSession!=null) {
webSocketSet.remove(httpSession.getId());
}
log.info("websocket closed, total socket num: "+ webSocketSet.size());
}
@ -41,7 +48,7 @@ public class WebSocketServer {
public static void sendMessageToAll(String msg){
try {
for (Session item : webSocketSet) {
for (Session item : webSocketSet.values()) {
item.getBasicRemote().sendText(msg);
}
}
@ -49,4 +56,5 @@ public class WebSocketServer {
log.error("websocket send msg error:", e);
}
}
}

View File

@ -0,0 +1,22 @@
package com.imdroid.beidou.controller;
import org.springframework.stereotype.Component;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Component
public class WebsocketListener implements ServletRequestListener{
@Override
public void requestInitialized(ServletRequestEvent sre) {
HttpSession session = ((HttpServletRequest) sre.getServletRequest()).getSession();
}
public WebsocketListener(){}
@Override
public void requestDestroyed(ServletRequestEvent arg0) {}
}

View File

@ -73,7 +73,7 @@
<hr>
</dd>
<dd>
<a href="/logout" class="login-out">退出登录</a>
<a href="/do_logout" class="login-out">退出登录</a>
</dd>
</dl>
</li>