Node Object Sharing (Socket) | Remote Method Invocation | Support for callbacks
Oshare
Android client ofStep 1.
Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2.
Add the dependency
dependencies {
compile 'com.github.fcannizzaro:oshare-android:1.0.3'
}
Before Start
var io = share.server(3000, shared, callback, 'java');
- insert language param 'java' and run the server. (only once)
- a Remote.java file will be created in the root.
Usage
1. Share Methods/Fields
@Share('constant.java')
private Integer java = 8;
@Share
private void alert(String str) {
System.out.println("Alert: " + str);
}
2. Register Sharing Classes
Shared.register(this);
3. Connect Socket
authorization, ReadyListener listener ])
init(String url, Class remote, [ , StringOshare.init("http://localhost:3000", Remote.class, this);
4. Attach ReadyListener (Optional)
public class Main implements ReadyListener {
@Override
public void onReady() {
// this listener is called when data is updated.
}
@Override
public void onConnected() {
// this listener is called when socket is connected.
}
}
Details
Annotations
@Share([, value])
- value fake name/path (default = field name).
@Callback
Mark method as callback.
Callback Usage
@Callback
void apiRun(String value, Integer number) {
// do something
}
@Override
public void onConnected() {
// add callback argument
Remote.api.run(Oshare.Cb(this));
// pass your arguments
Remote.api.submodule.hello("fcannizzaro", 22);
}
Callback names should at least contain method path.
Samples:
- Remote.api.run() -> apiRun()
- Remote.submodule.hello() -> submoduleHello()
Multiple Callbacks
@Callback
void multi0(String value) {
// do something with first callback
}
@Callback
void multi1(Integer value) {
// do something with second callback
}
@Override
public void onConnected() {
// add callback arguments
// callbacks are assigned in order.
// multi0 , multi1
Remote.multi(Oshare.Cb(this), Oshare.Cb(this));
}