gpt4 book ai didi

java - 当我尝试发送要在 Wicket 口网页上显示的消息时,无法打开 IWebSocketConnection

转载 作者:行者123 更新时间:2023-12-01 19:32:08 24 4
gpt4 key购买 nike

我正在尝试异步向网页发送一些消息,但我无法正确执行,缺少某些内容。我正在尝试进行一些计算,我想看看我所处的步骤。无论如何,在我的 calcul() 方法中,wsConnection 始终处于关闭状态。我尝试使用调试器,发现它在我的 calcul() 方法之前和之后进入 onConnect,并且 wsConnection 不为 null,但 connection...close = true。所以我有:

CalculPanel.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<body>
<wicket:extend>
<h1>Calcul</h1>
<form wicket:id="form">
<input type="submit" value="Calcul"/>
</form>
<div wicket:id="container">
<ul wicket:id="messages">
<li wicket:id="message"></li>
</ul>
</div>
<div style="color:red" wicket:id="feedback"></div>
</wicket:extend>
</body>
</html>

CalculPanel.java:

public class CalculPanel extends BasePage {

Logger LOGGER = LoggerFactory.getLogger(CalculPanel.class);

String message;

private String applicationName;

IWebSocketConnection wsConnection;

private WebMarkupContainer container;

private FeedbackPanel feedbackPanel;

private List<String> messages = Collections
.synchronizedList(new LinkedList<String>());

private IKey pageId;

private String sessionId;

private Model<String> updateModel;

public CalculPanel () {
Form<?> form = new Form("form") {
@Override
protected void onSubmit() {
super.onSubmit();
calcul();
}
};
this.feedbackPanel = new FeedbackPanel("feedback");
this.feedbackPanel.setOutputMarkupId(true);
add(this.feedbackPanel);

add(form);

setDefaultModel(new CompoundPropertyModel<CalculPanel >(this));
container = new WebMarkupContainer("container");
add(container.setOutputMarkupId(true));
container.add(new ListView<String>("messages",
new PropertyModel<List<String>>(this, "messages")) {
private static final long serialVersionUID = 1L;

@Override
protected void populateItem(ListItem<String> item) {
item.setDefaultModel(CompoundPropertyModel.of(item
.getDefaultModel()));
item.add(new Label("message", item.getModelObject()));
}
});

add(new WebSocketBehavior() {
private static final long serialVersionUID = 1L;

@Override
public void onException(Component component,
RuntimeException exception) {
LOGGER.warn("Got exception", exception);
}

@Override
protected void onConnect(ConnectedMessage message) {
super.onConnect(message);

CalculPanel.this.applicationName = message.getApplication().getName();
CalculPanel.this.sessionId = message.getSessionId();
CalculPanel.this.pageId = message.getKey();

Application application = Application.get(CalculPanel.this.applicationName);
WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
IWebSocketConnectionRegistry webSocketConnectionRegistry = webSocketSettings.getConnectionRegistry();
CalculPanel.this.wsConnection = webSocketConnectionRegistry.getConnection(application,
CalculPanel.this.sessionId, CalculPanel.this.pageId);

WebSocketService.getInstance().addClient(message);
LOGGER.info("Client connected");
}

@Override
protected void onMessage(WebSocketRequestHandler handler, TextMessage message) {
String msg = message.getText();
// use the message sent by the client

handler.push("A message pushed by the server");
}

@Override
protected void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) {
super.onPush(handler, message);

if (message instanceof FeedItem) {
FeedItem msg = (FeedItem) message;
updateModel.setObject(msg.toString());
}
handler.add(feedbackPanel);
}
});
}

public void calcul() {
.......
for (int agency = START_AGENCY; TOTAL_NUMBER_OF_AGENTIES >= agency; agency++) {
.......
this.message = agency + "....";
//info(this.message);
//wsConnection is always closed...
if (null != wsConnection && wsConnection.isOpen()) {
wsConnection.sendMessage(this.message);
}
.......
}

public void handleMessage(WebSocketRequestHandler handler, FeedItem message) {
messages.add(message.toString());
while (5 < messages.size()) {
messages.remove(0);
}
LOGGER.info("Push message {}. Number of messages {}.", message, messages.size());
LOGGER.info("FeedPage: {}", handler);
container.modelChanged();
handler.add(container);
}

@Override
public void onEvent(IEvent<?> event) {
if (event.getPayload() instanceof WebSocketPushPayload) {
WebSocketPushPayload wsEvent = (WebSocketPushPayload) event
.getPayload();
handleMessage(wsEvent.getHandler(), (FeedItem) wsEvent.getMessage());
}
}

我正在使用<wicket.version>7.10.0</wicket.version>

<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-native-websocket-javax</artifactId>
<version>${wicket.version}</version>
</dependency>

谢谢!

最佳答案

您正在每个calculate()上创建一个新的连接注册表,但您应该使用您的应用程序:

Application application = Application.get(applicationName);
WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
IWebSocketConnectionRegistry webSocketConnectionRegistry = webSocketSettings.getConnectionRegistry();
IWebSocketConnection wsConnection = webSocketConnectionRegistry.getConnection(application, sessionId, pageId);

请查看有关如何正确执行此操作的示例:

https://github.com/apache/wicket/tree/master/wicket-examples/src/main/java/org/apache/wicket/examples/websocket

关于java - 当我尝试发送要在 Wicket 口网页上显示的消息时,无法打开 IWebSocketConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59370083/

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