当前位置: 首页 > 知识库问答 >
问题:

WebSocket RemoteEndpoint无法将数据发送回客户端的问题

何昆
2023-03-14

我正在尝试创建类似node-websockify的东西,它基本上是一个简单的代理服务器,用于将数据从novnc服务器传输到novnc客户机,我使用了这里提到的方法

我有奇怪的错误发生时,发送ByteBuffer到远程客户端。如前所述,client是一个noVNC客户机,node-websockify和tightvnc工作得很好


import org.eclipse.jetty.websocket.server.WebSocketHandler;



org.eclipse.jetty.server.Server serverr = new org.eclipse.jetty.server.Server(8090);            
WebSocketHandler wsh = new WebSockify();
serverr.setHandler(wsh);
serverr.start();
serverr.join();


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.api.extensions.Frame;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebSocket
public class WebSockify extends WebSocketHandler {

    private static String authorizationHeader = "";

    Socket vncSocket;
    int vncPort = 5900;
    String vncPassword = "1234560";

    @Override
    public void configure(WebSocketServletFactory webSocketServletFactory) {
        webSocketServletFactory.register(WebSockify.class);
    }

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        System.out.println("Received handle " + target);
        try {
            if (this.getWebSocketFactory().isUpgradeRequest(request, response)) {
                System.out.println("Adding header");

                response.addHeader("Sec-WebSocket-Protocol", "binary");

                if (this.getWebSocketFactory().acceptWebSocket(request, response)) {

                    System.out.println("websocket accepted");
                    baseRequest.setHandled(true);

                    return;
                }
                System.out.println("websocket not accepted");
                if (response.isCommitted()) {
                    System.out.println("response commited.");
                    return;
                }
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
            super.handle(target, baseRequest, request, response);
        }
    }

    @OnWebSocketConnect
    public void onConnect(final Session session) throws IOException, InterruptedException {
        System.out.println("Connect: " + session.getRemoteAddress().getAddress());
        System.out.println(session.getUpgradeRequest().getRequestURI());        

        System.out.println("session open ? " + session.isOpen());
        vncSocket = new Socket("127.0.0.1", vncPort);
        Thread readThread = new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println("session open ? " + session.isOpen());
                    byte[] b = new byte[1500];
                    int readBytes;
                    while (true) {
                        readBytes = vncSocket.getInputStream().read(b);
                        System.out.println("session open ? " + session.isOpen());
                        System.out.println("read bytes " + readBytes + ", " + new String(b));
                        if (readBytes == -1) {
                            break;
                        }
                        if (readBytes > 0) {
                            System.out.println("session open ? " + session.isOpen());
                            ByteBuffer bb = ByteBuffer.wrap(b, 0, readBytes);
                            session.getRemote().sendBytes(bb);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    System.err.println(e);
                }
            }
        });
        readThread.start();

    }

    @OnWebSocketFrame
    public void onFrame(Frame f) throws IOException {
        System.out.printf("Frame: %d\n", f.getPayloadLength());
        byte[] data = new byte[f.getPayloadLength()];
        f.getPayload().get(data);
        vncSocket.getOutputStream().write(data);
    }

    @OnWebSocketError
    public void onError(Throwable cause) {
        System.err.println(cause.getMessage());
    }
}

看起来session.open()返回true,但下一行session.getRemote().sendBytes()导致错误。你能帮我弄清楚为什么吗?

当我在线程中使用会话时,问题就会发生,我的意思是,如果我没有创建一个新线程,session.getRemote().sendbytes()就可以正常工作。

共有1个答案

卞轶
2023-03-14

这与线程无关,实际上是由于客户端和服务器之间的握手错误造成的

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        System.out.println("Received handle " + target);
        try {
            if (this.getWebSocketFactory().isUpgradeRequest(request, response)) {
                System.out.println("Adding header");

                 // NO NEED, the client already put this header in the request
                //so no need to be added again
                //response.addHeader("Sec-WebSocket-Protocol", "binary");

                if (this.getWebSocketFactory().acceptWebSocket(request, response)) {

                    System.out.println("websocket accepted");
                    baseRequest.setHandled(true);

                    return;
                }
                System.out.println("websocket not accepted");
                if (response.isCommitted()) {
                    System.out.println("response commited.");
                    return;
                }
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
             // has to be removed, because this cause an Upgrade header to be added with is already existed and causes .
            //super.handle(target, baseRequest, request, response);
        }
    }
 类似资料:
  • 我正在Unity中制作一个游戏,我试图将数据从客户端发送到服务器并返回到客户端(试图保存实例),但当我收到数据并尝试将数据发送回客户端时,它表示udp客户端未连接。 它成功地将数据从我的Unity客户端发送到服务器,但一旦它到达那里,套接字就会断开连接,我就无法返回任何内容。正如你所看到的,我试图设置一些多播选项,但它似乎不能正常工作。 客户: 服务器: 因此,服务器中UdpClient的实例会保

  • 我有一个TCP服务器和一个客户端,简单的TCP服务器将接收传入的数据并打印出来,而客户端将继续创建一个套接字连接并循环发送数据到TCP服务器。 我得到的信息是,如果一个TCP连接被正确地关闭了,这个过程应该会继续下去,不会发生任何崩溃。 但在从客户端接收到一定数量的数据到服务器之后,客户端会崩溃,并出现错误 有没有什么办法可以避免这次撞车,让它持续运行呢?我是新来的所以如果我犯了什么愚蠢的错误,我

  • 使用worker来做服务器,没有用GatewayWorker,如何实现向指定用户推送消息? <?php use WorkermanWorker; require_once __DIR__ . '/Workerman/Autoloader.php'; // 初始化一个worker容器,监听1234端口 $worker = new Worker('websocket://workerman.net:1

  • 我需要实现一个github授权,然后将接收到的数据(JSON)发送到客户端。 我找到了这篇教程http://shiya.io/how-to-do-3-legged-oauth-with-github-a-general-guide-by-example-with-node-js/ 在该教程中,开发人员向我们展示了如下内容:“/”->“/login”->“/redirect”->“/user”(此处

  • 使用worker来做服务器,没有用GatewayWorker,如何实现向指定用户推送消息? <?php use WorkermanWorker; require_once __DIR__ . '/Workerman/Autoloader.php'; // 初始化一个worker容器,监听1234端口 $worker = new Worker('websocket://workerman.net:1