locked
Same Code Causes Differences when Started RRS feed

  • Question

  • User246147646 posted

    I am new to SignalR, with C#, using Visual Studio 2019.  I am having trouble with the guess a number code pasted below.  Fixing any errors is good, but what I really need is help with why my code is running differently with the same code.  Sometimes it runs statements out of order, sometimes incomplete, and before it seemed to be running an old version of the default.html.

    When I asked for help after having some problems, the user found that it ran fine on his computer.  

    When I make a change in settings, code, fixing studio, and resetting the computer, the code seems to do better and than there are some problems again.

    When it seemed to be running a different version, it happened on both of my computers!

    The rest  of the code is at  :  https://github.com/Joshei/signalr1.  This is really important to me, please help me.  

    Default.html :

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <!--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> -->
        <script src="signalr/hubs"></script>
        <script type="text/javascript">
    
        $(function () {
    
            var ainteger = "0";
            var buttons = ['1', '1', '1', '1', '1', '1'];
            var index = 0;
            var chat = $.connection.chatHub;
    
                $("#Join").click(function () {
    
                //restore this when working to prevent second click on same register button
                $('#Join').hide();
                $('#Name').hide();
                chat.server.register($("#Name").val());
                if (ainteger == "1") {
                   
                    chat.server.printthename();
                }
                //ainteger = "1";
            });
    
                
            chat.client.setinteger =   function () {
            
                ainteger = "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>

    
    
    using System;
    using System.Web;
    using Microsoft.AspNet.SignalR;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using System.Linq;
    using System.Diagnostics;
    
    
    
    
    namespace SignalRChat
    {
        public class ChatHub : Hub
        {
            //https://www.codeproject.com/Questions/233650/How-to-define-Global-veriable-in-Csharp-net
            private static int whoseturn = 0;
            private static int integer = 0;
            private clients A_client = new clients();
            private static readonly List<clients> ClientList = new List<clients>();
            public override Task OnConnected()
            {
                return base.OnConnected();
            }
            public void printthename()
            {
                string name = "";
                if (whoseturn == 0)
                { 
                    name = "Player one ";
                    Clients.Client(ClientList[0].ConnectionId).printname(name);
                    name = "Player two ";
                    Clients.Client(ClientList[1].ConnectionId).printname2(name);
                }
                //second player
                if (whoseturn == 1)
                {
                    name = "Player two ";
                    Clients.Client(ClientList[0].ConnectionId).printname(name);
                    name = "Player one ";
                    Clients.Client(ClientList[1].ConnectionId).printname2(name);
                }
                if (whoseturn == 0)
                {
                    whoseturn = 1;
                }
                else if (whoseturn == 1)
                {
                    whoseturn = 0;
                }
    
            }
            public void  register(string name1)
            {
                A_client.ConnectionId = Context.ConnectionId;
                A_client.Name = name1;
                ClientList.Add(A_client);
    
                if (integer == 0)
                {
                    integer = integer + 1;
                }
                else if (integer == 1)
                {
                    Clients.Client(ClientList[1].ConnectionId).setinteger();
                    Clients.Client(ClientList[0].ConnectionId).setinteger();
                }
            }
            
         }
    
    }

    Thank you,

    Joshueir

    The code is run with Visual Studio 2019 and two browser tabs.

    Friday, November 29, 2019 8:21 PM

Answers

  • User61956409 posted

    Hi Joshua,

    I modified the code you shared, and the client methods printname and printname2 can be executed well to display name.

    $(function () {
    
        var ainteger = "0";
        var buttons = ['1', '1', '1', '1', '1', '1'];
        var index = 0;
        var chat = $.connection.chatHub;
    
        $("#Join").click(function () {
            $('#Join').hide();
            $('#Name').hide();
    
            //real code beneath
            //chat.server.register($('#Name').val());
    
            chat.server.register("a");
            //first goes here than goes into register right above?!?!?
    
            //comment out here
            //if (ainteger == "1") {
            //    chat.server.printthename();
            //}
    
        });
                
        chat.client.setinteger =   function () {
            ainteger = "1";
    
            chat.server.printthename();
        };
    
        chat.client.printname = function (message) {
            $("#turn").html(message + ", its your turn!");
        };
    
        chat.client.printname2 =  function (message) {
            $("#turn").html(message + ", its not your turn!");
        };
    
        $.connection.hub.start().done();
    });

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 6, 2019 7:50 AM

All replies

  • User246147646 posted

    I took out the comments, please help!

    Josh

    Sunday, December 1, 2019 12:26 PM
  • User61956409 posted

    Hi Joshueir,

    Sometimes it runs statements out of order, sometimes incomplete, and before it seemed to be running an old version of the default.html.

    The rest  of the code is at  :  https://github.com/Joshei/signalr1

    In source code you shared, we can find the most of code snippet seems be commented out. We can reproduce the issue based on it, if possible, please clarify more about your scenario/requirements (What you want to achieve with the above code), so that we can understand and troubleshoot the issue better.

    Besides, you said "sometimes it runs statements out of order", you can try to debug your code step by step and troubleshoot it.

    With Regards,

    Fei Han

    Monday, December 2, 2019 5:52 AM
  • User246147646 posted

    Hello, Fei Han.  The code was commented out because I wanted to make sure you understood that this code has nothing to do with the problem.  The game is going to be a guess the number game using two browser tabs (for now.)  Right now I am using Internet Explorer with Visual Studio 2019.  The users are determined when the register function is called because the register button is pressed.  When this function runs twice the ainteger variable is set to valid and the text "player one" and "player two" should be displayed above the buttons.  That's all!

    Now, the game does not work (no names are displayed) and when tracing the code, line 38 works before entering the register function.

    I'm having a tough time getting this to work and I am unsure if it is acting weird because of errors or something else.  For example, an earlier version worked on someone else's computer and not mine.

    The code has been altered a very little bit, and can be cloned/viewed from the same GitHub address.

    Thanks,

    Joshua

    Tuesday, December 3, 2019 8:54 PM
  • User61956409 posted

    Hi Joshua,

    Now, the game does not work (no names are displayed) and when tracing the code, line 38 works before entering the register function.

    Do you mean the code line 38 of JavaScript client? or Hub server?

    With Regards,

    Fei Han

    Wednesday, December 4, 2019 6:29 AM
  • User246147646 posted

    Oh, in the default.html file.

    Wednesday, December 4, 2019 1:36 PM
  • User61956409 posted

    Hi Joshua,

    I modified the code you shared, and the client methods printname and printname2 can be executed well to display name.

    $(function () {
    
        var ainteger = "0";
        var buttons = ['1', '1', '1', '1', '1', '1'];
        var index = 0;
        var chat = $.connection.chatHub;
    
        $("#Join").click(function () {
            $('#Join').hide();
            $('#Name').hide();
    
            //real code beneath
            //chat.server.register($('#Name').val());
    
            chat.server.register("a");
            //first goes here than goes into register right above?!?!?
    
            //comment out here
            //if (ainteger == "1") {
            //    chat.server.printthename();
            //}
    
        });
                
        chat.client.setinteger =   function () {
            ainteger = "1";
    
            chat.server.printthename();
        };
    
        chat.client.printname = function (message) {
            $("#turn").html(message + ", its your turn!");
        };
    
        chat.client.printname2 =  function (message) {
            $("#turn").html(message + ", its not your turn!");
        };
    
        $.connection.hub.start().done();
    });

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 6, 2019 7:50 AM
  • User246147646 posted

    Hello Fei Han, I was so happy to get your response!  So far, on my side the program is working.  I understand your code, but I would also like to know why my code didn't work right.  If I understand this, than I will be better at programming with SignalR.

    Happy Holidays,

    Josh

    Saturday, December 7, 2019 5:10 PM
  • User61956409 posted

    Hi Josh,

    In your hub server, you are using a variable integer to check if current user is first registered user and using another variable whoseturn to check current turn, you have achieved it in hub server side, you just need to call printthename method after the second users has been registered, need not to check it again on client side, so I modified your code and commented out the below code.

    //comment out here
    //if (ainteger == "1") {
    //    chat.server.printthename();
    //}

    Besides, you can know and get documents about SignalR from here.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/

    With Regards,

    Fei Han

    Thursday, December 12, 2019 1:57 AM
  • User246147646 posted

    Is this some sort of rule, or did you just try to rewrite it and it worked?  It's fine if you did.

    Joshua

    Saturday, December 14, 2019 4:07 PM