建议我从 websocket 服务器检索实时数据 [英] Suggest me to retrieve real time data from websocket server

查看:35
本文介绍了建议我从 websocket 服务器检索实时数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事基于 GPS 的项目,我目前正在使用 volley 从网络接收数据,但最近他们计划使用实时数据升级服务.因此,Web 开发人员使用 websocket 对服务器进行了编程,对于他们所说的浏览器应用程序,他们使用 stomp.js 协议从服务器检索数据.他们要求我从他们的服务器(实时数据)中检索数据,但我不知道从 websocket 获取实时数据的最佳方法是什么.我从 github https://github.com/NaikSoftware/StompProtocolAndroid 找到了一个例子,但对我来说还不够学习和使用 stomp 的文档在互联网上是不够的.我希望在 android 中可以有更好的实现.

I am currently working on an GPS based project, I am currently using volley to receive data from web but recently they plan to upgrade the services with real time data. so web developers have programmed the server using websocket, and for browser application they said, they are using stomp.js protocol for retrieving data from server. They requested me to retrieve data from their server (Real Time Data) use any, but I don't know what will be the best suitable method to get real time data from websocket. I found one example from github https://github.com/NaikSoftware/StompProtocolAndroid but it's not enough for me to learn, and documentation for using stomp are not enough on internet. I hope there could be better implementation in android.

请提供任何帮助和建议,我们将不胜感激.请不要提及我的 firebase,因为我已经提到过他们关于 firebase 的事情,但他们拒绝使用它.请大家帮助我,给我建议或给我一个很好的例子来简单地使用 volley 从 websocket 服务器检索数据..

Please any help and suggestion would be appreciated. Please don't mention me firebase because I have already mentioned them about firebase and they refused to use it. Please guys help me, suggest me or give me one good example to simply retrieve data from websocket server using volley..

推荐答案

我的回答基于此问题中的评论.没有关于该问题的足够信息,所以我正在为您的问题编写 2 个解决方案

My answer is based on the comments in this question. Don't have enough information on the question so I am writing 2 solution for your question

解决方案 1 - 不使用 Stomp 实现 - 简单的 websocket

Solution 1 - Without using Stomp Implementation- Simple websocket

这是一个简单的 Websocket 实现.为此,您可以使用 koush/AndroidAsync

This is a simple Websocket implementation. for this you can use koush/AndroidAsync library

在你的项目中实现这个项目

implement this project into your project

    dependencies {
    compile 'com.koushikdutta.async:androidasync:2.+'
}

然后连接到Websocket服务器

then connect to Websocket server

 String url="ws://172.17.1.54:8000/";
    AsyncHttpClient.getDefaultInstance().websocket(url, "my-protocol", new WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            if (ex != null) {
                ex.printStackTrace();
                return;
            }

            webSocket.setStringCallback(new StringCallback() {
                public void onStringAvailable(String s) {
                    System.out.println("I got a string: " + s);
                }
            });
            webSocket.setDataCallback(new DataCallback() {
                public void onDataAvailable(DataEmitter emitter, ByteBufferList byteBufferList) {
                    System.out.println("I got some bytes!");
                    // note that this data has been read
                    byteBufferList.recycle();
                }
            });
        }
    });

这里 setStringCallback() &setDataCallback() 将接收您的实时更新.

here setStringCallback() & setDataCallback() will receive your real time update.

您也可以为此使用 codebutler/android-websockets 库.

You can also use codebutler/android-websockets library for this.

解决方案 2:使用 Stomp 实现

Solution 2: With use Stomp implementation

为此,您可以使用 Gozirra java 库来解决这个问题.您可以下载 客户端库.然后将其放入您的libs 文件夹.

For this you can use Gozirra java library for solving this problem. You can download client library.then put it into your libs folder.

用于连接到您的服务器

 Client c = new Client("server url", port, "login", "password");

为更新创建一个监听器

 Listener listener=new Listener() {
            @Override
            public void message(Map map, String s) {
                //Do your stuff
            }
        };

然后订阅您的主题消息

 c.subscribe("foo-channel", listener);

如果您想取消订阅,您可以使用以下代码

If you want to unsubscribe you can use below code

  c.unsubscribe("foo-channel", listener);  // Unsubscribe only one listener

 c.unsubscribe("foo-channel");   // Unsubscribe all listeners

用于断开客户端

c.disconnect();

我没有在真实服务器上测试过.但我认为这会起作用.请告诉我它是否能解决您的问题

I didn't test this with a real server .But I think this will work.Please let me know if it solves your problem

解决方案 3

正如您提到的库 https://github.com/NaikSoftware/StompProtocolAndroid你可以用这个.简化版如下

As you mention the library https://github.com/NaikSoftware/StompProtocolAndroid You can use this. simplify version is as follows

将 Maven 链接添加到您的项目级 gradle

Add maven link into your project level gradle

  repositories {
        maven { url "https://jitpack.io" }
    }

在你的模块级gradle中添加依赖

Add dependency in your module level gradle

 implementation 'com.github.NaikSoftware:StompProtocolAndroid:1.1.5'
 implementation 'org.java-websocket:Java-WebSocket:1.3.0'

然后使用以下代码获取消息

Then use following code for getting message

 StompClient mStompClient = Stomp.over(WebSocket.class, "ws://10.0.2.2:5000/");


    mStompClient.topic("/topic/general").subscribe(new Action1<StompMessage>() {
        @Override
        public void call(StompMessage stompMessage) {
            Log.e(TAG, stompMessage.getPayload());
        }
    });
    mStompClient.lifecycle().subscribe(new Action1<LifecycleEvent>() {
        @Override
        public void call(LifecycleEvent lifecycleEvent) {
            switch (lifecycleEvent.getType()) {

                case OPENED:
                    Log.e(TAG, "Stomp connection opened");
                    break;

                case ERROR:
                    Log.e(TAG, "Error", lifecycleEvent.getException());
                    break;

                case CLOSED:
                    Log.e(TAG, "Stomp connection closed");
                    break;
            }
        }
    });
    mStompClient.connect();

这篇关于建议我从 websocket 服务器检索实时数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆