public class ChatHub : Hub
{
private readonly ChatTicker ticker;
public ChatHub()
{
ticker = ChatTicker.Instance;
}
public void Send(string group, string username)
{
//注册到全局
ticker.GlobalContext.Groups.Add(Context.ConnectionId, group);
Clients.All.broadcastMessage(group, "user register:" + username);
}
}
public class ChatTicker
{
#region 实现一个单例
private static readonly ChatTicker _instance =
new ChatTicker(GlobalHost.ConnectionManager.GetHubContext<ChatHub>());
private readonly IHubContext m_context;
private ChatTicker(IHubContext context)
{
m_context = context;
//这里不能直接调用Sender,因为Sender是一个不退出的“死循环”,否则这个构造函数将不会退出。
//其他的流程也将不会再执行下去了。所以要采用异步的方式。
Task.Run(() => Sender());
}
public IHubContext GlobalContext
{
get { return m_context; }
}
public static ChatTicker Instance
{
get { return _instance; }
}
#endregion
public void Sender()
{
int count = 0;
while (true)
{
Thread.Sleep(500);
int tag = count%2;
//动态绑定前端的js函数 broadcaseMessage
m_context.Clients.Group(tag + "").broadcastMessage("group is:" + tag, "current count:" + count);
count++;
}
}
}<!DOCTYPE html>
<html>
<head>
<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">
<label>input your name:</label>
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Register" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
$.connection.hub.logging = true;
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.
$(‘#displayname‘).val(prompt(‘Enter your group, choose: 0 or 1:‘, ‘‘));
// Set initial focus to message input box.
$(‘#message‘).focus();
// Start the connection.
$.connection.hub.start().done(function () {
$(‘#sendmessage‘).click(function () {
// Call the Send method on the hub.
chat.server.send($(‘#displayname‘).val(), $(‘#message‘).val());
// Clear text box and reset focus for next comment.
$(‘#message‘).val(‘‘).focus();
});
});
});
</script>
</body>
</html>

SignalR实现服务器推送信息:广播与“组播”,布布扣,bubuko.com
原文:http://blog.csdn.net/kmguo/article/details/20042471