Skip to content

Commit

Permalink
add Android echo and broadcast demo clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Oberstein committed Feb 2, 2012
1 parent 986d12a commit a461dc4
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Demo/BroadcastClient/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="src" path="Autobahn_src"/>
<classpathentry kind="lib" path="/Autobahn/lib/jackson-core-asl-1.8.5.jar"/>
<classpathentry kind="lib" path="/Autobahn/lib/jackson-mapper-asl-1.8.5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
7 changes: 7 additions & 0 deletions Demo/BroadcastClient/.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<linkedResources>
<link>
<name>Autobahn_src</name>
<type>2</type>
<locationURI>_android_Autobahn_59a1d7d4/src</locationURI>
</link>
</linkedResources>
</projectDescription>
1 change: 1 addition & 0 deletions Demo/BroadcastClient/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BroadcastClientActivity"
Expand Down
1 change: 1 addition & 0 deletions Demo/BroadcastClient/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

# Project target.
target=android-8
android.library.reference.1=../../Autobahn
41 changes: 34 additions & 7 deletions Demo/BroadcastClient/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
android:weightSum="1" android:background="#028ec9" android:padding="12dip">
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textColor="#ffffff" android:textStyle="bold" android:gravity="center" android:text="Autobahn Broadcast Client" android:layout_width="match_parent" android:paddingBottom="16dip" android:textSize="22dip"></TextView>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/lightgrey" android:text="Autobahn Server Hostname / Port"></TextView>
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
<EditText android:id="@+id/hostname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/darkgrey"></EditText>
<EditText android:id="@+id/port" android:layout_width="match_parent" android:inputType="number" android:layout_height="wrap_content" android:layout_weight="3" android:textColor="@color/darkgrey"></EditText>
</LinearLayout>
<Button android:layout_gravity="center" android:layout_margin="16dip" android:id="@+id/start" android:bufferType="editable" android:text="Button" android:layout_width="100dip" android:layout_height="50dip"></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/lightgrey" android:id="@+id/statusline" android:text="Ready." android:textSize="14dip" android:layout_marginBottom="12dip"/>
<TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/lightgrey" android:text="Message to broadcast"></TextView>
<EditText android:id="@+id/msg" android:layout_width="match_parent" android:textColor="@color/darkgrey" android:text="Hello, world!" android:maxLines="1" android:layout_height="wrap_content"></EditText>
<Button android:layout_gravity="center" android:layout_margin="16dip" android:id="@+id/sendMsg" android:bufferType="editable" android:text="Send" android:layout_height="50dip" android:layout_width="100dip"></Button>

<ScrollView
android:id="@+id/logscroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">

<TextView
android:id="@+id/log"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>

</ScrollView>
<!--
<TextView android:id="@+id/log" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/lightgrey" android:text="" android:layout_height="match_parent" android:layout_width="fill_parent" android:scrollbars="vertical" android:maxLines="1000"></TextView>
-->
</LinearLayout>
6 changes: 6 additions & 0 deletions Demo/BroadcastClient/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="autobahn">#028ec9</color>
<color name="lightgrey">#cccccc</color>
<color name="darkgrey">#333333</color>
</resources>
3 changes: 1 addition & 2 deletions Demo/BroadcastClient/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BroadcastClientActivity!</string>
<string name="app_name">BroadcastClient</string>
<string name="app_name">Autobahn Broadcast Client</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,13 +1,168 @@
/******************************************************************************
*
* Copyright 2011 Tavendo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

package de.tavendo.autobahn.broadcastclient;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;

public class BroadcastClientActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

static final String TAG = "de.tavendo.autobahn.broadcast";
private static final String PREFS_NAME = "AutobahnAndroidBroadcast";

static EditText mHostname;
static EditText mPort;
static TextView mStatusline;
static Button mStart;

static EditText mMessage;
static Button mSendMessage;

static TextView mLog;
static ScrollView mLogScroller;

private SharedPreferences mSettings;

private void alert(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

private void loadPrefs() {

mHostname.setText(mSettings.getString("hostname", ""));
mPort.setText(mSettings.getString("port", "9000"));
}

private void savePrefs() {

SharedPreferences.Editor editor = mSettings.edit();
editor.putString("hostname", mHostname.getText().toString());
editor.putString("port", mPort.getText().toString());
editor.commit();
}

private void setButtonConnect() {
mHostname.setEnabled(true);
mPort.setEnabled(true);
mStart.setText("Connect");
mStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
start();
}
});
}

private void setButtonDisconnect() {
mHostname.setEnabled(false);
mPort.setEnabled(false);
mStart.setText("Disconnect");
mStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mConnection.disconnect();
}
});
}

private final WebSocketConnection mConnection = new WebSocketConnection();

private void start() {

final String wsuri = "ws://" + mHostname.getText() + ":" + mPort.getText();

mStatusline.setText("Status: Connecting to " + wsuri + " ..");

setButtonDisconnect();

try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
mStatusline.setText("Status: Connected to " + wsuri);
savePrefs();
mSendMessage.setEnabled(true);
mMessage.setEnabled(true);
}

@Override
public void onTextMessage(String payload) {
mLog.setText(mLog.getText() + "\n" + payload);
mLogScroller.post(new Runnable()
{
public void run()
{
mLogScroller.smoothScrollTo(0, mLog.getBottom());
}
});
}

@Override
public void onClose(int code, String reason) {
alert("Connection lost.");
mStatusline.setText("Status: Ready.");
setButtonConnect();
mSendMessage.setEnabled(false);
mMessage.setEnabled(false);
}
});
} catch (WebSocketException e) {

Log.d(TAG, e.toString());
}
}

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mHostname = (EditText) findViewById(R.id.hostname);
mPort = (EditText) findViewById(R.id.port);
mStatusline = (TextView) findViewById(R.id.statusline);
mStart = (Button) findViewById(R.id.start);
mMessage = (EditText) findViewById(R.id.msg);
mSendMessage = (Button) findViewById(R.id.sendMsg);
mLog = (TextView) findViewById(R.id.log);
mLogScroller = (ScrollView) findViewById(R.id.logscroller);

mSettings = getSharedPreferences(PREFS_NAME, 0);
loadPrefs();

setButtonConnect();
mSendMessage.setEnabled(false);
mMessage.setEnabled(false);

mSendMessage.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mConnection.sendTextMessage(mMessage.getText().toString());
}
});
}
}

0 comments on commit a461dc4

Please sign in to comment.