問題描述
Socket.io 和 Extjs:為 Store 檢索數據 (Socket.io and Extjs: Retrieving data for Store)
Extjs Store 有沒有辦法以實時提要的方式從 Socket.io 獲取數據?沒有輪詢的地方。
我發現需要輪詢,我將使用 express 從 URL 獲取 json 格式的數據。我在想我然後做一個 setTimeout 來為 Store 加載。就像我說的投票一樣。
var app = express();
app.get('/getnames', function(request, response) {
response.json({data:[{id:1,name:"John"}, {id:2, name:"James"}]});
});
還有其他方法嗎?
或者有什麼建議嗎?除了使用 setTimeout。
謝謝
參考解法
方法 1:
You dont need to use longpolling for websocket frameworks. All you need to do, is to listen events and write handlers. For example;
var socket = io.connect("localhost:8080");
socket.on("feed", function(data){
myStore.loadData([data], true);
});
When you emit "feed" event from your socket.io server, above code will add the feed data to the store immediately. To emit data on your socket.io server with node.js;
io.emit("feed", {"id":123, "msg":"example feed"});
Of course, you have to write the logic that when someone(or your app) post a feed, it should emit a message to the server, and the server must emit this to the other clients. This way, all clients have a realtime feed viewer.
(by oneofakind、abeyaz)