私は publishCreate メッセージブロードキャストに固執しましたが、何が間違っているのかわかりません。
Sample という名前の単純なモデルを作成しました。
module.exports = {
attributes: {
device: 'string',
value: 'float'
},
afterCreate: function(sample, next) {
console.log("afterCreate called");
Sample.publishCreate({value: sample.value});
console.log("publishCreate sent");
next();
}
};
モデルが自動的に publishCreate を行うかどうかをドキュメントで見つけられなかったので、afterCreate を追加しました。
次に、次のビューを作成しました。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/styles/iphone.css">
<!-- Bring in the socket.io client -->
<script type="text/javascript" src="/js/socket.io.js"></script>
<!-- then beef it up with some convenience logic for talking to Sails.js -->
<script type="text/javascript" src="/js/sails.io.js"></script>
<!-- listen on socket.io for incoming messages -->
<script type="text/javascript" src="/js/app.js"></script>
<script type="text/javascript">
socket.on('message', function(msg){
alert('message received');
});
</script>
</head>
<body>
<div class="block" style="height: 320px;">
<div class="centered">
<h1><%= temp %>°</h1>
</div>
</div>
</body>
</html>
理論的には、ブラウザでビューを呼び出してから、他のブラウザタブで呼び出すと
http://localhost:1337/sample/create?device=AA&value=10.0
上記のビューでクライアント側でメッセージを受信する必要がありますが、何も起こりません。
メッセージから、ソケットが接続され、新しいサンプルが作成されると publishCreate が呼び出されることがわかります。
理由は何ですか?res.view() を呼び出すときにコントローラーで何かをする必要がありますか?