繁体   English   中英

Java WebSocket与代理

[英]Java websocket with proxy

我已经整日整夜尝试了几天,试图使Websocket在Java中使用代理工作。 我尝试了不同的图书馆

https://github.com/TooTallNate/Java-WebSocket

https://github.com/AsyncHttpClient/async-http-client

但是可悲的是,这些库不支持带凭据的代理。 如果你们知道任何其他支持代理的库,那么我将不胜感激。

提前致谢

尝试使用nv-websocket-client库。 它支持在代理服务器上进行身份验证。 但是请注意,当前的实现仅支持基本身份验证。

// 1. Create a WebSocketFactory instance.
WebSocketFactory factory = new WebSocketFactory();

// 2. Set up information about a proxy server.
//    Credentials can be set here.
ProxySettings settings = factory.getProxySettings();
settings.setServer("http://proxy.example.com");
settings.setCredentials("id", "password");

// 3. Connect to a WebSocket endpoint via the proxy.
WebSocket ws = factory.createSocket("ws://websocket.example.com");

// 4. Add a listener to receive WebSocket events.
ws.addListener(new WebSocketAdapter() {
    @Override
    public void onTextMessage(WebSocket ws, String message) {
        // Received a text message.
        ......
    }
});

// 5. Perform a WebSocket opening handshake.
ws.connect();

// 6. Send frames.

// 6-1. Text
ws.sendText("Hello.");

// 6-2. Binary
byte[] binary = ......;
ws.sendBinary(binary);

// 6-3. Ping
ws.sendPing("Are you there?");

// 6-4. Pong (unsolicited pong; RFC 6455, 5.5.3. Pong)
ws.sendPong("I'm OK.");

// 6-5. Fragmented Frames
ws.sendText("How ", false)
  .sendContinuation("are ")
  .sendContinuation("you?", true);

// 6-6. Periodical Ping
ws.setPingInterval(60 * 1000);

// 6-7. Periodical Pong (unsolicited pong; RFC 6455, 5.5.3. Pong)
ws.setPongInterval(60 * 1000);

// 6-8. Close (if you want to send one manually).
ws.sendClose(WebSocketCloseCode.NORMAL, "Bye.");

// 7. Disconnect
ws.disconnect();

博客
WebSocket客户端库(Java SE 1.5+,Android)
http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html

GitHub上
https://github.com/TakahikoKawasaki/nv-websocket-client

的JavaDoc
http://takahikokawasaki.github.io/nv-websocket-client/

Maven的

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-websocket-client</artifactId>
    <version>1.3</version>
</dependency>

nv-websocket-client-1.3.jar为62,854字节,不需要任何外部依赖项。

您可以尝试Tyrus (Java EE中WebSocket API的参考实现); 客户端不需要运行任何Java EE服务器,如果您使用的是Java 7,则可以将客户端最小化为〜500kb

客户端行为代理依赖项应提供足够的信息以供尝试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM