Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b660bb

Browse files
committed
try to make stopping of the WebSockets more robust
1 parent 73973ad commit 9b660bb

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

src/main/java/com/gargoylesoftware/htmlunit/javascript/host/WebSocket.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,28 @@ public void close() throws IOException {
380380
public void close(final Object code, final Object reason) {
381381
if (readyState_ != CLOSED) {
382382
if (incomingSession_ != null) {
383-
incomingSession_.close();
383+
try {
384+
incomingSession_.close();
385+
}
386+
catch (final Throwable e) {
387+
LOG.error("WS close error - incomingSession_.close() failed", e);
388+
}
384389
}
385390
if (outgoingSession_ != null) {
386-
outgoingSession_.close();
391+
try {
392+
outgoingSession_.close();
393+
}
394+
catch (final Throwable e) {
395+
LOG.error("WS close error - outgoingSession_.close() failed", e);
396+
}
387397
}
388398
}
389399

390400
try {
391401
if (client_ != null) {
392402
try {
393403
client_.stop();
404+
client_.destroy();
394405
}
395406
catch (final UpgradeException e) {
396407
LOG.error("WS stop error (connection was not established so far)", e);

src/test/java/com/gargoylesoftware/htmlunit/javascript/host/WebSocketTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,27 @@ public void prototypeUrl() throws Exception {
697697
+ "</body></html>";
698698
loadPageWithAlerts2(html);
699699
}
700+
701+
/**
702+
* @throws Exception if the test fails
703+
*/
704+
@Test
705+
public void socketsGetClosedOnPageReplace() throws Exception {
706+
startWebServer("src/test/resources/com/gargoylesoftware/htmlunit/javascript/host",
707+
null, null, new ChatWebSocketHandler());
708+
try {
709+
final WebDriver driver = getWebDriver();
710+
driver.get(URL_FIRST + "WebSocketTest_chat.html");
711+
712+
driver.findElement(By.id("username")).sendKeys("Browser");
713+
driver.findElement(By.id("joinB")).click();
714+
715+
assertVisible("joined", driver);
716+
717+
driver.get(URL_FIRST + "plain.html");
718+
}
719+
finally {
720+
stopWebServers();
721+
}
722+
}
700723
}

src/test/resources/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocumentTest_cookieInLocalFile.html

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
foo
6+
</body>
7+
</html>

0 commit comments

Comments
 (0)