Skip to content
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

Open
jamesragray opened this issue Sep 17, 2024 · 1 comment
Open

WebSocket close doesn't necessarily close #43

jamesragray opened this issue Sep 17, 2024 · 1 comment

Comments

@jamesragray
Copy link

jamesragray commented Sep 17, 2024

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.

@jamesragray
Copy link
Author

jamesragray commented Sep 17, 2024

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

Start: totalWebSocketCount=1 webSocketCount=1
End: totalWebSocketCount=1 webSocketCount=0
Start: totalWebSocketCount=2 webSocketCount=1
End: totalWebSocketCount=2 webSocketCount=0
Start: totalWebSocketCount=3 webSocketCount=1
End: totalWebSocketCount=3 webSocketCount=0
Start: totalWebSocketCount=4 webSocketCount=1
End: totalWebSocketCount=4 webSocketCount=0
Start: totalWebSocketCount=5 webSocketCount=1
End: totalWebSocketCount=5 webSocketCount=0
Start: totalWebSocketCount=6 webSocketCount=1
End: totalWebSocketCount=6 webSocketCount=0
Start: totalWebSocketCount=7 webSocketCount=1
End: totalWebSocketCount=7 webSocketCount=0
Start: totalWebSocketCount=8 webSocketCount=1
End: totalWebSocketCount=8 webSocketCount=0
Start: totalWebSocketCount=9 webSocketCount=1
End: totalWebSocketCount=9 webSocketCount=0
Start: totalWebSocketCount=10 webSocketCount=1
End: totalWebSocketCount=10 webSocketCount=0
Start: totalWebSocketCount=11 webSocketCount=1
End: totalWebSocketCount=11 webSocketCount=0
Start: totalWebSocketCount=12 webSocketCount=1
End: totalWebSocketCount=12 webSocketCount=0
Start: totalWebSocketCount=13 webSocketCount=1
Start: totalWebSocketCount=14 webSocketCount=2
Start: totalWebSocketCount=15 webSocketCount=3
Start: totalWebSocketCount=16 webSocketCount=4
Start: totalWebSocketCount=17 webSocketCount=5
Start: totalWebSocketCount=18 webSocketCount=6
Start: totalWebSocketCount=19 webSocketCount=7
Start: totalWebSocketCount=20 webSocketCount=8
Start: totalWebSocketCount=21 webSocketCount=9
Start: totalWebSocketCount=22 webSocketCount=10
Start: totalWebSocketCount=23 webSocketCount=11
Start: totalWebSocketCount=24 webSocketCount=12
Start: totalWebSocketCount=25 webSocketCount=13
Start: totalWebSocketCount=26 webSocketCount=14
Start: totalWebSocketCount=27 webSocketCount=15
Start: totalWebSocketCount=28 webSocketCount=16
Start: totalWebSocketCount=29 webSocketCount=17
Start: totalWebSocketCount=30 webSocketCount=18
Start: totalWebSocketCount=31 webSocketCount=19
Start: totalWebSocketCount=32 webSocketCount=20
Start: totalWebSocketCount=33 webSocketCount=21
Start: totalWebSocketCount=34 webSocketCount=22

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant