SignalR Question about Function Call, Please Help Me!

Started by
13 comments, last by AndyPett 4 years, 6 months ago

I am using SignalR and I can't get a C# command to call the printinitial JavaScript function.  I have a similar example that works fine.  When debugging it does step on this.  I've been scratching my head for hours, please help me.  

Here are the two functions, what could it be?

C#.net :


public void register(string name)
        {
            int myplayernumber = -1;

            A_client.ConnectionId = Context.ConnectionId;
            A_client.Name = name;
            ClientList.Add(A_client);

            if (integer == 0)
            {
                myplayernumber = 1;
                integer = integer + 1;
            }
            else if (integer == 1)
            {
                Clients.Client(ClientList[0].ConnectionId).printinitial();
                myplayernumber = 2;
            }

            if (myplayernumber == 2)
            {
                 Clients.Client(ClientList[1].ConnectionId).printinitial();
            }




        }

HTML / JavaScript / JQuery


chat.client.printinital = function ()
{
	$("#turn").html("print_this");
};

 

Thank you,

Josheir

Advertisement

So, in debugging, the method "register" i being called correctly?
I see you are using if (integer == 0) -> where is the variable "integer" set?

It is possible that integer is never 0 or 1, and therefore the functions are never called. Could that be the case, or is "integer" set somewhere else?

Also, if integer == 0, no call to the printinitial function is being made.

Andy Pett

The Integer is a static, set to 0.  I changed the code, and the call to register is being made fine.

 


public void register(string name)
        {
            
            A_client.ConnectionId = Context.ConnectionId;
            A_client.Name = name;
            ClientList.Add(A_client);

            if (integer == 0)
            {
                Clients.Client(ClientList[0].ConnectionId).printinitial();
                integer = integer + 1;
            }
            else if (integer == 1)
            {
                Clients.Client(ClientList[1].ConnectionId).printinitial();
                
            }

        }

 

 

I have checked so much that I think maybe it is how the project was set up?

EDIT: I just noticed, you have called the function printinital (no i between t and a). That might be your problem.

Original reply:

Would you be able to provide the code on the client side as well?
Have you tried console.log() function to verify that the printinitial function is being called?

Andy Pett


<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
</head>


<body>
    
  	<!--Script references. -->
    <!--Reference the jQuery library. -->
    <script src="Scripts/jquery-3.3.1.min.js"></script>
    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-2.2.2.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="signalr/hubs"></script>
    <!--Add script to update the page and send messages.-->
    
    <!--<style type="text/css">
        #button2 {
            display: none;
        }
    </style>-->


    <script type="text/javascript">

        

        $(function () {

            var buttons = ['1', '1', '1', '1', '1', '1'];
            var index = 0;
            var chat = $.connection.chatHub;


            //called from server's play - mesage is button pressed
            chat.client.pressbutton = function (message) {

                if (message == "1") {

                    $('header').append(jQuery("#button1").prop("disabled", true));
                }
                if (message == "2") {

                    $('header').append(jQuery("#button2").prop("disabled", true));
                }
            };


            chat.client.printinitial = function () {

                console.log('2');
                // $("#turn").html("Player one, it's your turn!");
                $("#turn").html("aaaaa");


            };

            chat.client.printname = function (message) {

                $("#turn").html("" + message + ", it's your turn!");
            };


            $('#Join').click(function () {

                $('#Join').hide();
                $('#Name').hide();
                //put name in here
                chat.server.register($('#Name').val());

            });


            $('#button2').click(function () {
                
                chat.server.play($('#Name').val(), "2");
                // Clear text box and reset focus for next comment.
                //$('#message').val('').focus();
            });

            $('#button1').click(function () {
                console.log('here');
                //disables button calls press button to disable, if correct named user, also changes whosturnisit
                chat.server.play($('#Name').val(), "1");


            });

            
            $.connection.hub.start().done();

        });

    </script>


     <div id="div1" class="container">

        <form>

            <div id="turn">  </div>
            <input type="button" id="button1" name="button1" value="1" />
            <input type="button" id="button2" name="button2" value="2" />
            <input type="button" id="button3" name="button3" value="3" />
            <input type="button" id="button4" name="button4" value="4" />
            <input type="button" id="button5" name="button5" value="5" />
            <input type="button" id="button6" name="button6" value="6" />
            <input type="text" id="Name" name="Name" value="a" />
            <input type="button" id="Join" name="Join" value="Register" />



        </form>
    </div>



</body>
</html>

Thank you Andy, but that was an error I created when putting everything back together.

That was the client side.

The other files can be found in the signalr folder in the master branch at:  https://github.com/Joshei/signalr

Yes, I tried the console.log().  It didn't go into the function.

When debugging, can you verify that the code between if (integer == 0) or if (integer == 1) is being run?

 

have you Verified the values for your connectionIds (in your Clients list?). Personally, I have used SignalR and added each user to a Group where the group name is their unique ID. This is because I can then know what their «ID» will be in stead of keeping track of all their connectionIds (which can be many).

I will take another look when I Get in front of a computer. I am reading this on a small cell phone screen so I might be missing something.

Andy Pett

So, I have found a solution.
It turns out that when you add connection Id's to your list, you add more and more (because a user will create a new connectionId all the time). But you still only check index 0 and 1 from the list, which contains a connectionId the user is no longer using apparently.

So by using the Group I mentioned in my last post I was able to make it work.

In the ChatHub class, add the following method:


public void AddToGroup(string userId)
{
  	// You may want to add logic that prevents users
  	// to have the same userId. I have not considered
  	// that in this example.
	Groups.Add(Context.ConnectionId, userId).Wait();
	Clients.Group(userId).printinitial();
}

In the default.html page, add the following:


$.connection.hub.start().done(function () {

	chat.server.addToGroup($('#Name'));

});

Now, whenever you want to send notifications to a user, just use the Group call in stead of the Client call.

Ex:


    
            if (integer == 0)
            {
                Clients.Group(userId).printinitial();
                integer = integer + 1;
            }
            else if (integer == 1)
            {
                Clients.Group(userId2).printinitial();
            }

 

Andy Pett

I read your post and  I am wondering if I am understanding this right.  As far as I know, right now the Join button is clicked once per user, and is only used in two different browsers.  So, there should only be two connection IDs.  The list has been checked and it does hold two IDs.  Also, the code completes the entire function, it just doesn't enter the printinitial function.

Thanks in advance,

Josheir 

You're right.
And once I fixed this line:


            chat.client.printinital = function () {

to


            chat.client.printinitial = function () {

(Notice the spelling error)

it worked fine.

However, if the user refreshes the browser, they will get a new connectionId, so down the road you are likely to not retrieve the correct connectionId as long as you only retrieve one of the first two that are registered.

Andy Pett

This topic is closed to new replies.

Advertisement