我可以使用Java 1.6建立WebSocket连接吗?

时间:2019-02-20 12:11:26

标签: java websocket java-6

以前,我使用TCP套接字建立连接,但是消息传递存在问题,现在我正在使用WebSockets进行尝试。问题是我需要使用Java 6建立连接,并且我使用Jetty作为服务器,但是Jetty是否支持Java 6?我知道它与Java 8兼容,但是在这种情况下我不能使用Java 8。

这是我用来构建TCPClient的代码,有人可以帮助我使用带有Java 6的WebSockets建立连接。

public static void main (String args[]) {

        String host = "192.168.14.214";
        int port = 9080;
        new Client(host, port);
    }

    public Client(String host, int port) {
        try {
            String serverHostname = new String("192.168.14.214");

            System.out.println("Connecting to host " + serverHostname + " on port " + port + ".");

            Socket echoSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;

            try {
                echoSocket = new Socket(serverHostname, 9080);
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));

            } catch (UnknownHostException e) {
                System.err.println("Unknown host: " + serverHostname);
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Unable to get streams from server");
                System.exit(1);
            }

            AuthReqBean authReqBean = new AuthReqBean();
            AuthReqBean.AuthRequest authReq = authReqBean.new AuthRequest();
            authReq.setLgnNme("username");
            authReq.setPwd("asd");
            authReqBean.setAuthRequest(authReq);
            ObjectWriter ow = new 
ObjectMapper().writer().withDefaultPrettyPrinter();
            String json = ow.writeValueAsString(authReqBean);
            System.out.println(json);

            out.write(json);
       while (true) {
                System.out.print("client: ");
                String userInput = in.readLine();
                System.out.println("server: " + userInput);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我尝试使用websockets建立一些测试代码来建立连接,但是每次使用Java 6时连接失败,但是当Java 8正常工作时。

public class SimpleEchoClient {
    public static void main(String[] args)
    {
        String destUri = "ws://echo.websocket.org";

        WebSocketClient client = new WebSocketClient();
        SimpleEchoSocket socket = new SimpleEchoSocket();
        try
        {
            client.start();

            URI echoUri = new URI(destUri);
            ClientUpgradeRequest request = new ClientUpgradeRequest();
            client.connect(socket,echoUri,request);
            System.out.printf("Connecting to : %s%n",echoUri);
            socket.awaitClose(5,TimeUnit.SECONDS);
 }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
        finally
        {
            try
            {
                client.stop();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

 public SimpleEchoSocket()
    {
        this.closeLatch = new CountDownLatch(1);
    }

    public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException
    {
        return this.closeLatch.await(duration,unit);
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason)
    {
        System.out.printf("Connection closed: %d - %s%n",statusCode,reason);
        this.session = null;
        this.closeLatch.countDown(); // trigger latch
    }

    @OnWebSocketConnect
    public void onConnect(Session session)
    {
        System.out.printf("Got connect: %s%n",session);
        this.session = session;
        try
        {
            Future<Void> fut;
            fut = session.getRemote().sendStringByFuture("Hello");
            fut.get(2,TimeUnit.SECONDS); // wait for send to complete.

            fut = session.getRemote().sendStringByFuture("Thanks for the conversation.");
            fut.get(2,TimeUnit.SECONDS); // wait for send to complete.

            session.close(StatusCode.NORMAL,"I'm done");
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(String msg)
    {
        System.out.printf("Got msg: %s%n",msg);
    }

0 个答案:

没有答案