首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SignalR的ExcelDNA RTD

使用SignalR的ExcelDNA RTD
EN

Stack Overflow用户
提问于 2021-10-04 16:01:39
回答 2查看 79关注 0票数 1

我正在尝试将ExcelDNA RTD与ASP.NET SignalR服务器连接起来。每当服务器上发生变化时,就会将一条消息推送到已连接的客户端,并且我的ExcelDna插件将获得新消息,但注册的函数不会更新。

我的RTD服务器:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using ExcelDna.Integration;
using ExcelDna.Integration.Rtd;

namespace DMT.Excel.AddIn
{
    [ComVisible(true)]
    public class SignalRServer : ExcelRtdServer
    {
        private HubConnection _connection;
        private List<Topic> _topics = new List<Topic>();

        public TradesRtdServer()
        {
            _connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:5000/api/test/hub")
                .WithAutomaticReconnect()
                .Build();

            _connection.On<object>("Test", m =>
            {
                foreach (Topic topic in _topics)
                {
                    topic.UpdateValue(m);
                }
            });


            Task.Run(() => _connection.StartAsync());
        }

        protected override bool ServerStart()
        {
            DmtAddIn.Logger.Information("ServerStart");
            return true;
        }

        protected override void ServerTerminate()
        {
            DmtAddIn.Logger.Information("ServerTerminate");
        }

        protected override object ConnectData(Topic topic, IList<string> topicInfo, ref bool newValues)
        {
            DmtAddIn.Logger.Information("ConnectData: {0} - {{{1}}}", topic.TopicId, string.Join(", ", topicInfo));
            _topics.Add(topic);
            return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA);
        }

        protected override void DisconnectData(Topic topic)
        {
            _topics.Remove(topic);
            DmtAddIn.Logger.Information("DisconnectData: {0}", topic.TopicId);
        }
    }
}

我的函数

代码语言:javascript
复制
        [ExcelFunction(Name = "SignalR.Test.RTD")]
        public static object GetSignalRMessages()
        {
            return XlCall.RTD("Excel.AddIn.Trading.SignalRServer", null, "Test");
        }

当我调试时,我可以看到每当从服务器推送消息时都会命中topic.UpdateValue(m);,而不是GetSignalRMessages

在将主题更改传播到函数时,我是否遗漏了什么?

谢谢!

约瑟夫

EN

回答 2

Stack Overflow用户

发布于 2021-10-04 21:33:48

我设法解决了这个问题,方法是从SignalR服务器发送一个字符串,然后在客户端反序列化它

票数 1
EN

Stack Overflow用户

发布于 2021-10-04 21:43:10

ExcelRtdServer检查传入UpdateValue的值是否与之前的值不同。您可能每次都传递相同的值,或者传递一些被解释为Excel数据类型的值作为相同的值(例如,某些对象类型每次都转换为相同的字符串)。

您最好通过比ExcelRtdServer更高级别的IObservable或Rx抽象来构建它。请参阅此处的示例https://github.com/Excel-DNA/Samples/tree/master/RtdClocks

也许像这个结合了Rx和SignalR的项目:https://github.com/jwooley/SignalrRxSamples

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69439032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档