谷歌计算引擎实例上的Websocket连接失败

时间:2017-04-11 11:37:34

标签: javascript java websocket google-compute-engine

我正在测试使用javascript websocket连接的聊天服务应用程序(javascript和java)。在我自己的机器上一切正常。我在Google计算引擎(GCE)实例上部署了该应用程序。当我尝试使用聊天服务时,连接失败(错误代码:1006)。我将在下面发布相关代码

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');
            };

我尝试了以下替代方案

  1. 将window.location.host替换为实例
  2. 的实际外部IP地址(***下方)

    server = new WebSocket('ws://***.***.**.*' + '<c:url value="/chat/${chatSessionId}" />');

    1. 在外部IP地址之后添加端口号
    2. 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
          ...
      }
      

1 个答案:

答案 0 :(得分:0)

我发现在tomcat的server.xml中,以下部分被注释掉,导致问题

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
相关问题