Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"OnDisconnected(): no suitable method found to override" - SignalR

I've been trying to implement a chat room by following the "Asp.Net SignalR Chat Room" tutorial on CodeProject (http://www.codeproject.com/Articles/562023/Asp-Net-SignalR-Chat-Room). However, I'm getting the error "Hubs.ChatHubs.OnDisconnected(): no suitable method found to override"

ChatHub class:

public class ChatHub : Hub
{
    #region Data Members

    static List<UserDetail> ConnectedUsers = new List<UserDetail>();
    static List<MessageDetail> CurrentMessage = new List<MessageDetail>();

    #endregion

    #region Methods

    public void Connect(string userName)
    {
        var id = Context.ConnectionId;


        if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
        {
            ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName });

            // send to caller
            Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);

            // send to all except caller client
            Clients.AllExcept(id).onNewUserConnected(id, userName);

        }

    }

    public void SendMessageToAll(string userName, string message)
    {
        // store last 100 messages in cache
        AddMessageinCache(userName, message);

        // Broad cast message
        Clients.All.messageReceived(userName, message);
    }

    public void SendPrivateMessage(string toUserId, string message)
    {

        string fromUserId = Context.ConnectionId;

        var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId);
        var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);

        if (toUser != null && fromUser != null)
        {
            // send to 
            Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message);

            // send to caller user
            Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message);
        }

    }

    public override System.Threading.Tasks.Task OnDisconnected()
    {
        var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
        if (item != null)
        {
            ConnectedUsers.Remove(item);

            var id = Context.ConnectionId;
            Clients.All.onUserDisconnected(id, item.UserName);

        }

        return base.OnDisconnected();
    }


    #endregion

    #region private Messages

    private void AddMessageinCache(string userName, string message)
    {
        CurrentMessage.Add(new MessageDetail { UserName = userName, Message = message });

        if (CurrentMessage.Count > 100)
            CurrentMessage.RemoveAt(0);
    }

    #endregion

}

Any clues as to why this is happening?

like image 887
Palindrome Avatar asked Aug 31 '14 20:08

Palindrome


2 Answers

For version 2.1.1+ change

public override Task OnDisconnected()

to

public override Task OnDisconnected(bool stopCalled)
like image 74
Garrett Fogerlie Avatar answered Sep 19 '22 23:09

Garrett Fogerlie


You have a missmatch in your signalR depedency, they changed the signature for OnDisconnected in 2.1.1

So upgrade all projets to 2.1.1 or downgrade all projects to 2.1.0 and it should work

like image 26
Anders Avatar answered Sep 19 '22 23:09

Anders