如何使用ASP.net MVC中的websockets将数据保存到SQL数据库中? 我想在我的视图上显示数据。然后,当数据发生变化时 - 我想将新数据发送到控制器并在数据库上覆盖它们。显然我想在视图上显示当前数据。 这应该作为监测汇率的应用程序。
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://someurl.com/data");
ws.onopen = function ()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt)
{
// var received_msg = evt.data;
var data = JSON.parse(evt.data);
alert("Message is received - so now data on view should refresh and save to database...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>