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.
- Loading branch information
Tobias Oberstein
committed
Oct 17, 2011
1 parent
bd93fc0
commit 3c7f400
Showing
5 changed files
with
199 additions
and
7 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 |
---|---|---|
@@ -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> |
174 changes: 174 additions & 0 deletions
174
Demo/SimpleRpc/src/de/tavendo/autobahn/simplerpc/SimpleRpcActivity.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 |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/****************************************************************************** | ||
* | ||
* 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.simplerpc; | ||
|
||
import java.util.ArrayList; | ||
|
||
import android.app.Activity; | ||
import android.content.SharedPreferences; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
import de.tavendo.autobahn.Autobahn; | ||
import de.tavendo.autobahn.AutobahnConnection; | ||
|
||
public class SimpleRpcActivity extends Activity { | ||
|
||
@SuppressWarnings("unused") | ||
private static final String TAG = "de.tavendo.autobahn.simplerpc"; | ||
|
||
private static final String PREFS_NAME = "AutobahnAndroidSimpleRpc"; | ||
|
||
private SharedPreferences mSettings; | ||
|
||
private static EditText mHostname; | ||
private static EditText mPort; | ||
private static TextView mStatusline; | ||
private static Button mStart; | ||
|
||
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() { | ||
mStart.setText("Connect"); | ||
mStart.setOnClickListener(new Button.OnClickListener() { | ||
public void onClick(View v) { | ||
test(); | ||
} | ||
}); | ||
} | ||
|
||
private void setButtonDisconnect() { | ||
mStart.setText("Disconnect"); | ||
mStart.setOnClickListener(new Button.OnClickListener() { | ||
public void onClick(View v) { | ||
mConnection.disconnect(); | ||
} | ||
}); | ||
} | ||
|
||
private final AutobahnConnection mConnection = new AutobahnConnection(); | ||
|
||
private void test() { | ||
|
||
final String wsuri = "ws://" + mHostname.getText() + ":" + mPort.getText(); | ||
|
||
mStatusline.setText("Connecting to\n" + wsuri + " .."); | ||
|
||
setButtonDisconnect(); | ||
|
||
// we establish a connection by giving the WebSockets URL of the server | ||
// and the handler for open/close events | ||
mConnection.connect(wsuri, new Autobahn.SessionHandler() { | ||
|
||
@Override | ||
public void onOpen() { | ||
|
||
// The connection was successfully established. we set the status | ||
// and save the host/port as Android application preference for next time. | ||
mStatusline.setText("Connected to\n" + wsuri); | ||
savePrefs(); | ||
|
||
// We establish a prefix to use for writing URIs using shorthand CURIE notation. | ||
mConnection.prefix("calc", "http://example.com/simple/calc#"); | ||
|
||
testRpc(); | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason) { | ||
|
||
// The connection was closed. Set the status line, show a message box, | ||
// and set the button to allow to connect again. | ||
mStatusline.setText("Connection closed."); | ||
alert(reason); | ||
setButtonConnect(); | ||
} | ||
}); | ||
} | ||
|
||
private void testRpc() { | ||
|
||
ArrayList<Integer> nums = new ArrayList<Integer>(); | ||
for (int i = 0; i < 10; ++i) nums.add(i); | ||
|
||
mConnection.call("calc:asum", Integer.class, new Autobahn.CallHandler() { | ||
|
||
@Override | ||
public void onResult(Object result) { | ||
int res = (Integer) result; | ||
alert("calc:asum result = " + res); | ||
} | ||
|
||
@Override | ||
public void onError(String errorId, String errorInfo) { | ||
alert("calc:asum RPC error - " + errorInfo); | ||
} | ||
}, nums); | ||
|
||
mConnection.call("calc:add", Integer.class, new Autobahn.CallHandler() { | ||
|
||
@Override | ||
public void onResult(Object result) { | ||
int res = (Integer) result; | ||
alert("calc:add result = " + res); | ||
} | ||
|
||
@Override | ||
public void onError(String errorId, String errorInfo) { | ||
alert("calc:add RPC error - " + errorInfo); | ||
} | ||
}, 23, 55); | ||
} | ||
|
||
@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); | ||
|
||
mSettings = getSharedPreferences(PREFS_NAME, 0); | ||
loadPrefs(); | ||
|
||
setButtonConnect(); | ||
} | ||
} |