The*_*son 9 java websocket node.js tyrus
我正在开发一个开源项目Storj.我正在编写一个连接到Node.js websocket后端的Java客户端.客户使用Tyrus.沟通应如下:
我遇到问题,因为@OnMessage永远不会被调用.我尝试使用一个简单的javascript客户端在线访问相同的URL并使用相同的标记:https: //www.websocket.org/echo.html
我确实得到了一个回应,它告诉我Java项目有问题.
在能够下载文件之前,在早期阶段,我可以毫无问题地上传文件.但是,该步骤不需要调用@OnMessage(它只是上传文件然后服务器与消息断开连接),所以我不确定我的@OnMessage是否正常工作.
以下是Websocket的相关代码(也可在Github上获得):https: //github.com/NutterzUK/storj-java-bridge-client/blob/master/storj-client/src/main/java/storj/io /client/websockets/WebsocketFileRetriever.java
package storj.io.client.websockets;
import com.google.gson.Gson;
import storj.io.restclient.model.FilePointer;
import javax.websocket.*;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Logger;
/**
* Created by steve on 12/07/2016.
*/
@ClientEndpoint
public class WebsocketFileRetriever {
private Logger logger = Logger.getLogger(this.getClass().getName());
private Gson gson = new Gson();
private FilePointer filePointer;
private File outputFile;
private AuthorizationModel authModel;
private CountDownLatch latch;
public WebsocketFileRetriever(FilePointer filePointer, File outputFile, CountDownLatch latch){
this.filePointer = filePointer;
this.latch = latch;
this.outputFile = outputFile;
authModel = new AuthorizationModel();
authModel.setToken(filePointer.getToken());
authModel.setOperation(filePointer.getOperation());
authModel.setHash(filePointer.getHash());
}
@OnMessage
public void onMessage(String s){
logger.info("Received ... " + s);
}
@OnMessage
public void onMessage(ByteBuffer message, Session session) {
logger.info("Received ...." + message);
}
@OnOpen
public void onOpen(Session session, EndpointConfig endpointConfig) {
logger.info("Opened");
try {
session.getBasicRemote().sendText(gson.toJson(authModel), true);
} catch (IOException e) {
e.printStackTrace();
}
logger.info("sent: " + gson.toJson(authModel));
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info("Closed Websocket: " + closeReason.getCloseCode() + " " + closeReason.getReasonPhrase());
//latch.countDown();
}
@OnError
public void onError(Session session, Throwable t) {
t.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
以及启动此websocket的代码,可在此处获得 https://github.com/NutterzUK/storj-java-bridge-client/blob/master/storj-client/src/main/java/storj/io/client/DefaultStorjClient .java:
CountDownLatch latch;
latch = new CountDownLatch(1);
ClientManager wsClient = ClientManager.createClient();
try {
wsClient.setDefaultMaxBinaryMessageBufferSize(Integer.MAX_VALUE);
wsClient.setDefaultMaxTextMessageBufferSize(Integer.MAX_VALUE);
logger.info("CONNECTING TO: " + "ws://" + pointer.getFarmer().getAddress() + ":" + pointer.getFarmer().getPort());
final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
wsClient.connectToServer(new WebsocketFileRetriever(pointer, encryptedOutputFile, latch), cec, new URI("ws://" + pointer.getFarmer().getAddress() + ":" + pointer.getFarmer().getPort()));
latch.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将Tyrus升级到最新版本,我得到了相同的结果.有任何想法吗?
此代码的输出是:
Aug 25, 2016 8:55:31 PM storj.io.client.DefaultStorjClient downloadFile
INFO: CONNECTING TO: ws://164.storj.eu:8607
Aug 25, 2016 8:55:35 PM storj.io.client.websockets.WebsocketFileRetriever onOpen
INFO: Opened
Aug 25, 2016 8:55:35 PM storj.io.client.websockets.WebsocketFileRetriever onOpen
INFO: sent: {"token":"06c36d4bac4f07ee1751068b5b2230f22e884b38","hash":"837b79bec927a1d8fa7fedd2ea0bb276e0d86e0f","operation":"PULL"}
Aug 25, 2016 8:56:11 PM storj.io.client.websockets.WebsocketFileRetriever onClose
INFO: Closed Websocket: NORMAL_CLOSURE Closing
Run Code Online (Sandbox Code Playgroud)
发送消息后,它会在@OnClose的"NORMAL_CLOSURE"消息之前挂起一段时间.
更新:一种非常简单的方法来运行它来重现问题
我已经在git存储库中添加了一个测试用户名和密码,所以可用的代码在这里:https://github.com/NutterzUK/storj-java-bridge-client
要运行它,您只需运行storj.io.client.main.MainTest
快速浏览它的功能.它将首先发送一些HTTP请求以获取令牌.它将使用该令牌通过websocket连接到某人的机器,并将该令牌作为文本发送.作为响应,它应该接收一个文件作为字节.
在连接之前,它会打印出令牌及其即将连接的地址.在关闭之前它会挂起一点,并且不会调用onMessage方法.为了测试,如果你在其中放置一个System.exit(在DefaultStorjClient.java中取消注释第152行),它将不会连接,因此你可以在另一个客户端使用该令牌.我已经使用https://www.websocket.org/echo.html进行了测试(确保您的浏览器允许不安全的网址,因为它不是"wss"),要在Chrome中执行此操作,您需要点击顶部的屏蔽是的.我可以看到服务器确实响应:

这表明确实发送了一个blob以响应文本消息,但Tyrus中的@OnMessage从未被触发.
最后我改用TallNate,这个问题就不存在了。
我计时后发现它总是在30秒后断开连接。通常响应速度比 30 秒快,所以我不确定为什么它挂起然后断开连接。我尝试在 Tyrus 中设置超时,但它仍然在 30 秒处断开连接。最后,我尝试了 TallNate,看看是否可以在那里设置超时……并且开箱即用。
https://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
| 归档时间: |
|
| 查看次数: |
519 次 |
| 最近记录: |