Skip to content

Instantly share code, notes, and snippets.

@toxinu
Created September 24, 2020 01:41
Show Gist options
  • Select an option

  • Save toxinu/48203b61ca9772e53a445aa272604457 to your computer and use it in GitHub Desktop.

Select an option

Save toxinu/48203b61ca9772e53a445aa272604457 to your computer and use it in GitHub Desktop.
blog/websocket-with-flask-and-gevent
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<p>Websocket with Flask, Gevent and Gevent-websocket</p>
<p id="log"></p>
<button id="send" type="button">Send!</button>
<body>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function(){
if ("WebSocket" in window) {
ws = new WebSocket("ws://" + document.domain + ":5000/websocket");
ws.onmessage = function (msg) {
var message = JSON.parse(msg.data);
$("p#log").html(message.output);
};
};
// Bind send button to websocket
$("button#send").live("click", function() {
ws.send(JSON.stringify({'output': 'Sent from my browser!'}));
});
// Cleanly close websocket when unload window
window.onbeforeunload = function() {
ws.onclose = function () {}; // disable onclose handler first
ws.close()
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment