Android client receives message push from Faye

Source: Internet
Author: User

Android client receives message push from Faye Mar 3rd, 2014 Basic Principles

First read the 260 Mes-saging with Faye section of rails-casts to understand the basic principles of Faye.

According to the tutorial, Google solved the thin bug and later found that rubych-ina should have encountered similar problems.

The solution is to addFaye::WebSocket.load_adapter('thin')

require 'faye'Faye::WebSocket.load_adapter('thin')faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)run faye_server

Of course, in theory, Faye supports various web servers. Faye is used because it is closely related to Rails. If more concurrency is considered, select Node .? Js is also good, and faye also has a Node version.

Implementation

After the web end is implemented, consider porting it to the android Client

Google an-droid faye, found that someone has made an Android-based faye-cli-ent, according to the web-socket protocol, it is not difficult to implement one.

Compare the js Code of the faye Client

$(function() {  var faye = new Faye.Client('http://localhost:9292/faye');  faye.subscribe('/messages/new', function (data) {  alert(data);  });});

And An-droid-Faye-Cli-ent java code

URI uri = URI.create(String.format("ws://%s:443/events", baseUrl));String channel = String.format("/%s/**", User.getCurrentUser().getUserId());mClient = new FayeClient(uri, channel);mClient.setFayeListener(this);mClient.connectToServer(null);

We know how to implement it. We need to point out that the js client uses the http protocol directly, while what we need on the java side is the ws protocol (that is, Web-Socket, wss is similar to https and is an enhanced security edition of ws)

If you are using An-droid Stu-diobuild.gradleAdd the following dependency:

dependencies {    compile 'com.saulpower.fayeclient:library:1.0'}

Because this application is based on Ruby-China, you can view the source code of the home page of RubyCh-ina on the port number. The chan-nel number is also visible in the home page source code, with a tem-p_ac-cess _-token, this token is specifically used for faye chan-nel number, this token can be obtained through the api of the first version.

<script type="text/javascript" data-turbolinks-eval=false>  ROOT_URL = "http://ruby-china.org/";  ASSET_URL = "http://l.ruby-china.org/";  FAYE_SERVER_URL = "http://ruby-china.org:8080/faye";    CURRENT_USER_ACCESS_TOKEN = "72a47853462c91db8f7ed8d3d8899d00";  if (FAYE_SERVER_URL) {    App.initNotificationSubscribe();  }</script>

In the RubyCh-in-a4Android, the user gets the tem-p_ac-cess _-token and registers Ser-veice when logging on. Next, if someone @ you in the forum, you should be able to receive the message push on your mobile phone.

Source Code

All of the above Code can be found in the RubyCh-in-a4Android

Original article from: gonjay. github. io

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.