java - Websocket 无法建立连接

标签 java struts2 websocket

我有以下代码来向浏览器发送推送通知,一旦我运行应用程序,EJB 就会启动,并在控制台中显示超时、发送等内容。

但是 Firefox 显示无法与位于 ws://localhost:8080/Notifications 的服务器建立连接。

JavaScript函数

<script type="text/javascript">
             var wsocket;
      function connect() {
          wsocket = new WebSocket("ws://localhost:8080/Notifications");
          alert("got connected");
          wsocket.onmessage = onMessage;
      }
      function onMessage(evt) {
          alert(evt);
          var arraypv = evt;
                    alert("array" + arraypv);
          document.getElementById("foo").innerHTML = arraypv[0];
      }
      alert("window" + connect);
      window.addEventListener("load", connect, false);
        </script>

Struts 映射

    <action name="Notifications" class="com.example.controller.Notifications">

    </action>

通知类

@ServerEndpoint("/Notifications")
public class Notifications {

   static Queue<Session> queue = new ConcurrentLinkedQueue();

   public static void send() {
       System.err.println("send");
       String msg = "Here is the message";
      try {
         /* Send updates to all open WebSocket sessions */
         for (Session session : queue) {
            session.getBasicRemote().sendText(msg);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
    }

    @OnOpen
public void openConnection(Session session) {
    System.err.println("in open connection");
    queue.add(session);
}

@OnClose
public void closedConnection(Session session) {
    System.err.println("in closed connection");
    queue.remove(session);
}

@OnError
public void error(Session session, Throwable t) {
    System.err.println("in error");
    queue.remove(session);
}

PriceVolumeBean 类

@Startup
@Singleton
public class PriceVolumeBean {
@Resource TimerService tservice;
private Random random;

    @PostConstruct
    public void init() {
        System.err.println("in init");
        random = new Random();
        tservice.createIntervalTimer(1000, 1000, new TimerConfig());
    }

    @Timeout
    public void timeout() {
        System.err.println("in timeout");
        Notifications.send();
    }
}

pom.xml

   <dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.0</version>
</dependency>

最佳答案

部署时

<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.0</version>
    <scope>provided</scope>   
</dependency>

提供此内容。

关于java - Websocket 无法建立连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369238/

相关文章:

java - 如何阻止用户在此 read() 方法中输入字符串?

java - 在 Jython 中导入 Paramiko

java - 从 hibernate 查询中获取 java.util.map 列表形式的结果

java - 标签 'select' ,字段 'list' ,名称 'uuid' : The requested list key 'deptList' could not be resolved as a collection/array/map/enumeration/iterator type

java - 按前缀从属性文件读取属性的最有效解决方案是什么?

java - quartz 调度: Using DailyCalendar

java - includeProperties 不起作用?

java - 处理来自 WebSocket 消息的 MethodArgumentNotValidException

amazon-web-services - Apache Tomcat 在使用 websocket 时返回 404

https - 网页如何向本地网络发送消息