signalR asp.net设置用户名和连接ID

时间:2017-06-04 22:48:20

标签: jquery asp.net signalr signalr-hub signalr.client

我是signalR中的新手(和JQuery:D).....我想设置一些用户名并将其映射到connectionId ...在检查了一些样本后这是我的代码。如果它不起作用.. ...页面正在运行而没有错误,但div id =" msg"保持不变。

现在我想要的是在客户端页面上编写用户名和连接列表。

SERVER

<GooglePlacesAutocomplete
                placeholder='Search'
                minLength={2} // minimum length of text to search
                autoFocus={true}
                listViewDisplayed='auto'    // true/false/undefined
                fetchDetails={true}
                renderDescription={(row) => row.description} // custom description render
                onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
                    console.log(data);
                    console.log(details);
                }}
                getDefaultValue={() => {
                    return ''; // text input default value
                }}
                query={{
                    // available options: https://developers.google.com/places/web-service/autocomplete
                    key: 'AIzaSyCj2w25IckuLttTxl1MMhhQ0D8aG-tnSZc',
                    language: 'en', // language of the results
                    types: '(cities)', // default: 'geocode'
                }}
                styles={{
                    description: {
                        fontWeight: 'bold',
                    },
                    predefinedPlacesDescription: {
                        color: '#1faadb',
                    },
                }}

                currentLocation={false} // Will add a 'Current location' button at the top of the predefined places list
                currentLocationLabel="Current location"
                nearbyPlacesAPI='GooglePlacesSearch' // Which API to use: GoogleReverseGeocoding or GooglePlacesSearch
                GoogleReverseGeocodingQuery={{
                    // available options for GoogleReverseGeocoding API : https://developers.google.com/maps/documentation/geocoding/intro
                }}
                GooglePlacesSearchQuery={{
                    // available options for GooglePlacesSearch API : https://developers.google.com/places/web-service/search
                    rankby: 'distance',
                    types: 'food',
                }}


                filterReverseGeocodingByTypes={['locality', 'administrative_area_level_3']} // filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities

                predefinedPlaces={[homePlace, workPlace]}

                debounce={200} // debounce the requests in ms. Set to 0 to remove debounce. By default 0ms.
            />

客户端

namespace SERVXZ
{
    class UserConn
    {
        public string Usrname { set; get; }
        public string ConnectionID { set; get; }
    }

    public class XZHUB : Hub
    {
        //public static ConcurrentDictionary<string, string> MyUsers = new ConcurrentDictionary<string, string>();
        List<UserConn> ulist = new List<UserConn>();
        public override Task OnConnected()
        {
            var us = new UserConn();
            us.Usrname = Context.QueryString["username"];
            us.ConnectionID = Context.ConnectionId;
            ulist.Add(us);

           // var username = Context.QueryString["username"];
           // MyUsers.TryAdd(Context.ConnectionId,username);


            return base.OnConnected();
        }

        public void Send()
        {
            Clients.All.broadcastMessage(ulist[0]);

        }
    }
}

提前谢谢.......................:)

1 个答案:

答案 0 :(得分:1)

The problem is the hub name. If you don't set the hub name it'll be generated with the name xZHUB.

So you could do 2 things:

Use the default name:

var con = $.connection.xZHUB;

Or change the hub name

[HubName("XZHUB")]
public class XZHUB : Hub

There are also another things you need to check:

The hubs are not static, so ulist will be recreated in each call. You should consider making it static or change that logic.

The "SUB" button will make a post so it won't work. I replaced it with a normal button:

<button ID="SUB">Submit</button>