Skip to content

Instantly share code, notes, and snippets.

@iwata-n
Created July 6, 2016 16:16
Show Gist options
  • Save iwata-n/572465bc468c96513cd6babd5f53e636 to your computer and use it in GitHub Desktop.
Save iwata-n/572465bc468c96513cd6babd5f53e636 to your computer and use it in GitHub Desktop.
milkcocoa+javaでPushとSendをするサンプル
package jp.dip.iwatan;
import com.mlkcca.client.*;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Date;
public class Main {
public static void main(String[] args) throws InterruptedException, JSONException {
MilkCocoa milkCocoa = new MilkCocoa("flagiqah22fs.mlkcca.com");
DataStore dataStore = milkCocoa.dataStore("message");
Streaming streaming = dataStore.streaming();
streaming.size(1);
streaming.addStreamingListener(new StreamingListener() {
@Override
public void onData(ArrayList<DataElement> arrayList) {
for (DataElement element : arrayList) {
System.out.print("onData:");
System.out.println(element.getValue());
}
}
@Override
public void onError(Exception e) {
System.out.print("onError:");
e.printStackTrace();
}
});
streaming.next();
dataStore.addDataStoreEventListener(new DataStoreEventListener() {
@Override
public void onPushed(DataElement dataElement) {
System.out.print("onPushed:");
System.out.println(dataElement.getValue());
}
@Override
public void onSetted(DataElement dataElement) {
}
@Override
public void onSended(DataElement dataElement) {
System.out.print("onSended:");
// System.out.println(dataElement.getValue());
}
@Override
public void onRemoved(DataElement dataElement) {
}
});
dataStore.on("push");
dataStore.on("send");
while(true) {
Thread.sleep(500);
Date date = new Date();
// DataElementValue params = new DataElementValue();
// params.put("date", date.getTime());
// dataStore.push(params);
JSONObject object = new JSONObject();
object.put("date", date.getTime());
dataStore.send(object);
System.out.println("Send");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment