Code Monkey home page Code Monkey logo

Comments (10)

drulia avatar drulia commented on May 18, 2024

I'm having similar problem.

I have already built websockets server which was built for JS primarily and I'm reusing client session cookies from simple HTTP request when client loaded the page. When client socket connects to the server I capture that cookie and use it to identify the user. This makes handling users super simple, as there is libraries for Node.js that can essentially handle session cookies for you.

Unfortunately once I connect to server from android, I have no cookies set, even if I try to make HTTP connection before I connect to server.

P.S.
I'm just starting to learn Android dev and the stuff above that I wrote might make little to no sense at all, in which case I would gladly clarify my use case.

Thanks for your job on this socket.io client!

from socket.io-client-java.

nkzawa avatar nkzawa commented on May 18, 2024

On just released v0.1.3, you can access to http headers like the following:

    socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Transport transport = (Transport)args[0];
                transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
                    @Override
                    public void call(Object... args) {
                        @SuppressWarnings("unchecked")
                        Map<String, String> headers = (Map<String, String>)args[0];
                        // set header
                        headers.put("X-SocketIO", "hi");
                    }
                }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
                    @Override
                    public void call(Object... args) {
                        @SuppressWarnings("unchecked")
                        Map<String, String> headers = (Map<String, String>)args[0];
                       // get header
                        String value = headers.get("X-SocketIO");
                    }
                });
            }
        });

from socket.io-client-java.

danpe avatar danpe commented on May 18, 2024

@nkzawa Will this add "X-SocketIO": "hi" to each socket.io request that I will send?

I'm using passport.js with https://github.com/jfromaniello/passport.socketio.

from socket.io-client-java.

nkzawa avatar nkzawa commented on May 18, 2024

@danpe yes.

from socket.io-client-java.

danpe avatar danpe commented on May 18, 2024

Can I give the socket a java.net.CookieManager / java.net.CookieStore to use ?
Or I should implement my own function to read the and write to the cookie store using the example you gave ?

If so, do you want me to add this feature as a pull request ?

from socket.io-client-java.

nkzawa avatar nkzawa commented on May 18, 2024

@danpe Unfortunately, you need to implement your own.

I don't have any thought to add such feature to the library for now, since I'd like to keep it simple as much as possible. Though it depends on how many requests we get.

Thank you for your suggestion anyway!

from socket.io-client-java.

TangMonk avatar TangMonk commented on May 18, 2024

use url param to send cookie instead of send headers directly will better :D

from socket.io-client-java.

akshaylive avatar akshaylive commented on May 18, 2024

Need cookie store! :(

from socket.io-client-java.

hayksaakian avatar hayksaakian commented on May 18, 2024

if you use this, you will probably be sad, as I was when I found out that the JavaScript socket.io client will never support sending headers.

see:

socketio/socket.io-client#648 (comment)

In my case, i need to rewrite my API to handle authentication in a separate step -- after connecting.

from socket.io-client-java.

eschwitters avatar eschwitters commented on May 18, 2024

@nkzawa Your example seems to work except that the Transport headers are actually a Map, List> in the PollingXHR class.

Not sure why they need to be a Map of Lists not just a Map of Strings?

from socket.io-client-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.