My chat with signalR is not working, I’ve followed several tutorials and does not work

Asked

Viewed 23 times

0

File [ Chathub ]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace Sandes
{
    public class ChatHub : Hub
    {
        public void Send(string nome, string mensagem)
        {
            Clients.All.broadcastMessage(nome, mensagem);
        }
    }
}

Archive [chatApp]

<div style="margin-top:25px;" class="container">
    <label>Nome:</label>
    <input type="text" id="displayname" /><br/>
    <label>Mensagem:</label>
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />

    <ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="~/Scripts/jquery-1.10.2.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.-->

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message.
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page.
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val('');
        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
</script>

and also added the command ( app.Mapsignalr(); )

  • Please fix your code.

  • Sorry, first time using the platform.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.