我正在尝试将一些消息异步发送到网页,但是我做不到,有些东西丢失了。我正在尝试进行一些计算,我想看看自己所在的位置。无论如何,在我的calcul()方法中,wsConnection始终处于关闭状态。我尝试使用调试器,然后看到它在我的calcul()方法之前和之后进入onConnect,并且wsConnection不为null,但connection ... closed = 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);
请查看有关如何正确执行此操作的示例: