0

I built already windows forms app signalr server and hub named "MyHub". I created new MVC project website to connect to my windows forms server, nevertheless i stack with that how to connect to the server. Can you help me out? This is my current code below. Besides on "MyHub" there is at the moment one method called "Send" which sends out messages to all, how to make something like: HubProxy.On (this is from win forms) but here in mvc.

@{
    ViewBag.Title = "Chat";
}
<fieldset>
    <legend style="color:orangered">Welcome To  Satya's signalR MVC Group Chat Club</legend>
</fieldset>
<div class="form-group col-xl-12">
    <label style="color: blue; font-style: oblique;font-size: medium" id="label1">Write Your Message Here!</label><br />
    <textarea class="form-control" rows="4" cols="40" id="message" placeholder="Share what's in your mind..."></textarea>
    <br />
    <input type="button" class="btn btn-primary" id="sendmessage" value="Send" />
    <br />
    <br />
    <label style="color: blue;font-style:oblique;font-size:medium" id="label2">Group Chat Conversations History</label>
    <div class="container chatArea">
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
</div>
@section scripts {
    <script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
    <script src="~/signalr/hubs"></script>
    <script>

        $(function () {
            var chat = "MyHub"
            chat.hub.url = 'http://xxxxxxxx:4848/';
            chat.hub.qs = { 'username' : 'John' };
            chat.client.Send = function (name, message) {
                $('#discussion').append('<ul style="list-style-type:square"><li><strong style="color:red;font-style:normal;font-size:medium;text-transform:uppercase">' + htmlEncode(name) + '  ' + '<strong style="color:black;font-style:normal;font-size:medium;text-transform:lowercase">said</strong>'
                    + '</strong>: ' + '<strong style="color:blue;font-style:oblique;font-size:medium">' + htmlEncode(message) + '</strong>' + '</li></ul>');
            };
            $('#displayname').val(prompt('Your Good Name Please:', ''));
            $('#message').focus();
            chat.start().done(function () {
                $('#sendmessage').click(function () {
                    chat.client.Send($('#displayname').val(), $('#message').val());
                    $('#message').val('').focus();
                });
            });
        });
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}

EDIT:

On server side:

class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            //app.UseCors(CorsOptions.AllowAll);
            //app.MapSignalR();
             // Branch the pipeline here for requests that start with "/signalr"
            app.Map("/signalr", map =>
            {
                // Setup the CORS middleware to run before SignalR.
                // By default this will allow all origins. You can 
                // configure the set of origins and/or http verbs by
                // providing a cors options with a different policy.
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    // EnableJSONP = true
                };
                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch already runs under the "/signalr"
                // path.

                hubConfiguration.EnableDetailedErrors = true;
                map.RunSignalR(hubConfiguration);
            });
        }
    }


    public class MyHub : Hub
    {
     public async Task Send(string name, string message)
        {
            await Clients.All.addMessage(name, message);
            uListHistory.Add(new ChatHistory { name = name, message = message });
        }
....

Now on MVC client side i have:

  @{
        ViewBag.Title = "Chat";
    }
    <fieldset>
        <legend style="color:orangered">Welcome To  Satya's signalR MVC Group Chat Club</legend>
    </fieldset>
    <div class="form-group col-xl-12">
        <label style="color: blue; font-style: oblique;font-size: medium" id="label1">Write Your Message Here!</label><br />
        <textarea class="form-control" rows="4" cols="40" id="message" placeholder="Share what's in your mind..."></textarea>
        <br />
        <input type="button" class="btn btn-primary" id="sendmessage" value="Send" />
        <br />
        <br />
        <label style="color: blue;font-style:oblique;font-size:medium" id="label2">Group Chat Conversations History</label>
        <div class="container chatArea">
            <input type="hidden" id="displayname" />
            <ul id="discussion"></ul>
        </div>
    </div>
    @section scripts {
        <script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
        <script src="http://52.236.38.106:4848/signalr/hubs"></script>
        <script>

            $(function () {
                $.connection.hub.url = "http://52.236.38.106:4848/signalr";
                var chat = $.connection.myHub;
                chat.client.AddMessage = function (name, message) {
                    $('#discussion').append('<ul style="list-style-type:square"><li><strong style="color:red;font-style:normal;font-size:medium;text-transform:uppercase">' + htmlEncode(name) + '  ' + '<strong style="color:black;font-style:normal;font-size:medium;text-transform:lowercase">said</strong>'
                        + '</strong>: ' + '<strong style="color:blue;font-style:oblique;font-size:medium">' + htmlEncode(message) + '</strong>' + '</li></ul>');
                };
                $('#displayname').val(prompt('Your Good Name Please:', ''));
                $('#message').focus();
                $.connection.hub.start().done(function () {
                    $('#sendmessage').click(function () {
                        chat.server.send($('#displayname').val(), $('#message').val());
                        $('#message').val('').focus();
                    });
                });
            });
            function htmlEncode(value) {
                var encodedValue = $('<div />').text(value).html();
                return encodedValue;
            }
        </script>
    }
Mickey25
  • 21
  • 5
  • 1. Mistake: Do you have a generate hubproxy file? Is wrong i think – Stephu Mar 25 '18 at 17:59
  • Does your server generate the hubfile? – Stephu Mar 25 '18 at 20:38
  • @Tester Could you tell me how to do so? Hub class is in windows forms app where i developed server code and there is hub class i want MVC client website to connect to by IP of the server. – Mickey25 Mar 25 '18 at 20:45
  • @Tester hmm how to do so? – Mickey25 Mar 25 '18 at 20:45
  • https://stackoverflow.com/questions/39975129/signalr-generated-proxy-vs-dynamically-created-hub-file there you find the difference between with and without generated proxy. Rest should be clear. Maybe you should read https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client befire you ask any additional questions – Stephu Mar 25 '18 at 20:52
  • @Tester Look at EDIT in my main code - this is what i have on server side and mvc client side - mvc client cannot connect even to server (from other win form clients i can) but not from mvc not mentioning about sending messages. What to change? – Mickey25 Mar 25 '18 at 21:21
  • Do you have any errors for example im browswr console? – Stephu Mar 25 '18 at 21:30
  • @Tester no look here: https://www.dropbox.com/s/icgyd5gl2nd4umk/111.png?dl=0 – Mickey25 Mar 25 '18 at 21:39
  • Does you code is rslly called? Try write some logs to console. Missing render script? – Stephu Mar 25 '18 at 21:43
  • @Tester I am getting ask from website about name which comming from that line: $('#displayname').val(prompt('Enter your name:', '')); when accessing debuging website. – Mickey25 Mar 25 '18 at 21:47
  • Add messagrs for example also to the done of the promise and trace whats running and whats not – Stephu Mar 25 '18 at 21:52
  • @Tester can you show where to put what, not MVC expert. – Mickey25 Mar 25 '18 at 21:54
  • Extend the code after connection should start. Something like $.connection.hub.start().done(function() { alert("Connected to Signalr Server"); $('#sendmessage').click(function () { chat.server.send($('#displayname').val(), $('#message').val()); $('#message').val('').focus(); }); }) .fail(function(error) { alert("failed in connecting to the signalr server"+error); }) – Stephu Mar 25 '18 at 21:59
  • @Tester After i place my name in pop up window i immediatly get message : failed in connecting to server – Mickey25 Mar 25 '18 at 22:02
  • ... would be simpler if you then write the whole error there! – Stephu Mar 25 '18 at 22:03
  • @Tester i do not see any other information in message box – Mickey25 Mar 25 '18 at 22:04
  • Did you make: .fail(function(error) { alert("failed in connecting to the signalr server"+error); }) – Stephu Mar 25 '18 at 22:06
  • @Tester you catched it remotly :) : Failed in connecting to the signalr server : Error You are using a version of the client that isn;t compatible with the server Client version 1.5, server version: 1.4 . Hmm i have signalr on both client and server 2.2.3 if recall why 1.4 and 1.5 :O – Mickey25 Mar 25 '18 at 22:10

1 Answers1

0

Server and Client Signalr Version must match

According your error messages it's looks like you do not have correct signalr versions.

Hint: Next time please try do debug by yourself as much as you can. For that add trace messages to your code and show in browser windows for errors.

Stephu
  • 3,236
  • 4
  • 21
  • 33
  • But why it shows 1.4 or even 1.5 i checked on on both sides i have 2.2.3 – Mickey25 Mar 25 '18 at 22:16
  • Out of your sample code the version of your server part is not visible. – Stephu Mar 25 '18 at 22:17
  • In mean that if would ask at the beginning with this error message it would be very faster. In JS you can also you Console.Log("Somestring") to log to console... – Stephu Mar 25 '18 at 22:17
  • where should i put this?: Console.Log("Somestring") . Just asking on next time. – Mickey25 Mar 25 '18 at 22:31
  • That would mean signalrR has to be exactly the same version on both client and server side otherwise it fail from what i can see. – Mickey25 Mar 25 '18 at 22:32
  • The thing with console.log is only a hint for you how you can better debug. Yes you must have same version on both sides. – Stephu Mar 25 '18 at 22:35
  • With Console.Log ("some string ") you can log into broesers console. I often use it for debugging. – Stephu Mar 25 '18 at 22:41
  • Since i know .Fail has to be putten in code after function however could you put in answer where to put this Console ? I do not get. P.S I updated server signalr and it works know. – Mickey25 Mar 25 '18 at 22:44
  • Read this. https://stackoverflow.com/questions/4539253/what-is-console-log and vote for answer becausr of long discussions:-) – Stephu Mar 25 '18 at 22:49
  • i see now but what do i need additionaly show something in console since i have .Fail which raise message box? – Mickey25 Mar 25 '18 at 22:52
  • At the moment nothing but mybe you need something do debug in future:-) – Stephu Mar 26 '18 at 04:36