Skip to content

Instantly share code, notes, and snippets.

@baralong
Created October 4, 2013 08:27
Show Gist options
  • Save baralong/6822725 to your computer and use it in GitHub Desktop.
Save baralong/6822725 to your computer and use it in GitHub Desktop.
A repo for an issue when trying to join a group using SignalR.RabbitMq
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
namespace SignalR.RabbitMq.Example
{
public class Chat : Hub
{
private static int _id = 0;
public void Send(string message)
{
Clients.All.addMessage(string.Format("{0} - {1}", message, _id), Context.ConnectionId);
Clients.Others.addMessage(string.Format("Some one said - {0} - {1}", message, _id), Context.ConnectionId);
_id++;
}
private const string GroupName = "SecretGroup";
public void SendGroup(string message)
{
Clients.Group(GroupName).addMessage(string.Format("{0} - {1}", message, _id), GroupName);
}
public Task JoinGroup(string groupName)
{
return Groups.Add(Context.ConnectionId, GroupName);
}
}
}
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<input type="text" id="msg" />
<a id="broadcast" href="#">Send message</a>
<a id="joingroup" href="#">Join group</a>
<a id="sendgroup" href="#">Send group message</a>
<p>Messages : </p>
<div id="console"> </div>
<ul id="messages"> </ul>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
$.connection.hub.logging = true;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message, from) {
$('#messages').prepend('<li>' + message + " from " + from + '</li>');
};
chat.client.onConsoleMessage = function (message) {
$('#console').html('From the console application : ' + message);
};
$("#broadcast").click(function () {
// Call the chat method on the server
chat.server.send($('#msg').val())
.done(function () {
console.log('Success!');
})
.fail(function (e) {
console.warn(e);
});
});
$("#sendgroup").click(function () {
// Call the chat method on the server
chat.server.sendGroup($('#msg').val())
.done(function () {
console.log('Success!');
})
.fail(function (e) {
console.warn(e);
});
});
$("#joingroup").click(function() {
chat.server.joinGroup("SecretGroup")
.done(function () {
console.log('Success!');
$("#joingroup").hide();
$("#sendgroup").show();
})
.fail(function (e) {
console.warn(e);
});
});
$("#broadcast").hide();
$("#joingroup").hide();
//$("#sendgroup").hide();
// Start the connection
$.connection.hub.start(function () {
$("#broadcast").show();
$("#joingroup").show();
console.log("Success");
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment