Last active
August 29, 2015 14:20
-
-
Save KitaitiMakoto/ce91df33609c44c3738d to your computer and use it in GitHub Desktop.
Socket.IOでメッセージが届いたことを確認する ref: http://qiita.com/KitaitiMakoto/items/aedf9737b2fc7fd70f8f
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
// クライアント側 | |
var ws = io(); | |
// 色々初期化処理 | |
post(data).then(function(response) { | |
doSomethingWith(response); | |
}); | |
function post(data) { | |
return new Promise(function(resolve, reject) { | |
ws.emit("post", data, resolve); | |
}); | |
} |
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
// クライアント側 | |
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(); | |
}); | |
} | |
}); | |
} |
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
// クライアント側 | |
var ws = io(); | |
// 色々初期化処理 | |
ws.emit("post", data, function onack(response) { | |
// データが届いたことを確認できた | |
doSomethingWith(response); | |
}); |
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
// サーバー側 | |
// 色々初期化処理 | |
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