【问题标题】:SignalR C# server and ASP.NET clientSignalR C# 服务器和 ASP.NET 客户端
【发布时间】:2014-01-02 21:17:06
【问题描述】:

我需要客户端向服务器发送消息,服务器向客户端推送消息。

在将其添加到我正在开发的网络应用程序之前,我正在尝试做一个小示例。

我阅读了一些我找到的教程并看到了一些示例,但我仍然无法使其工作。

服务器:

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(TestServer.Startup))]
namespace TestServer
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;


namespace TestServer
{
    [HubName("ServerHub")]
    class ServerHub : Hub
    {
        public void send(string message)
        {
            Clients.All.serverUpdate(message);
        }
    }
}

客户: 我想在推送来自服务器的消息时更改 Label2 文本。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="TestClient.MainPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

    </div>
    </form>
    <script src="Scripts/jquery-2.0.3.min.js"></script>
    <script src="Scripts/jquery-ui-1.10.3.min.js"></script>
    <script src="Scripts/jquery.signalR-2.0.1.min.js"></script>
    <script src="/signalr/hubs"></script>
    <script>
        var hub = $.connection.ServerHub;
        hub.client.serverUpdate = function (str) {
            $("#Label2").text(str);
        };
    </script>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Client.Hubs;
using Microsoft.AspNet.SignalR.Client;

namespace TestClient
{
    public partial class MainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var hubConnection = new HubConnection("http://localhost/signalr");
            IHubProxy ClientHubProxy = hubConnection.CreateHubProxy("ServerHub");
            hubConnection.Start().Wait();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ClientHub ch = new ClientHub();
            ch.sendUpdate("button clicked");
        }
    }
}

我需要在服务器主中添加与 Signalr 相关的内容吗?

在客户端中,我不确定在下一行放置什么 url:

var hubConnection = new HubConnection("http://localhost/signalr");

不确定我将消息发送到服务器的方式(单击按钮后)是否正确。

【问题讨论】:

  • 我没有那个代码了。尝试阅读下面链接中的教程,其中包含不同的代码示例。

标签: asp.net signalr signalr-hub


【解决方案1】:

没有内置的 hub.client.serverUpdate 方法 - 该方法的名称必须与您在集线器上定义的方法的名称匹配(发送)。请参阅此处的入门教程:

http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-getting-started-with-signalr-20

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多