forked from crossbario/autobahn-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Android echo and broadcast demo clients
- Loading branch information
Tobias Oberstein
committed
Feb 2, 2012
1 parent
986d12a
commit a461dc4
Showing
8 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
|
||
# Project target. | ||
target=android-8 | ||
android.library.reference.1=../../Autobahn |
This file contains 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
This file contains 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
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> |
This file contains 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
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> |
169 changes: 162 additions & 7 deletions
169
Demo/BroadcastClient/src/de/tavendo/autobahn/broadcastclient/BroadcastClientActivity.java
This file contains 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
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()); | ||
} | ||
}); | ||
} | ||
} |