GithubHelp home page GithubHelp logo

androidwebsocket's Introduction

AndroidWebSocket

Help to use webSocket in Android
based on "org.java-websocket:Java-WebSocket:1.3.4"

dependencies

Maven

<dependency>
  <groupId>xyz.leohan</groupId>
  <artifactId>AndroidWebSocket</artifactId>
  <version>1.1.3-beta</version>
  <type>pom</type>
</dependency>

Gradle

compile 'xyz.leohan:AndroidWebSocket:1.1.3-beta'

How to Use

  1. write your own BroadcastReceiver extends xyz.leohan.websocketlib.WebSocketReceiver like this:
public class MyReceiver extends WebSocketReceiver {
    @Override
    public void onMessage(String msg) {
        //Here is message from webSocket
        //you can deal it with eventBus、RxBus、Notification and so on
        Log.i("webSocket", msg);
    }
}
  1. write these in your AndroidManifest.xml:
 <service android:name="xyz.leohan.websocketlib.WebSocketService" />
 <receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="xyz.leohan.Broadcast.WebSocket" />
     </intent-filter>
 </receiver>
  1. call init() :
 //do this in your BaseApplication or MainActivity
 //Make sure you called this only once
 //WebSocketAndroidClient.init(this,"ws://192.168.1.108:9898");
 WebSocketAndroidClient.init(this,"ws://192.168.1.108:9898",3600);
 WebSocketAndroidClient.getInstance().connect(new WebSocketAndroidClient.onWebSocketOpenListener() {
              @Override
              public void onOpen(ServerHandshake handshakedata) {
                 //TODO connected
              }
          } );
  1. call disconnect:

it will auto reconnect by error or server offline,but when you call disconnect,you need call connect again

   WebSocketAndroidClient.getInstance().disConnect();
  1. send message:
   WebSocketAndroidClient.getInstance().sendMsg(editText.getText().toString());

History

  • v1.1.4-beta 1.update gradle to 4.1 2.update buildTool to 26.0.2 3.add isConnected function.
  • v1.1.3-beta
    1. add init function. add timeout time param.
  • v1.1.2-beta
    1. add connenct listener
    2. fix disconnect and reconnect bug
    3. throw NotYetConnectException when send msg

Contact me

if you have any trouble in use this lib ,you can send me an e-mail :[email protected]

androidwebsocket's People

Contributors

leohan1992 avatar

Stargazers

 avatar Stark.Tony avatar  avatar

Watchers

James Cloos avatar  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.