Socket.io Android 配置Session

业务需求,使用socket.io做软件打开时服务器推送。
gradle加入socket之类略过。
主要难点一下,给Socket.io 添加本地持久化session。博主使用的是SharedPrefsCookiePersistor,只需重写Manager.EVENT_TRANSPORT事件。
代码如下。

socket=IO.socket(RequestFactory.end_point);
            final SharedPrefsCookiePersistor persistor=new SharedPrefsCookiePersistor(getBaseContext());
            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> headers = (Map>)args[0];
                            // modify request headers
                            List cookies=new ArrayList();
                            for(Cookie cookie: persistor.loadAll()){
                                cookies.add(cookie.toString());
                            }
                            headers.put("Cookie", cookies);
                        }
                    });

                    transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
                        @Override
                        public void call(Object... args) {
                            @SuppressWarnings("unchecked")
                            Map> headers = (Map>)args[0];
                            // access response headers
                            if(headers==null) return;
                                List cookieStrings = headers.get("Set-Cookie");
                            List cookies = null;
                            if (cookieStrings==null) return;
                            for (int i = 0, size = cookieStrings.size(); i < size; i++) {
                                Cookie cookie = Cookie.parse(HttpUrl.parse(RequestFactory.end_point), cookieStrings.get(i));
                                if (cookie == null) continue;
                                if (cookies == null) cookies = new ArrayList<>();
                                cookies.add(cookie);
                            }
                            persistor.saveAll(cookies);
                        }
                    });
                }
            });

读取传输的数据

application.getSocket().on("message", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                final Dialog dialog=com.alibaba.fastjson.JSON.parseObject(args[0].toString(),Dialog.class);
                DialogActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        messageMultiItemTypeAdapter.getDatas().addAll(0,dialog.getMessages());
                        messageMultiItemTypeAdapter.notifyDataSetChanged();
                        recyclerView.smoothScrollToPosition(messageList.size()-1==-1?0:messageList.size()-1);

                    }
                });
            }
        });

你可能感兴趣的:(Socket.io Android 配置Session)