Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using java websocket open container shell successfully but no response with following command #46

Open
yaoyuanming opened this issue May 16, 2021 · 0 comments

Comments

@yaoyuanming
Copy link

this is the websocket between font end and back end:

File configFile = new File("config/kubeconfig");
final String configYAML;
configYAML = String.join("\n",Files.readAllLines(configFile.toPath()));
Config kubernetesConfig = Config.fromKubeconfig(configYAML);
kubernetesConfig.setTrustCerts(true);

String urlStr = "https://127.0.0.1/api/v1/namespaces/namespace1/pods/pod1/exec?container=container1&stdin=true&stdout=true&stderr=true&tty=true&command=sh&pretty=true&follow=true";
URL url = new URL(urlStr);
clientWebSocketListener = initServerWebSocket(url, kubernetesConfig);
clientWebSocketListener.setSession(session);

@OnMessage
public void onMessage(Session session, byte[] messages) throws UnsupportedEncodingException {
    if (Objects.isNull(session)) {
            return;
        }
        String sessionId = session.getId();
        WebsocketMapping websocketMapping = clients.get(sessionId);
        ClientWebSocketListener listener = websocketMapping.getListener();
        try {
            // 向apiServer发送消息
            listener.waitUntilReady(1);
            listener.send(message.getBytes(), message.length);
        } catch (IOException | InterruptedException ie) {
            // 通知发送方
            session.getAsyncRemote().sendText("向apiServer发送消息失败!失败消息内容: " + message);
        }
}

private static ClientWebSocketListener initServerWebSocket(URL url, Config  config) {
        OkHttpClient client = HttpClientUtils.createHttpClient(config);
        final ClientWebSocketListener serverWebSocketListener = new ClientWebSocketListener();
        final Request.Builder request = new Request.Builder().url(url).get();
        final OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
        clone.newWebSocket(request.build(), serverWebSocketListener);
        return serverWebSocketListener;

    }

here is the onmessage in websocket between apiserver and back end

@Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {
        readyLatch.countDown();
        this.getSession().getAsyncRemote().sendBinary(ByteBuffer.wrap(bytes.toByteArray()));
    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {
        readyLatch.countDown();
        this.getSession().getAsyncRemote().sendText(text);

    }

the problem is this url open container shell successfully but have no response in the following command like ls, or cd bin/. is there are some format conver I miss or some step I miss?

this question confuse me for a long time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant