您的位置:首页 > 其它

signalR消息实时推送

2015-06-09 17:53 323 查看
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>SignalR Simple Chat</title>
<style type="text/css">
.container{
background-color:#99ccFF;
border:thick solid #808080;
padding:20px;
margin:20px;
}
</style>
</head>
<body>
<div class="container ">
<input type="text" id="mm"/>
<input type="button" id="gg" value="Send " />
<input type="hidden" id="nn" />
<ul id="discussion">
</ul>
</div>

<script src="Scripts/jquery-1.7.1.min.js"></script>

<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>

<script src="/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>:  ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#nn').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#mm').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#gg').click(function () {

// Call the Send method on the hub.
chat.server.send($('#nn').val(), $('#mm').val());
// Clear text box and reset focus for next comment.
$('#mm').val('').focus();
});

});

});
</script>
</body>
</html>


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: