julianschuh / Android_Pusher

A simple pusher implementation and activity for Android!

Home Page:http://www.emorym.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android_Pusher

This code provides a simple implementation allowing to connect to a socky websocket server.

This project makes use of a slightly modified Weberknect, available at http://v.google.com/p/weberknecht/

This project is under the Apache License, Version 2.0! http://www.apache.org/licenses/LICENSE-2.0.html

Examples:

Pusher pusher = new Pusher("ws://ws.test.com/app");
PusherChannel channel = pusher.subscribe("chat-channel");
channel.bind( "new_message" , new PusherCallback() {
  public void onEvent(String eventName, JSONObject eventData, String channelName) {
    Toast.makeText(getApplicationContext(),
    							 "Received\nEvent: " + eventName + "\nChannel: " + channelName + "\nData: " + eventData.toString(),
    							 Toast.LENGTH_LONG).show();
    /* this code runs in the UI thread by default, so you can change UI elements */
    myTextField.setText( eventData.getString("content") );
  }
});

you can bind to any event of a channel as long as you are subscribed to it:

channel.bindAll(new PusherCallback() {
  public void onEvent(String eventName, JSONObject eventData, String channelName) {
    Log.d( "Pusher", "Received " + eventData.toString() + " for event '" + eventName +"' on channel " + channelName );
  }
});

by default, all callbacks run on the UI thread, you can change that behaviour by passing another Looper to PusherCallbacks's constructor

HandlerThread hd = new HandlerThread("i'm a separate thread");
hd.start();

channel.bindAll(new PusherCallback(hd.getLooper()) { // if none given: Looper.getMainLooper()
  public void onEvent(String eventName, JSONObject eventData, String channelName) {
    /* i'm fine */
    Log.d( "Pusher", "I will show up on any event on any subscribed channel" );

    /* note that the following wont work, since we aren't in the ui thread. Toasts will work, though */
    someTextField.setText("i will kill your app");
  }
});

You can even subscribe to private channels:

pusher.subscribe("private-test", new PusherAuthenticationCallback() {
	public void authenticate(Pusher pusher, Map<String, String> authParams, PusherChannel channel) {
		Log.d(LOG_TAG, "Joining channel " + channel.getName() + ", Params: " + authParams.toString());
							
		String auth = "...";
		// Retrieve the authentication token, e.g. by sending a http request to the application server
		pusher.subscribe(channel, auth);
	}
});

About

A simple pusher implementation and activity for Android!

http://www.emorym.com


Languages

Language:Java 100.0%