首页 > 解决方案 > 从集线器调用客户端功能在初始连接上不起作用

问题描述

我正在尝试编写一个将位于 Windows 服务中的 signalR 集线器,该集线器将使 javascript 客户端通过网页连接到它。当我启动集线器时,我能够获得稳定的连接,并从 javascript 调用集线器功能。然而,当我尝试从集线器调用 javascript 函数时,我什么也得不到,直到我关闭网页并重新尝试使用完全相同的步骤。

我有一个名为 SignalR.js 的 javascript 文件,它将启动连接,然后我在每个页面上都有页面特定的功能。

SignalR.js

$(document).ready(function () {
    $.connection.hub.url = 'http://localhost:1200/signalr';


srv = $.connection.tcpHub;

$.connection.hub.start().done(function () {
    //alert('connected to hub');
    if (getCookie('UserID') != "") {
        Connect(getCookie('UserID'), getCookie('UniqueKey'));
        gotoState(availableState.READY);
        alert(state);
    }
    else {
        gotoState(availableState.NOT_LOGGED_IN);
    }
});

网页 1

$(function () {
    srv.client.fillFurnaceMap = function (furnaceMap){
         //methodology to populate controls on the webpage using the passed structure
    }
});

带有集线器的 Windows 服务

public void Connect(string user_id, string uniqueKey)
    {
        //if the unique key supplied does not exist then user is not cconnected
        if (ConnectedUsers.Count(x => x.UniqueKey.Equals(uniqueKey)) == 0)
        {
            //if the user name does not exist we will grab the connection id and add them to the connected users 
            //and give it the most current screen version
            if (ConnectedUsers.Count(x => x.UserId.Equals(user_id)) == 0)
            {
                var ConnectionId = Context.ConnectionId;
                ConnectedUsers.Add(new UserViewModel(user_id, uniqueKey, ConnectionId));
                Clients.Caller.userConnected(i_g_screenVersion);
            }
            else
            {
                //if user name does exist has already been connected once and not disconneccted we will update the connection id 
                SetConnection(user_id, uniqueKey);
            }
        }
    }

    public void Fill_Furnace_Map(one_miwwfmpstk01 frnMap)
    {
        log.Info("sending furnace map to front end");
        string str_pv_groupName = "FurnaceChargeMap" + frnMap.page_number;
        foreach (string client in lst_g_groups[str_pv_groupName]) {
            log.Info(Context.ConnectionId);
            Clients.Client(ConnectedUsers.Find(x => x.UniqueKey == client).ConnectionId).fillFurnaceMap(frnMap);
        }
    }

在我断开连接并重新连接之前,c# 集线器不会调用网页 1 中的填充炉图。我打开了信号器跟踪,它甚至没有尝试调用该方法。我是否将它列在错误的位置,或者在初始连接时如何让集线器识别它。

标签: javascriptc#signalr

解决方案


推荐阅读