Skip to content

Instantly share code, notes, and snippets.

@shogochiai
Created April 3, 2015 01:20
Show Gist options
  • Save shogochiai/04bdae3a6fe9af3acade to your computer and use it in GitHub Desktop.
Save shogochiai/04bdae3a6fe9af3acade to your computer and use it in GitHub Desktop.
PubNubをmilkcocoaで包んでsendだけ使えるようにしたやつ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>milkpubfire test</title>
</head>
<body>
<input type="text" id="pub" />
<script src="https://cdn.pubnub.com/pubnub-dev.js"></script>
<script src="milkcocoa-sync.js"></script>
<script src="index.js"></script>
</body>
</html>
(function(){
var milkcocoa = new MilkCocoa();
var ds = milkcocoa.dataStore("hoge");
var input = document.getElementById("pub");
ds.on("send", function(err, data){
console.log(err, data);
})
input.addEventListener("keypress", function(e){
if(e.keyCode == 13) ds.send({data: input.value});
});
}());
(function(global){
function MilkCocoa() {
this.pubnub = PUBNUB.init({
publish_key: 'pub-c-3110c846-acee-4a93-b334-605c31524237',
subscribe_key: 'sub-c-8472c902-d950-11e4-895c-02ee2ddab7fe'
});
}
MilkCocoa.prototype.dataStore = function(path) {
return new DataStore(this, path);
}
function DataStore(milkcocoa, path) {
this.parent = milkcocoa;
this.path = path;
}
DataStore.prototype.send = function(params) {
var milkcocoa = this.parent;
var self = this;
milkcocoa.pubnub.publish({channel : self.path, message : params});
}
DataStore.prototype.on = function(event, cb) {
var milkcocoa = this.parent;
var self = this;
if(event == "send") {
milkcocoa.pubnub.subscribe({
channel : self.path,
message : function(data){ cb(null, data); },
error : function(error){ cb(error, null); }
});
}
}
global.MilkCocoa = MilkCocoa;
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment