问题
Hi i just started learning SignalR v2. I have a textarea in my aspx page and i want to update it in realtime whenever a user presses a key on it.
Issue: I opened 2 tabs in my browser. If i type the first character example "a", nothing happens: Tab1 textarea: "a" tab2 textarea: NOTHING
Then i proceed to type the second character "b" Tab1 textarea: "ab" tab2 textarea: "a"
Conclude: Its always not updating the latest character i typed!
SignalR Class
public class DocHub : Hub
{
public void Update(String text)
{
this.Clients.Others.updateText(text);
}
}
index.aspx
<body>
<form id="form1" runat="server">
<div style="font-size:50px;"> New Document</div>
<textarea rows="4" cols="50" id="ta"></textarea>
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/jquery.signalR-2.1.2.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function () {
var hubProxy = $.connection.docHub;
hubProxy.client.updateText = function (msg) {
$("#ta").val(msg);
};
$("#ta").keydown(function () {
hubProxy.server.update($("#ta").val());
});
$.connection.hub.start();
});
</script>
</form>
</body>
回答1:
I really would try to use keyup instead
来源:https://stackoverflow.com/questions/30326904/signalr-updating-textarea-realtime