socket.io-java-client cookies / custom headers [英] socket.io-java-client cookies / custom headers

查看:260
本文介绍了socket.io-java-client cookies / custom headers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为网站做了一个移动应用程序,他们使用Socket.io和Websocket通信。

I'm doing a mobile app for a website and they use Socket.io and Websocket to comunicate.

我使用socket.io-java-client在Android上连接到Socket.io服务器,问题是,我不知道如何设置自定义标头/ cookie与它。

I'm using socket.io-java-client on Android to connect to the Socket.io server, the problem is, i don't know how to set custom headers / cookies with it.

这里是如何代码看起来像:

here is how the code looks like:

    public void runIO(){
        try {
            SocketIO socket = new SocketIO("http://192.168.1.60:1337");
            socket.connect(new IOCallback() {
                @Override
                public void onMessage(JSONObject json, IOAcknowledge ack) {
                    try {
                        System.out.println("Server said:" + json.toString(2));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onMessage(String data, IOAcknowledge ack) {
                    System.out.println("Server said: " + data);
                }

                @Override
                public void onError(SocketIOException socketIOException) {
                    System.out.println("an Error occured");
                    socketIOException.printStackTrace();
                }

                @Override
                public void onDisconnect() {
                    System.out.println("Connection terminated.");
                }

                @Override
                public void onConnect() {
                    System.out.println("Connection established");
                }

                @Override
                public void on(String event, IOAcknowledge ack, Object... args) {
                    System.out.println("Server triggered event '" + event + "'");
                }
            });

            // This line is cached until the connection is establisched.
            socket.send("Hello Server!");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


推荐答案

事实上有一些简单的函数可以在连接之前用来设置头文件:

In fact there are simple functions that can be used to set headers before connection:

addHeader(String key,String value) setHeaders(Properties headers)

您也可以使用 SocketIO ,Properties headers)(而不是 SocketIO(final String url),我正在使用):

You can also use SocketIO(final String url, Properties headers) (instead of SocketIO(final String url) that i was using):

// Haeader Properties initiation
    private Properties headers = new Properties();

,然后例如设置cookie:

and then for exemple to set cookies:

headers.setProperty("Cookie","key=data;key2=data2");

最后连接替换

this.socket = new SocketIO(val);

this.socket = new SocketIO(val,this.headers);

这篇关于socket.io-java-client cookies / custom headers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆