Last active
April 18, 2017 19:39
-
-
Save FZambia/8517f815eb4ef1fc47a40686a2d6c835 to your computer and use it in GitHub Desktop.
Using centrifuge-mobile gomobile-generated library from Java (for Android development)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.fz.centrifugoandroid; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.widget.TextView; | |
| import centrifuge.Message; | |
| import centrifuge.MessageHandler; | |
| import centrifuge.Sub; | |
| public class AppMessageHandler implements MessageHandler { | |
| protected MainActivity context; | |
| public AppMessageHandler(Context context) { | |
| this.context = (MainActivity) context; | |
| } | |
| @Override | |
| public void onMessage(Sub sub, final Message message) { | |
| context.runOnUiThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| TextView tv = (TextView) ((Activity) context).findViewById(R.id.text); | |
| tv.setText(message.getData()); | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.fz.centrifugoandroid; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.widget.TextView; | |
| import centrifuge.Centrifuge; | |
| import centrifuge.Client; | |
| import centrifuge.Credentials; | |
| import centrifuge.EventHandler; | |
| import centrifuge.MessageHandler; | |
| import centrifuge.Sub; | |
| import centrifuge.SubEventHandler; | |
| public class MainActivity extends AppCompatActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| TextView tv = (TextView) findViewById(R.id.text); | |
| Credentials creds = Centrifuge.newCredentials( | |
| "42", "1488055494", "", | |
| "24d0aa4d7c679e45e151d268044723d07211c6a9465d0e35ee35303d13c5eeff" | |
| ); | |
| EventHandler events = Centrifuge.newEventHandler(); | |
| Client client = Centrifuge.new_( | |
| "ws://192.168.1.33:8000/connection/websocket", | |
| creds, | |
| events, | |
| Centrifuge.defaultConfig() | |
| ); | |
| try { | |
| client.connect(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| tv.setText(e.getMessage()); | |
| return; | |
| } | |
| SubEventHandler subEvents = Centrifuge.newSubEventHandler(); | |
| MessageHandler messageHandler = new AppMessageHandler(this); | |
| subEvents.onMessage(messageHandler); | |
| try { | |
| Sub sub = client.subscribe("channel", subEvents); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment