Skip to content

Instantly share code, notes, and snippets.

@KitaitiMakoto
Last active August 29, 2015 14:20
Show Gist options
  • Save KitaitiMakoto/ce91df33609c44c3738d to your computer and use it in GitHub Desktop.
Save KitaitiMakoto/ce91df33609c44c3738d to your computer and use it in GitHub Desktop.
Socket.IOでメッセージが届いたことを確認する ref: http://qiita.com/KitaitiMakoto/items/aedf9737b2fc7fd70f8f
// クライアント側
var ws = io();
// 色々初期化処理
post(data).then(function(response) {
doSomethingWith(response);
});
function post(data) {
return new Promise(function(resolve, reject) {
ws.emit("post", data, resolve);
});
}
// クライアント側
var ws = io();
// 色々初期設定
userActionStream // 「保存ボタンを押す」とかのアクション
.pipeThrough(actionToData()) // DOMツリーからデータ集めて整形
.pipeThrough(post(ws)) // ここの話なので下に実装置いときます
.pipeTo(soSomethingWithResponse());
function post(ws) {
return new TransformStream({
transform: function(data, enqueue, done) {
ws.emit("post", data, function ack(response) {
enqueue(response);
done();
});
}
});
}
// クライアント側
var ws = io();
// 色々初期化処理
ws.emit("post", data, function onack(response) {
// データが届いたことを確認できた
doSomethingWith(response);
});
// サーバー側
// 色々初期化処理
var io = require("socket.io")(server);
io.on("connection", function(user) {
user.on("post", function(data, ack) {
// dataをDBに保存したりする
// ユニークキーが割り振られたりする
data.id = dbRecord.id;
ack(data);// データが届いたことの確認、兼、追加データの通知
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment