Skip to content

Instantly share code, notes, and snippets.

@ikatson
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikatson/264ffc6e496854b58015 to your computer and use it in GitHub Desktop.
Save ikatson/264ffc6e496854b58015 to your computer and use it in GitHub Desktop.
uwsgi Websocket error: ASYNC call without async mode !!!, IOError: unable to fd to the event queue
<!doctype html>
<meta charset=utf-8>
<title>UWSGI WS test</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
angular.module('app', []).controller('Ctrl', function ($scope) {
this.logMessages = [];
var self = this;
var log = function (msg) {
self.logMessages.push({date: new Date(), msg: msg});
$scope.$digest();
}
var ws = new WebSocket('ws://localhost:5000/ws/');
ws.onopen = function () {
log('WS open');
ws.send('message to server');
}
ws.onmessage = function (msg) {
log('WS message received: ' + msg);
};
ws.onclose = function () {
log('WS closed');
};
ws.onerror = function (err) {
log('WS error: ' + err);
}
});
</script>
<body ng-app="app" ng-controller="Ctrl as ctrl" class="container">
<h2>uWSGI websocket test</h2>
<div ng-repeat="msg in ctrl.logMessages.slice().reverse()"><pre>[{{msg.date|date:'mediumTime'}}]: {{msg.msg}}</pre><div>
</body>
gevent>=1.0
uwsgi
import uwsgi
def application(env, start_response):
if env['PATH_INFO'] != '/ws/':
start_response('404 Not Found', [])
return
uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', ''))
websocket_fd = uwsgi.connection_fd()
def process_msg(msg):
uwsgi.websocket_send('got %s' % msg)
while True:
uwsgi.wait_fd_read(websocket_fd, 3)
uwsgi.suspend()
fd = uwsgi.ready_fd()
if fd > -1:
if fd == websocket_fd:
msg = uwsgi.websocket_recv_nb()
if msg:
process_msg(msg)
else:
# on timeout call websocket_recv_nb again to manage ping/pong
msg = uwsgi.websocket_recv_nb()
if msg:
process_msg(msg)
[uwsgi]
gevent=1000
route = ^/$ static:%dindex.html
wsgi=server:application
pythonpath=%d
pythonhome=$(VIRTUAL_ENV)
http=:5000
http-websockets=
master=
py-autoreload=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment