GithubHelp home page GithubHelp logo

sheng930920 / androidwebsocketdemo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wu19910708/androidwebsocketdemo

0.0 2.0 0.0 145 KB

android使用websocket进行长链接的一个简单的demo,可以用来收发消息或别的操作,里面用到了autobahn的jar包

Java 100.00%

androidwebsocketdemo's Introduction

androidWebsocketDemo

android使用websocket进行长链接的一个简单的demo,可以用来收发消息或别的操作,里面用到了autobahn的jar包

##基本操作都在WebSocketService 这个类中,websocketHost要填写自己服务器的,ws开头的url,代码中我有说

webSocketConnection.connect(websocketHost,new WebSocketHandler(){


                //websocket启动时候的回调
                @Override
                public void onOpen() {
                    Log.d(TAG,"open");
                
                }


                //websocket接收到消息后的回调
                @Override
                public void onTextMessage(String payload) {
                    Log.d(TAG, "payload = " + payload);
                }

                //websocket关闭时候的回调
                @Override
                public void onClose(int code, String reason) {
                    Log.d(TAG, "code = " + code + " reason = " + reason);
                    switch (code) {
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3://手动断开连接
                            
                            break;
                        case 4:
                            break;
                        
                        case 5://网络断开连接
                        
                            break;
                    }
                }
            } , options);

##发送消息时的注意 我们公司里是用json进行数据传输的,里面有个cmd字段,是用来区别消息类型,我们传给后台的时候也要用json。具体还是要看手机端和后台商量决定,这里就不写出来了。 ##心跳包 如果想保持websocket连接的稳定性,建议加上心跳包。可以每隔一段时间就发个简单的字符串给后台,让后台知道你在线,可以用timetask配合CountDownTimer来写

androidwebsocketdemo's People

Contributors

wu19910708 avatar

Watchers

James Cloos avatar Andy avatar

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.