Closed polmp closed 7 years ago
Edit: I think what causes the problem.
I emit a message and then I redirect to a webpage. I suppose the program doesn't find the user that sent because it was disconnected when I do the redirect. I close the issue :)
@controller.route('/user_login', methods=['GET', 'POST'])
@socketio.on('getuser', namespace='/test')
def user_login():
sid=request.sid
global userinfo
userinfo=eval(request.form.keys()[0])['Key']
File "D:\svn\trunk\server\controller\useraction.py", line 222, in user_login sid=request.sid File "E:\newflasky\venv\lib\site-packages\werkzeug\local.py", line 343, in getattr return getattr(self._get_current_object(), name) AttributeError: 'Request' object has no attribute 'sid'
when i add socketio in views,find this error how to solve this problem? thanks
@polmp i do not do the redirect
you can't use request.sid
in an HTTP view, because there is no Socket.IO context. That only works in a Socket.IO event handler.
thank you very much! I know it I also have a problem about callbacks official website said SocketIO supports acknowledgement callbacks that confirm that a message was received by the client: this is my demo
def connect_event_callback(*args):
print '#=> client called {0}'.format(inspect.stack()[0][-4:-2])
@io.on('connect')
def connect_event_handler():
io.emit(
'connect event',
{'data': 'hello word!'},
callback=message_event_callback
)
<script type="text/javascript">
var socket = io.connect(
location.protocol+
'//'+
document.domain+
':'+
location.port
);
socket.on('connect event', function(data, func){
console.log('#=> recive server data', data.data);
func();
});
</script>
i can't understand func(),so I can't run it can you help me or give me a demo that i can run it thinks
In your example, func()
is a placeholder for your message_event_callback()
function in the server. When the client calls func()
, a similar call will be made on the server side to your function. If you pass arguments into func()
, the same arguments will be passed into your server callback function. I don't think I have any examples of callbacks that go in that direction, I have use callbacks many times, but in the reverse direction, where the client requests acknowledgements from the server, sorry.
hi man, i meet the same issue but it is from here:
i use postman to post data like: 192.168.31.34:8050/socket/send
and the post body is a form-data which key-value is data:'these are some datas'
,and my code is
@app.route("/socket/send",methods=['post'])
# @socketio.on("json") #no matter i uncomment this or not
def sendsocket():
data = request.form['data']
emit('message',jsonify("i'm data from socket",data),json=True,namespace="/socket")
return jsonify("finish!")
it gets this:
Traceback (most recent call last):
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\developsoftware\python\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\developsoftware\python\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "D:\developsoftware\python\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\Users\noel\Desktop\selfcss\webserver\message.py", line 39, in sendsocket
emit('message',jsonify("i'm data from socket",data),json=True,namespace="/socket")
File "D:\developsoftware\python\lib\site-packages\flask_socketio\__init__.py", line 693, in emit
room = flask.request.sid
File "D:\developsoftware\python\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'sid'
@NoelCarlton the emit()
function has a default of emitting to the sender of an originating event. This default only makes sense when you call the function from an event handler. You are calling it from a route, which does not have the Socket.IO context (in particular, request.sid
).
Do you know to which user(s) you want to emit an event from this route? If you want to emit to all your connected clients, add broadcast=True
. If you know the sid
of the user you want to address, then add room=sid
. You also need to specify the namespace, so add namespace='/'
or use the proper namespace name.
I'm having similar issues with a login form.
Where my app is
`from flask import Flask, render_template, flash, request, url_for, redirect
app = Flask(name)
@app.route('/') def index(): title = "Cheam Squash Ladders" return render_template("index.html", title=title)
@app.route('/register', methods=["POST", 'GET']) def squash(): user = request.squash.get("user") email = request.squash.get("email") password = request.squash.get("password") confirm_email = request.squash.get("confirm") return render_template("register.html")
@app.route('/register') def register(): return render_template("register.html")`
my forms are in
`<!DOCTYPE html>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous">
I'm getting this error when I try to add the arg include_self
Full traceback File "../app/views.py", line 230, in emit_update emit('event',{'bar':'foo'},namespace='/example_namespace',broadcast=True,include_self=False) File "../flask/lib/python2.7/site-packages/flask_socketio/init.py", line 652, in emit include_self=include_self, callback=callback) File "../flask/lib/python2.7/site-packages/flask_socketio/init.py", line 343, in emit skip_sid = flask.request.sid File "../flask/lib/python2.7/site-packages/werkzeug/local.py", line 347, in getattr return getattr(self._get_current_object(), name) AttributeError: 'Request' object has no attribute 'sid'
I'm doing anything wrong? Without include_self works.