详解在ASP.NET Core下使用SignalR技术(2)

var userName = prompt("Enter your name: "); var chat = $connectionchatHub; chatclientmessageReceived = function (originatorUser, message) { $("#messages")append('<li><strong>' + originatorUser + '</strong>: ' + message); }; chatclientgetConnectedUsers = function (userList) { for (var i = 0; i < userListlength; i++) addUser(userList[i]); }; chatclientnewUserAdded = function (newUser) { addUser(newUser); } $("#messageBox")focus(); $("#sendMessage")click(function () { chatserversend(userName, $("#messageBox")val()); $("#messageBox")val(""); $("#messageBox")focus(); }); $("#messageBox")keyup(function (event) { if (eventkeyCode == 13) $("#sendMessage")click(); }); function addUser(user){ $("#userList")append('<li>' + user + '</li>'); } $connectionhublogging = true; $connectionhubstart()done(function () { chatserverconnect(userName); });

最后我们来运行它吧:

详解在ASP.NET Core下使用SignalR技术

四、最后

附上一个可用的Demo:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdpspj.html