|
|
@@ -0,0 +1,124 @@
|
|
|
+package cn.seecoder.paas.util.socket;
|
|
|
+
|
|
|
+import cn.seecoder.paas.service.facade.k8s.ExecApi;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
+import lombok.extern.apachecommons.CommonsLog;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.websocket.OnClose;
|
|
|
+import javax.websocket.OnMessage;
|
|
|
+import javax.websocket.OnOpen;
|
|
|
+import javax.websocket.Session;
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+@ServerEndpoint(value = "/ws/terminal", configurator = ServerEndpointConfig.class)
|
|
|
+@NoArgsConstructor
|
|
|
+@Data
|
|
|
+@Component
|
|
|
+@CommonsLog
|
|
|
+public class WsServerEndpoint implements ApplicationContextAware {
|
|
|
+
|
|
|
+ private Session session;
|
|
|
+
|
|
|
+ private ExecApi execApi;
|
|
|
+
|
|
|
+ private Process process;
|
|
|
+
|
|
|
+ private Thread outThread;
|
|
|
+
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private InputStream inputStream;
|
|
|
+
|
|
|
+ private OutputStream outputStream;
|
|
|
+
|
|
|
+ @OnOpen
|
|
|
+ public void onOpen(Session session) {
|
|
|
+ this.session = session;
|
|
|
+ try {
|
|
|
+ this.execApi = applicationContext.getBean(ExecApi.class);
|
|
|
+ String namespace = session.getRequestParameterMap().get("namespace").get(0);
|
|
|
+ String podName = session.getRequestParameterMap().get("podName").get(0);
|
|
|
+ this.process = execApi.getTerminal(namespace, podName);
|
|
|
+ outputStream = process.getOutputStream();
|
|
|
+ inputStream = process.getInputStream();
|
|
|
+ outThread = new Thread(() -> {
|
|
|
+ try {
|
|
|
+ while (true){
|
|
|
+ try {
|
|
|
+ byte data[] = new byte[1024];
|
|
|
+ if (inputStream.read(data) != -1) {
|
|
|
+ session.getBasicRemote().sendText(new String(data));
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (!process.isAlive()) {
|
|
|
+ session.getBasicRemote().sendText("Timeout exited.\r");
|
|
|
+ session.close();
|
|
|
+ process.destroy();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ process.destroy();
|
|
|
+ try {
|
|
|
+ session.getBasicRemote().sendText("Exited.\r");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ session.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ outThread.start();
|
|
|
+ log.info("连接成功!\n");
|
|
|
+ this.session.getBasicRemote().sendText("连接成功!");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("连接失败!\n", e);
|
|
|
+ try {
|
|
|
+ this.session.getBasicRemote().sendText("连接失败!Error:" + e.getMessage() + "\n");
|
|
|
+ } catch (IOException ex) {
|
|
|
+ log.error("发送失败!\n", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClose
|
|
|
+ public void onClose() {
|
|
|
+ if (process != null) {
|
|
|
+ process.destroy();
|
|
|
+ }
|
|
|
+ log.info("连接中断!\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnMessage
|
|
|
+ public String onMessage(String text, Session session) {
|
|
|
+ if (this.process != null && this.outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.write(text.getBytes());
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
+ }
|
|
|
+}
|