简体   繁体   中英

SignalR in ASP.NET WebForms

In one of my projects I need to use SignalR library. So I decided to create one simple app to check how it works. I created this app based on a few tutorials which show how to write chat room.
The only difference is that my project is WebForms and I also use ASP.NET controls in it.

This is what I've done so far:

  1. I installed SignalR

  2. I created ChatHub file:

ChatHub.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Microsoft.AspNet.SignalR;

using Microsoft.AspNet.SignalR.Hubs;    
namespace SignalRTest
{

    public class ChatHub : Hub
    {
        public void sendMessage(string msg)
        {
            Clients.All.addMessage(msg);
        }
    }
}
  1. In Default.aspx page:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.10.2.min.js"></script> <script src="Scripts/jquery.signalR-2.2.0.min.js"></script> <script src="/signalr/hubs"></script> <script type="text/javascript"> $(function () { var chat = $.connection.chatHub; chat.client.addMessage = function (msg) { $('#chat_list').append("<li>" + msg + "</li>"); }; $.connection.hub.start().done(function () { $('#Button1').click(function () { chat.server.sendMessage($('#TextBox1').val()); }); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Send" /> </div> <div> <ul id="chat_list"> </ul> </div> </form> </body> </html> 

  1. Startup file:

Startup.cs

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(SignalRTest.Startup))]
namespace SignalRTest
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

I set up breakpoints and clicking on Send button fires sendMessage method on server and also addMessage JS function on clients side. But here is the problem:

When I send something to other user he can see that message in <ul></ul> element. But not me. Then when he sends me response I can see it but his list gets cleared.

Can it be caused by using ASP controls?
I'm not sure if I get references to these controls in best way.

PS. Sorry for bad formatting but I can't handle C# code.

Your issue is that you want your button to both run server side and fire your javascript code.

<asp:Button ID="Button1" runat="server" Text="Send" />

the runat="server" bit will mean that you will see the page refresh, as the application will try to run server side code, hence the messages will clear.

If you run the code on an ordinary html element, this code should work fine.

IE go for:

<input ID="Button1" type="button" value="Send" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM