-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebSocket close doesn't necessarily close #43
Comments
Sample of code that demonstates the problem: import std.stdio;
import vibe.vibe;
void main() {
auto settings = new HTTPServerSettings;
settings.port = 9020;
settings.bindAddresses = ["0.0.0.0"];
auto router = new URLRouter;
router.get("/ws", handleWebSockets(&handleConn));
router.get("/", &index);
auto listener = listenHTTP(settings, router);
scope (exit)
{
listener.stopListening();
}
runApplication();
}
long totalWebSocketCount, webSocketCount;
void index(HTTPServerRequest req, HTTPServerResponse res) {
res.writeBody(
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Webpage with JavaScript</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to My Simple Webpage</h1>
</div>
<script>
var socket;
function connect() {
socket = new WebSocket(getBaseURL() + '/ws');
socket.onopen = function() {
console.log("Websocket opened");
let msg = { id : "na", kind : "setpage", value : location.pathname };
socket.send(JSON.stringify(msg));
};
socket.onmessage = function(e) {
console.log(e);
};
socket.onclose = function(e) {
console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
setTimeout(function() {
connect();
}, 1000);
};
socket.onerror = function(err) {
console.error('Socket encountered error: ', err.message, 'Closing socket');
socket.close();
};
}
connect();
function getBaseURL() {
return (window.location.protocol=="https:"?"wss://":"ws://") + window.location.host;
}
</script>
</body>
</html>
`,"text/html");
}
void handleConn(scope WebSocket sock) {
webSocketCount++;
totalWebSocketCount++;
writefln("Start: totalWebSocketCount=%s webSocketCount=%s",totalWebSocketCount,webSocketCount);
sock.close;
webSocketCount--;
writefln("End: totalWebSocketCount=%s webSocketCount=%s",totalWebSocketCount,webSocketCount);
} Here is the output
For the first part the page http://0.0.0.0:9020 is opened using google chrome and the second part (when End's disappear) is when the page is opened via firefox. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calling close on a websocket doesn't necessarily close the websocket.
When connecting to the server from firefox, close gets stuck here which I traced to getting stuck in the while loop here.
Can add test code if that would help.
The text was updated successfully, but these errors were encountered: