tarzanbappa
tarzanbappa

Reputation: 4958

Signalr Uncaught TypeError: currentHub.server.OnConnected is not a function(…)

I'm connecting to a sugnalr Hub using signalr javascript client.

The Hub OnConnected Method is this

   public override Task OnConnected()
        {
            try
            {
                 var cType = Context.QueryString["type"];
                var connectionId = Context.ConnectionId;
                var connectedUserList = (from d in Users
                                         where d.ClientType == cType
                                                          select d).ToList();
                if (connectedUserList.Count > 0)
                {
                    var conUser = connectedUserList.First<ConnectedUsers>();
                    conUser.ConnectionIds.Add(connectionId);
                }
                else
                {
                    var newUser = new ConnectedUsers
                    {
                        ConnectionIds = new HashSet<string> {connectionId}
                       ,
                        ClientType = cType
                    };
                    Users.Add(newUser);
                }
            }
            catch (Exception ex)
            {

            }
            return base.OnConnected();
        }

Other Hub method

 public void PushMessage()
        {
            try
            {
                var context = GlobalHost.ConnectionManager.GetHubContext<MainHub>();
                   var user = (from d in Users
                                       where d.ClientType == "WIN"
                                       select d).Single();
                context.Clients.Clients(user.ConnectionIds.ToArray()).sendAlert("EXCEL");
            }
            catch (Exception e)
            {
                //throw;
            }
        }

And the Javascript client connect code as below

<html>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    <script src="~/Scripts/jquery.signalR-2.2.1.js"></script>
    <script src="@Url.Content("http://localhost:1010/signalr/hubs")"></script> 
    @RenderSection("scripts", required: false)
</body>
</html>


<script type="text/javascript">

    var currentHub = [];
    var connectionDetails = {};
    var SignalRHandler = {
        _settings: {
            connectionDetails: {
                id: "1",
                userName: ""
            },
            hub: $(),
            callBack: $(),
            url: ""
        },

        initilaize: function(options) {
            this._settings = $.extend(true, this._settings, options);
            if (!$.connection)
                alert("$.connection undefined");
            else
                this._bindEvents();
        },

        _bindEvents: function() {
            // Start Hub
            $.connection.hub.url = this._settings.url;
            $.connection.hub.qs = { 'type': 'WEB' }
            currentHub = $.connection[this._settings.hub], connectionDetails = this._settings.connectionDetails;

            if (currentHub != undefined) {

                currentHub.client.sendAlert = this._settings.callBack;

                $.connection.hub.start().done(function() {
                    currentHub.server.OnConnected();

                });
                $.connection.hub.disconnected(function() {
                    setTimeout(function() {
                        $.connection.hub.start().done(function() {
                            currentHub.server.OnConnected();
                        });
                    }, 2000); // Restart connection after 2 seconds.
                });
            }

        },
        pushChatMessage: function () {
            currentHub.server.pushMessage();
        },
        showMsg:function(msg) {
            alert(msg);
        }
    };
    jQuery(document).ready(function($) {

        SignalRHandler.initilaize({
            callBack: SignalRHandler.showMsg,
            hub: "mainHub",
            url: "http://localhost:1010/signalr/"

        });

    });
</script>

This code is working perfectly and connecting to the hub and receive messages from the hub continuously. But when I check the debugger console it says.

Uncaught TypeError: currentHub.server.OnConnected is not a function(…)

How to get rid of this error?

Upvotes: 0

Views: 1947

Answers (1)

sam2929
sam2929

Reputation: 479

When you call server methods from the SignalR client-side, the method name should be in camel-case unless you specified a different name using the HubMethodName attribute on your method in the hub.

So in your case the call you the OnConnected method should look like this: currentHub.server.onConnected();

Reference: https://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#callserver

Upvotes: 2

Related Questions