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

why the client can not receive the emit data during connecting? #989

Closed
chendongxtu opened this issue May 29, 2019 · 16 comments
Closed

why the client can not receive the emit data during connecting? #989

chendongxtu opened this issue May 29, 2019 · 16 comments

Comments

@chendongxtu
Copy link

chendongxtu commented May 29, 2019

@socketio.on("connect", namespace="/test")
def test_connect():
emit("my_response", {"data": "Connected", "count":0})

in the test demo, this works well, and i can see the data {"data": "Connected", "count":0} in the webbrowser.

but if i use it like following:

@socketio.on("connect", namespace="/test")
def test_connect():
emit("my_response", {"data": "Connected", "count":0})
## add here ##
return False

then in the browser, it will try to connect, but the strange place is that i can not see the data {"data": "Connected", "count":0} in the webbrowser console, i have used ipdb to trace, and it really has sent the data. so i am confused why client can not recieve the response data?
can you help me? thanks

this is the pic without return False
企业微信截图_15591241442631

this is the pic with return False:
企业微信截图_15591240067634

@miguelgrinberg
Copy link
Owner

What do you return False? That has special significance, it's saying that you are rejecting the connection.

@chendongxtu
Copy link
Author

because i wanna to tell somesthing to the client why i refused the connection before i return False

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented May 29, 2019

Okay, then this is wrong. You have two options to do that:

  1. Add the always_connect=True setting in the SocketIO constructor. This will fully accept the connection before the connect handler is invoked, so you can emit freely. If you return False, then a disconnect() will be issued to close the connection.

  2. Instead of returning False, raise a ConnectionRefusedError exception, which takes an argument where you can pass an error message back to the client:

from socketio import ConnectionRefusedError

@socketio.on('connect')
def connect():
    raise ConnectionRefusedError('authentication failed')

@miguelgrinberg
Copy link
Owner

I'm going to leave this issue open as a reminder that I need to properly export the ConnectionRefusedError exception from this package, and also to document it here (it is currently only documented in the python-socketio package).

@chendongxtu
Copy link
Author

okay, thanks very much

@chendongxtu
Copy link
Author

chendongxtu commented May 30, 2019

emm, in fact, the ConnectionRefusedError class is not existed in the socketio/init.py,
and the class SocketIo in flask_socket does not have the param always_connect in ins' constructor.
is my flask_socketio version too low or they are new features waiting to be added?

@miguelgrinberg
Copy link
Owner

@chendongxtu you need to upgrade flask-socketio and all of its dependencies to the latest. As I said above, this package does not have the exception, it is in the python-socketio package, which is a dependency.

@sirlada
Copy link

sirlada commented Jun 18, 2019

hi @miguelgrinberg, sorry to interrupt:
updated Flask-SocketIO to 4.1 and got this error:
ImportError: cannot import name 'ConnectionRefusedError'

resolved error through updating python-socketio to latest version.
so i guess the dependencies for python-socketio have to be updated in setup.py

install_requires=[
        'Flask>=0.9',
        'python-socketio>=2.1.0'
    ],

@YuqiaoS
Copy link

YuqiaoS commented Nov 4, 2019

@miguelgrinberg When I raise the error, on the client side I get undefined when I console.log the error argument from my 'connect_error' handler. The event seems to be invoked but how do I get the message?

@miguelgrinberg
Copy link
Owner

@YuqiaoS can I see your code?

@YuqiaoS
Copy link

YuqiaoS commented Nov 4, 2019

@miguelgrinberg So the frontend is not printing the error seems to be an error from the https://github.com/MetinSeylan/Vue-Socket.io lib I'm using, as I do get an xhr error when not using that. (yea there was an update to fix this..gotta update the package)

So I get Error: xhr poll error. But the xhr itself for the io connect returns 401 Unauthorized, with the response of "Unauthorized". It's supposed to return the message passed into the exception right? So is this because maybe some thing is catching exception and handling it? I'm using flask_login as well.

@miguelgrinberg
Copy link
Owner

You are not showing code nor logs, so I cannot say. Get logging enabled in the server and you will see what the server is sending back.

@YuqiaoS
Copy link

YuqiaoS commented Nov 5, 2019

Here's the log. The message seems to be sent.

INFO:engineio.server:1b2ab86e02da4cd2a7fef1a3992750b1: Sending packet MESSAGE data 4"i deny u"
WARNING:engineio.server:Application rejected connection

@miguelgrinberg
Copy link
Owner

The server is sending a normal message with the string "i deny u". This isn't what I expect would be sent when you raise a connection refused exception. Are you sure you are raising an exception? I just don't see how that could translate to a normal message.

@YuqiaoS
Copy link

YuqiaoS commented Nov 5, 2019

from flask_socketio import ConnectionRefusedError

@socketio.on('connect')
def connect():
if not current_user.is_authenticated:
        raise ConnectionRefusedError('i deny u')

@Grkmus
Copy link

Grkmus commented Feb 27, 2022

This maybe should be in a seperate issue but ConnectionRefusedError is not passing any arguments to the client in the error packet like it is mentioned in the documentation. It is only sending one string nothing else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants