修改命令行窗口的bug
This commit is contained in:
parent
f5be484563
commit
e7df85fdfa
@ -4,10 +4,28 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
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
|
@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
|
@Bean
|
||||||
public ServerEndpointExporter serverEndpointExporter(){
|
public ServerEndpointExporter serverEndpointExporter(){
|
||||||
return new ServerEndpointExporter();
|
return new ServerEndpointExporter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,7 +38,8 @@ public class CmdLineController extends BasicController{
|
|||||||
/****** 发送指令 *******/
|
/****** 发送指令 *******/
|
||||||
@PostMapping(value = "/gnss/config_cmd")
|
@PostMapping(value = "/gnss/config_cmd")
|
||||||
@ResponseBody
|
@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("device_id") String deviceId,
|
||||||
@RequestParam("cmd_type") int cmdType) {
|
@RequestParam("cmd_type") int cmdType) {
|
||||||
String sendCmd = cmd.replaceAll(" +","");
|
String sendCmd = cmd.replaceAll(" +","");
|
||||||
@ -74,6 +75,7 @@ public class CmdLineController extends BasicController{
|
|||||||
|
|
||||||
// 保存
|
// 保存
|
||||||
GnssMsg gnssMsg = new GnssMsg();
|
GnssMsg gnssMsg = new GnssMsg();
|
||||||
|
gnssMsg.setTenantid(getTenantId(session));
|
||||||
gnssMsg.setCreatetime(new Date());
|
gnssMsg.setCreatetime(new Date());
|
||||||
gnssMsg.setDeviceid(deviceId);
|
gnssMsg.setDeviceid(deviceId);
|
||||||
gnssMsg.setMsgtype(msgType);
|
gnssMsg.setMsgtype(msgType);
|
||||||
|
|||||||
@ -1,31 +1,38 @@
|
|||||||
package com.imdroid.beidou.controller;
|
package com.imdroid.beidou.controller;
|
||||||
|
|
||||||
|
import com.imdroid.beidou.config.WebSocketConfig;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
import javax.websocket.*;
|
import javax.websocket.*;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
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
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WebSocketServer {
|
public class WebSocketServer {
|
||||||
// thread safety counter
|
// thread safety counter
|
||||||
// thread safety set to hold websocket objects
|
// thread safety set to hold websocket objects
|
||||||
private static CopyOnWriteArraySet<Session> webSocketSet = new CopyOnWriteArraySet<>();
|
private static Map<String,Session> webSocketSet = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@OnOpen
|
@OnOpen
|
||||||
public void onOpen(Session session){
|
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());
|
log.info("new websocket opened, total socket num: "+ webSocketSet.size());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClose
|
@OnClose
|
||||||
public void onClose(Session session){
|
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());
|
log.info("websocket closed, total socket num: "+ webSocketSet.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +48,7 @@ public class WebSocketServer {
|
|||||||
|
|
||||||
public static void sendMessageToAll(String msg){
|
public static void sendMessageToAll(String msg){
|
||||||
try {
|
try {
|
||||||
for (Session item : webSocketSet) {
|
for (Session item : webSocketSet.values()) {
|
||||||
item.getBasicRemote().sendText(msg);
|
item.getBasicRemote().sendText(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,4 +56,5 @@ public class WebSocketServer {
|
|||||||
log.error("websocket send msg error:", e);
|
log.error("websocket send msg error:", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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) {}
|
||||||
|
}
|
||||||
@ -73,7 +73,7 @@
|
|||||||
<hr>
|
<hr>
|
||||||
</dd>
|
</dd>
|
||||||
<dd>
|
<dd>
|
||||||
<a href="/logout" class="login-out">退出登录</a>
|
<a href="/do_logout" class="login-out">退出登录</a>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user