android tcpstomp协议,GitHub - boumba100/android-stomp-websocket: Android STOMP client library via webs...

android-stomp-websocket

This library was created to allow android devices to communicate whith STOMP servers via websocket.

Getting started

To get started, download the project from this repository. This project contains the STOMP client library(wstompclient) and an example app using the library. Since the project is created with Android Studio, you can simply open it with the IDE and run it on an emulator or a physical device.

If you want to include the wstompclient library into your own project do as the following:

Android Studio

ClickFile → New → Import Module and select the wstompclient folder.

The step above will add ':wstompclient' to the settings.gradle file as shown below.

include ':app', ':wstompclient'

To compile the library in your application simply add this line to the project's build.gradle file.

dependencies {

...

compile project(':stompclient')

...

}

Example code for android client

Stomp server connection

Private StompClient stompClient;

//connect to server

stompClient = new StompClient("ws://:port/endpoint") { //example "ws://localhost:8080/message-server"

@Override

protected void onStompError(String errorMessage) {

Log.d(TAG, "error : " + errorMessage);

}

@Override

protected void onConnection(boolean connected) {

Log.d(TAG, "connected : " + String.valueOf(connected));

}

@Override

protected void onDisconnection(String reason) {

Log.d(TAG, "disconnected : " + reason);

}

@Override

protected void onStompMessage(final Frame frame) {

runOnUiThread(new Runnable() {

@Override

public void run() {

Toast.makeText(getApplicationContext(), frame.getBody(), Toast.LENGTH_SHORT).show();

}

});

}

};

}

// disconnect from stomp server and websocket

stompClient.unSubscribe("destincation");

For different protocol use this constructor

new StompClient("ws://:port/endpoint", new Draf_...()) {

...

}

Subscription

stompClient.

(destination); // subscribe to topic

stompClient.unSubscribe(destination); // unsubscribes to topic

Send message

stompClient.sendMessage(destination, string);

Custom frames

This library already includes simple STOMP protocol frame templates for these commands :

CONNECT

DISCONNECT

SEND

SUBSCRIBE

UNSUBSCRIBE

If you want to add extra headers to the frames before sending it, you can easily do as below :

Frame connectionFrame = new Frame().setConnection(destination).append("login", "username").append("password", "password123");

stompClient.sendFrame(connectionFrame);

If you simply want to create a fully custom frame you can do as bellow

new Frame().setCommand(command).append(name, value);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值