OnMessage(WebSocket)在Glassfish,Java中不起作用8

问题描述:

我在运行我的Java EE应用程序时遇到问题。我有以下代码服务器edpoint:OnMessage(WebSocket)在Glassfish,Java中不起作用8

import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.enterprise.context.ApplicationScoped; 
import javax.websocket.*; 
import javax.websocket.server.ServerEndpoint; 



@ApplicationScoped 
@ServerEndpoint(
     value = "/model" 
) 
public class ModelWebSocket { 


    private final ModelHandle model = new ModelHandle(); 

    @OnOpen 
    public void onOpen(Session session) throws IOException { 

    } 

    @OnError 
    public void onError(Throwable error) { 
     /// put model back to pool, log erros 
    } 


    // This code never calls, 
    @OnMessage 
    public void onMessage(String message, Session session) { 

     //TODO: Add decoders/encoders to annotations 
     try {  
      ModelInputData inputData = new ModelInputDataDecoder().decode(message);   
      double input = inputData.getInput(); 
      double timespan = inputData.getTimespan(); 

      model.setInput(input); 

      ModelProcessData process = model.getOuptput(timespan); 

      try { 
       session.getBasicRemote().sendText(
         new ModelProcessDataEncoder().encode(process) 
       ); 
      } catch (IOException | EncodeException ex) { 
       Logger.getLogger(ModelWebSocket.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (DecodeException ex) { 
      Logger.getLogger(ModelWebSocket.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

} 

And the following stub for a client stub in javascript: 

<!DOCTYPE html> 
<!-- 
This is a test 
--> 
<html> 
    <head> 
     <title>Test</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     <!-- insert the main script here --> 

     <script type ="text/javascript"> 

      //copied from https://learn.javascript.ru/websockets 
      var socket = new WebSocket(
        "ws://localhost:8080/SimulatorWeb/model" 
        ); 

      socket.onopen = function() { 
       alert("Connection OK"); 
      }; 

      socket.onclose = function (event) { 
       if (event.wasClean) { 
        alert('Connection closed'); 
       } else { 
        alert('Connection interrupted'); // например, "убит" процесс сервера 
       } 
       alert('Code: ' + event.code + ' redason: ' + event.reason); 
      }; 

      socket.onmessage = function (event) { 
       alert("Data accepted " + event.data); 
      }; 

      socket.onerror = function (error) { 
       alert("Error " + error.message); 
      }; 



      function send(){ 
       socket.send("dummy"); 
      } 
     </script> 

     <button type="button" onclick="send();" >Send</button> 
    </body> 
</html> 

客户端似乎发送消息,但OnMessage从不拦截。 我有以下警告的时候,我开始Glassfish的4.1.1:

  • 警告:WELD-000411:观察法[BackedAnnotatedMethod]私人 org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType( @Observes ProcessAnnotatedType)接收所有带注释的 类型的事件。考虑使用@WithAnnotations限制事件或带有限制的泛型类型 。 WARN:WELD-000411:Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType)接收所有注释的 类型的事件。考虑使用@WithAnnotations限制事件或带有限制的泛型类型 。 WARN:WELD-000411:观察方法 [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType,BeanManager)接收的事件为所有 注解的类型。考虑限制事件使用@WithAnnotations 或带边界的泛型。

我花了很多时间进行调试,但我仍然无法找到问题......我使用Java 8 SDK和NetBeans IDE。

(在代码中,我不使用解码器/编码,因为我认为他们的问题,并从ServerEnpoint中删除)

我不知道有什么用Glassfish的问题是...我swithed到Tomcat并手动添加到WEB_INF/lib我需要的所有外部库(在我的情况下是javax.json)。 Tomcat似乎对原始代码没有任何警告,并且一切正常。