项目作者: geeckmc

项目描述 :
Android webSocket client for Ratchet Server
高级语言: Java
项目地址: git://github.com/geeckmc/WebSocketAndroidClient.git
创建时间: 2017-09-15T04:31:20Z
项目社区:https://github.com/geeckmc/WebSocketAndroidClient

开源协议:

下载


WebSocketAndroidClient

Android webSocket client for Ratchet Server

Credit : This android library use Autobahn-java

Installation

1 - Add it in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

2 - Add the dependency

  1. dependencies {
  2. compile 'com.github.geeckmc:WebSocketAndroidClient:0.0.7'
  3. }

3 - Add packaging options

  1. android
  2. {
  3. ...
  4. packagingOptions {
  5. exclude 'META-INF/LICENSE'
  6. exclude 'META-INF/ASL2.0'
  7. }
  8. }

Usage

1 - Create an Web Socket Instance and start connection

  1. Ws ws = new Ws.Builder().from( "ws://server_address");
  2. ws.connect();

2 - Subscribe to channel

Basically get raw data

  1. ws.on("path/to/channel", new Ws.WsListner() {
  2. @Override
  3. public void onEvent(String eventUri, Object data) {
  4. if(data != null) //your logic here
  5. }
  6. });

OR

Get parsed object from json response,
for example to get User from channel do something like this

  1. ws.on("path/to/channel", User.class, new Ws.WsListner<User>() {
  2. @Override
  3. public void onEvent(String eventUri, User user) {
  4. if(user != null) Log.e(TAG,user.name);
  5. }
  6. });

2 - Send data to server

  1. ws.send("Hello World");

or send to specific channel

  1. ws.send("path/to/channel","Hello Channel");

3 - Close Connection and remove listners

  1. ws.end();

License

  1. Licensed under the Apache License, Version 2.0 (the "License");
  2. you may not use this file except in compliance with the License.
  3. You may obtain a copy of the License at
  4. http://www.apache.org/licenses/LICENSE-2.0
  5. Unless required by applicable law or agreed to in writing, software
  6. distributed under the License is distributed on an "AS IS" BASIS,
  7. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. See the License for the specific language governing permissions and
  9. limitations under the License.