0
我正在測試使用javascript websocket連接的聊天服務應用程序(javascript和java)。一切工作正常在我自己的機器上。我在我的Google計算引擎(GCE)實例上部署了該應用程序。當我嘗試使用聊天服務時,連接失敗(錯誤代碼:1006)。我將發佈以下谷歌計算引擎實例上的Websocket連接失敗
相關代碼WebSocket連接的JavaScript代碼是
var server;
try {
server = new WebSocket('ws://' + window.location.host +
'<c:url value="/chat/${chatSessionId}" />');
server.binaryType = 'arraybuffer';
} catch(error) {
modalErrorBody.text(error);
modalError.modal('show');
return;
}
server.onerror = function(event) {
modalErrorBody.text(event.data);
modalError.modal('show');
};
我嘗試以下替代
- 與實際的外部IP地址替換window.location.host( ***以下)
server = new WebSocket('ws://***.***.**.*' + '<c:url value="/chat/${chatSessionId}" />');
- 添加端口號的外部IP地址
server = new WebSocket('ws://***.***.**.*:80' + '<c:url value="/chat/${chatSessionId}" />');
無這些工作之後。相同的錯誤消息(出錯代碼:1006)
這裏是代碼的Java的一部分:
@ServerEndpoint(value = "/chat/{sessionId}",
encoders = ChatMessageCodec.class,
decoders = ChatMessageCodec.class,
configurator = ChatEndpoint.EndpointConfigurator.class)
@WebListener
public class ChatEndpoint implements HttpSessionListener
{
private static final String HTTP_SESSION_PROPERTY = "com.wrox.ws.HTTP_SESSION";
private static final String WS_SESSION_PROPERTY = "com.wrox.http.WS_SESSION";
private static long sessionIdSequence = 1L;
private static final Object sessionIdSequenceLock = new Object();
private static final Map<Long, ChatSession> chatSessions = new Hashtable<>();
private static final Map<Session, ChatSession> sessions = new Hashtable<>();
private static final Map<Session, HttpSession> httpSessions =
new Hashtable<>();
public static final List<ChatSession> pendingSessions = new ArrayList<>();
@OnOpen
public void onOpen(Session session, @PathParam("sessionId") long sessionId)
{
...
}
@OnMessage
...
@OnClose
...
}