gpt4 book ai didi

javascript - JMS 连接杀死 websocket

转载 作者:行者123 更新时间:2023-12-03 04:40:27 27 4
gpt4 key购买 nike

我有一个网站,它打开一个 websocket 并在警报中显示从服务器发送的消息。但是,一旦我创建 JMS 连接,websocket 将不再连接。该应用程序在 glassfish 4 上运行。这是代码:

wstest.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>TODO supply a title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<h:outputScript name="websocket.js" library="js" target="head"/>
</h:head>
<h:body>
TODO
</h:body>
</html>

websocket.js:

var host = 'ws://localhost:8095/path';
var websocket = new WebSocket(host);

websocket.onopen = function () {
alert('Websocket open!');
};

websocket.onmessage = function (event) {
alert(event.data);
};

WebsocketEndpoint.java:

@ServerEndpoint("/path")
public class WebsocketEndpoint {

private WebsocketChat chat = new WebsocketChat();

@OnOpen
public void onOpen(Session session) {
System.out.println("Session opened");
session.getAsyncRemote().sendText("Hello Javascript");
}
}

WebsocketChat.java:

public class WebsocketChat {

@Resource(mappedName="jms/ConnectionFactory")
private ConnectionFactory connectionFactory;

public WebsocketChat() {
//init();
}

public final void init() {
try {
Connection con = connectionFactory.createConnection();
} catch (JMSException ex) {
Logger.getLogger(WebsocketChat.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

注意,WebsocketChat的构造函数中对init方法的调用被注释掉了。这样我就可以看到两个 javascript 警报:“Websocket 打开!”当 websocket 打开时,以及来自服务器的“Hello Javascript”。在 glassfish 输出中也可以看到消息“ session 已打开”。

但是,当我取消注释 WebsocketChat 构造函数中的 init 方法时,我仍然可以看到“Websocket 打开”警报,但不会显示“Hello Javascript”警报。 “ session 已打开”消息也从 glassfish 输出中消失。 init 方法负责创建 JMS 连接。

最佳答案

我预计您的 init() 方法会抛出 NullPointerException,因为 connectionFactory 尚未初始化:

public class WebsocketChat {

@Resource(mappedName="jms/ConnectionFactory")
private ConnectionFactory connectionFactory;

public WebsocketChat() {
// You can't access the connectionFactory here because the container cannot
// inject it before the the object is created.
// The object will not be created until this constructor has completed
// executing.
}

/* The container will call this after the object is created */
@PostConstruct
public final void init() {
try {
try (Connection con = connectionFactory.createConnection()) {
// do something with the connection...
}
} catch (JMSException ex) {
Logger.getLogger(WebsocketChat.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

更新了 WebsocketEndpoint:

@ServerEndpoint("/path")
public class WebsocketEndpoint {

/*
* You need to inject this. The container will create and initialise it for you.
*/
@Inject
private WebsocketChat chat;

@OnOpen
public void onOpen(Session session) {
System.out.println("Session opened");
session.getAsyncRemote().sendText("Hello Javascript");
}
}

FWIW, the above code works for me in WildFly 10.1 with one change:

    @Resource(lookup="java:/ConnectionFactory")  
private ConnectionFactory connectionFactory;

这恰好是 WildFly 中的默认 ConnectionFactory。

此外,您的 JavaScript 可能需要在其 Web 套接字 URL 中包含 webapp 上下文:

 var host = 'ws://localhost:8095/<webapp-context>/path';

The code above also works in GlassFish 4.1.1 with the following change:

    @Resource(name="java:comp/DefaultJMSConnectionFactory")  
private ConnectionFactory connectionFactory;

关于javascript - JMS 连接杀死 websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43113723/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com