我在Payara服務器上設置了一個Java EE端點,我嘗試使用Autobahn WebSockets連接Android客戶端。我有以下設置:「Connection refused」Autobahn Android to Java EE Endpoint
我的WebSocket端點服務器上:
public class CommunicationSocket extends Endpoint {
@Override
public void onOpen(Session aSession, EndpointConfig aConfig) {
aSession.addMessageHandler(new MessageHandler.Whole<byte[]>() {
@Override
public void onMessage(byte[] aMessage) {
// Do something fun
}
});
}
}
我註冊的WebSocket這樣:
public class RegisterSocket implements ServerApplicationConfig {
@Override
public Set<ServerEndpointConfig> getEndpointConfigs(
Set<Class<? extends Endpoint>> aEndpointClasses) {
Set<ServerEndpointConfig> result = new HashSet<>();
for (Class endpointClass : aEndpointClasses) {
if (endpointClass.equals(CommunicationSocket.class)) {
ServerEndpointConfig sec
= ServerEndpointConfig.Builder.create(endpointClass,
"/WebSocket").build();
result.add(sec);
}
}
return result;
}
@Override
public Set<Class<?>> getAnnotatedEndpointClasses(
Set<Class<?>> aScanned) {
return Collections.emptySet();
}
}
WebSocket的,現在可以達到ATT WS://本地主機:46588 /服務器/ WebSocket的。我已經證實,它具有以下JavaScript的工作中鉻:
function WebSocketTest() {
if ("WebSocket" in window) {
ws = new WebSocket("ws://localhost:46588/Server/WebSocket");
ws.onopen = function() {
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
alert("Message is received... \n" + received_msg);
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
然而,當我嘗試使用我的Android客戶端使用的高速公路連接,插座關閉,不能夠建立與消息的連接「連接被拒絕」。我用下面的代碼中的onCreate連接(mSocket是活動類的字段):
WebSocketHandler messageHandler = new WebSocketHandler() {
@Override
public void onOpen() {
System.out.println("Connected...");
}
@Override
public void onClose(int code, String reason) {
System.out.println("Socket closing: " + reason);
mSocket = null;
}
@Override
public void onTextMessage(String payload) {
System.out.println("Received: " + payload);
}
@Override
public void onBinaryMessage(byte[] payload) {
System.out.println("Message received: " + payload);
}
};
mSocket = new WebSocketConnection();
try {
String address = "ws://localhost:46588/Server/WebSocket";
mSocket.connect(address, messageHandler);
} catch (WebSocketException e) {
System.out.println("Could not connect to WebSocket: "
+ e.getMessage());
mSocket = null;
}
我有互聯網的權限我在我的表現爲這樣:
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
有沒有在我的代碼什麼或方法是錯誤的?是否有可能記錄/捕獲連接事件(握手或不能)以揭示連接被拒絕的原因?